我写了个JSP用户通过SQL2005登录,但是发布后跳转出现了问题,以下是我代码:
//获取用户名
String name=request.getParameter("admin");
//获取密码
String powd=request.getParameter("mm");
//属性、加载JDBC驱动、连接
Connection con=null;
Statement st=null;
ResultSet re=null;
try {
Class.forName("com.miscrosoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=cd","asd","");
st=con.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
//SQL语句
String sql="select * from admin where admin='"+name+"' and mm='"+powd+"'";
try {
re=st.executeQuery(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if(re.next()){// 有匹配的用户名和密码,登陆成功
response.sendRedirect("htgl.jsp");
}
else{
response.sendRedirect("ht.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
}
下面是出现的错误页面显示:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
com.servlet.ht_servlet.doPost(ht_servlet.java:52)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.
这个是第52行re=st.executeQuery(sql);
谢谢大家了……
------解决方案--------------------
st应该是个null值
------解决方案--------------------
- Java code
String name = request.getParameter("username"); String pwd = request.getParameter("password"); //out.println(name+pwd); String sql ="select * from Info where username='"+name+"' and password='"+pwd+"'"; //out.println(sql); Statement stm= null; ResultSet rs =null; try { stm = conn.createStatement(); rs = stm.executeQuery(sql); if(rs.next()) { session.setAttribute("username",name); response.sendRedirect("index.html"); } else { response.sendRedirect("index1.html"); } } catch(SQLException e) { e.printStackTrace(); }
代码迷推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.daimami.com/search?q=317