jsp访问数据库
public class AccessDB {public int read(String name , String pwd){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
int count=0;
try {
//注册驱动、建立连接
conn = JdbcUtils.getConnection();
//创建语句
String sql = "select name,pwd from userconfig where name=? and pwd=?";
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, pwd);
//执行语句
rs = ps.executeQuery();
while(rs.next()) {
count++;
}
}catch(Exception e){
e.printStackTrace();
return -1;
}finally {
JdbcUtils.free(rs, conn, ps);
}
return count;
}
public static void main(String[] args) {
AccessDB a = new AccessDB();
int i = a.read("wjx", "123");
System.out.println(i);
}
}
//此时read方法返回了1(i的值为1)
//用jsp调用却出错了
<body>
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("uname");//name="wjx"
String pwd = request.getParameter("upwd");//pwd="123"
//out.print(name + pwd);
AccessDB adb = new AccessDB();
int i = adb.read(name,pwd);
out.print(i);//输出-1
if(i>0){
request.getRequestDispatcher("A.jsp").forward(request, response);
}else if(i==0){
response.sendRedirect("login.jsp");
}else{
out.print("系统出现异常");
}
%>
</body>
//此时i的值却为-1
//这是为什么?