基本sql连接数据库

来源:互联网 发布:打开照片的软件 编辑:程序博客网 时间:2024/05/18 09:44
//获取连接,向数据库插入数据
Connection connection=null;
Statement stmt=null;

try {
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_ssh","root","123456");
stmt=connection.createStatement();
String sql="insert into usermes values(null,'"+name+"','"+password+"')";
System.out.println("sql"+sql);
stmt.executeUpdate(sql);
response.sendRedirect("registerSuccess.jsp");
} catch (ClassNotFoundException e) {
response.sendRedirect("registerSuccess.jsp");
e.printStackTrace();
} catch (SQLException e) {
response.sendRedirect("registerSuccess.jsp");
e.printStackTrace();
}finally{
if(stmt!=null){
stmt.close();
stmt=null;
}
if(connection!=null){
connection.close();
connection=null;
}
}
 %>