在成功链接数据库后,写好了正确的sql语句,但是在获取结果时总是空?终于找到原因了....
代码如下:
<body>
<%!
public static final String DBDRIVER = "com.mysql.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3308/userinfo";
public static final String DBUSER = "root";
public static final String DBPASS = "mysql";
%>
<%
//接收用户名和密码,完成对用户的验证
String u = request.getParameter("username");//这个是从之前的一个jsp页面获取的传过来的参数
String p = request.getParameter("password");
u = new String (u.getBytes("ISO-8859-1"),"UTF-8");//这句话是之所以总是查询结果为空的主要原因,只有将获取过来的参数成功转化,才能匹配数据库中的内容
//到数据库中去验证
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql = "select user_pwd from uinfo where user_name = '"+u+"'";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()){
//说明用户名存在
if(rs.getString(1).equals(p)){
//用户一定合法
response.sendRedirect("demo03.jsp");
}else{
//密码错误
response.sendRedirect("demo01.jsp?errNo=1");
}
}else{
//说明用户名出错
response.sendRedirect("demo01.jsp?errNo=2");
}
}catch(Exception e){
System.out.println(e);
}finally{
rs.close();
pstmt.close();
conn.close();
}
//验证,先不到数据库去验证,就简单验证
/*if(u.equals("jxs")&&p.equals("1")){
//合法,跳转 demo03.jsp
response.sendRedirect("demo03.jsp");
} else{
//不合法,跳转到demo01.jsp
response.sendRedirect("demo01.jsp");
} */
%>
</body>