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());
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。