首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

loginservlet连接数据库时的异常

2012-09-12 
loginservlet连接数据库时的错误我的目的是当数据库中由此用户信息且登录成功的时候,跳到showinfo.jsp这个

loginservlet连接数据库时的错误
我的目的是当数据库中由此用户信息且登录成功的时候,跳到
showinfo.jsp这个页面,但是现在数据库中明明有数据但它还是会跳到
errorpage.jsp。个人感觉是因为没有查询到数据库的数据,但不知道具体怎么改!

Java code
public class LoginServlet extends HttpServlet {    /**     *      */    private static final long serialVersionUID = 1L;    private DataBase database = null;    //初始化函数    public void init() throws ServletException {        database = new DataBase();    }    //处理get请求方法    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doPost(request, response);    }    //处理post请求方法    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        HttpSession session = request.getSession();       //获取用户当前会话        String username = "";                                    String password = "";        String page = null;        if(request.getParameter("txtUserName") != null)            username = request.getParameter("txtUserName");  //获取用户名        if(request.getParameter("txtPassword") != null)            password = request.getParameter("txtPassword");  //获取密码        //判断用户名、密码是否匹配        boolean temp = false;        //System.out.println(username + ", " + password);        Connection con = database.getConnection();        temp = DBUtilitya.isValid(con, username, password);        //关闭数据库连接        database.closeConnection(con);        if(temp){            InfoItem infoitem = null;            Connection con2 = database.getConnection();            infoitem = DBUtilitya.getUser(con2, username);            database.closeConnection(con2);            //将用户信息对象InfoItem存储在session对象当中            session.setAttribute("infoitem",infoitem);            //如果用户通过验证,则返回用户有关信息            page = "showinfo.jsp";        } else{            //如果验证失败,则显示验证失败页面            page = "errorpage.jsp";        }        //重定向到相应的页面        response.sendRedirect(page);        return;    }}

Java code
public class DBUtilitya {    //判断用户名、密码是否正确    public static boolean isValid(Connection con, String username, String userpwd){        PreparedStatement prepStmt = null;        ResultSet rs = null;                //存储查询结果        String selectStatement = "select * from userinfo1 where  username='" +username+"' and userpwd = '" +userpwd+"' ";        boolean temp = false;        try{           prepStmt = con.prepareStatement(selectStatement);           prepStmt.setString(1,username);           prepStmt.setString(2,userpwd);           rs = prepStmt.executeQuery();     //查询数据库并返回结果记录集           if (rs.next())                    //数据库里含有此用户名、密码对则返回true              temp = true;           else              temp = false;                  //否则返回false,用户名、密码不匹配           rs.close();           prepStmt.close();        }catch(Exception e){           e.printStackTrace();        }        return temp;                         //返回验证结果    }    //根据用户名从数据库中获取用户的注册信息    public static InfoItem getUser(Connection con, String username){        PreparedStatement prepStmt = null;        ResultSet rs = null;                 //结果记录集合        InfoItem infoitem = null;        String queryString = "select username,roomnumber1,roomnumber2, roomnumber3, roomnumber4 from userinfo1 where username='" +username+"'";        try{           prepStmt = con.prepareStatement(queryString);           prepStmt.setString(1, username);           rs = prepStmt.executeQuery();     //查询数据库并返回结果记录集合           if(rs.next())           {                   //数据库里含有此用户名、密码对则返回InfoItem对象               infoitem = new InfoItem(rs.getString("username"),                                       rs.getString("roomnumber1"),                                       rs.getString("roomnumber2"),                                       rs.getString("roomnumber3"),                                       rs.getString("roomnumber4")                                       );           } else {                         //否则返回null               infoitem = null;           }           rs.close();           prepStmt.close();        }catch(Exception e){           e.printStackTrace();        }        return infoitem;           } 


之后显示的错误信息是:
Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3729)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3713)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4553)
at zieckey.login.servlet.DBUtilitya.isValid(DBUtilitya.java:14)
at zieckey.login.servlet.LoginServlet.doPost(LoginServlet.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1815)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)


[解决办法]
String queryString = "select username,roomnumber1,roomnumber2, roomnumber3, roomnumber4 from userinfo1 where username='" +username+"'";

改成 

 String queryString = "select username,roomnumber1,roomnumber2, roomnumber3, roomnumber4 from userinfo1 where username = ?";

或者


 prepStmt.setString(1, username); 去掉
[解决办法]
那你自己在 isValid 方法里面调试 看返回值 为什么为false

要么 sql有问题

要么 isValid 方法有异常

这个不难判断啊
[解决办法]
如果一直跳errorpage.jsp界面,肯定是temp验证一直是false;
按照4楼的该法修改查询数据后,打印一下:
rs = prepStmt.executeQuery(); //查询数据库并返回结果记录集
if (rs.next()){ //数据库里含有此用户名、密码对则返回true
System.out.println("name:"+rs.getString("username"));
temp = true;}
else{
System.out.println("no data");
temp = false; 
如果输出为:no data。
把sql语句打印出来看看有什么问题。
[解决办法]
探讨

引用:

那你自己在 isValid 方法里面调试 看返回值 为什么为false

要么 sql有问题

要么 isValid 方法有异常

这个不难判断啊

我是小白,能不能教下我怎么调试!

[解决办法]

探讨

引用:

你可以在数据库直接运行一下这个sql:select * from userinfo1 where username='username' and userpwd = 'password';username和password就是你的条件,看看这个sql能抽到数据吗

select * from userinfo1 where username='李四' an……

热点排行