日长小笔记
显示格式为UTF-8类型
request.setCharacterEncoding(“utf-8”);
从页面获取信息
String email = request.getParameter(“email”);
String pwd = request.getParameter(“password”);
//连接数据库
String url = "jdbc:mysql://localhost:3306/ksj;
String user = “root”;
String password = “自己设置的密码”;
try {
Class.forName(“com.mysql.jdbc.Driver”);//加载数据库驱动
Connection con = DriverManager.getConnection(url,user,password);
//执行sql操作
//连接需要注册的用户表
String sql = “insert into t_user(email,uname,pwd) values(?,?,)”;
PreparedStatement stmt = con.prepareStatement(sql);
//填充sql参数
stmt.setString(1,email);
stmt.setString(2, pwd);
//更新计数
stmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
con.close();//关闭数据库
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//页面跳转
response.sendRedirect(“跳转页面的名称”);
}