jsp中使用的mysql笔记

public static Connection getConnection() throws SQLException,
java.lang.ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/usermanager?useUnicode=true&characterEncoding=gbk";
String username = "root";
String password = "123";
Connection con = DriverManager.getConnection(url, username, password);
return con;
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
res.setCharacterEncoding("utf-8");
String name=req.getParameter("name");
String pw=req.getParameter("pw");
name=new String(name.getBytes("ISO-8859-1"),"utf-8");
pw=new String(pw.getBytes("ISO-8859-1"),"utf-8");
try {
Connection con = getConnection();
Statement sql_statement = con.createStatement();
String query = "select * from user where uname=? and password=?";
PreparedStatement ps=con.prepareStatement(query);
ps.setString(1, name);ps.setString(2, pw);
ResultSet result = ps.executeQuery();

con.close();
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException");
System.err.println(e.getMessage());
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}

你可能感兴趣的:(mysql)