JDBC在多个查询条目后冻结

时间:2014-05-05 23:26:07

标签: java jdbc

当我输入正确的用户名/密码时,查询工作正常,从客户端到服务器的连接正常工作。但是,如果我使用了不正确的用户名/密码组合,那么正确的程序会冻结。我使用JDBC连接使用java。

String usr = clientName.getText();
            String pass = clientPass.getText();
            String u = null, p = null;

            ResultSet results=null; 

            String stmtLogin = "Select * from userInfo where username = '" + usr + "'";

            try
            {           
                results = statement.executeQuery(stmtLogin);

                results.next();         
                    u = results.getString(1);
                    p = results.getString(2);           


            } 
            catch (SQLException e)
            {
                e.printStackTrace();
            }

            if ((pass.equals(p)) && usr.equals(u))

            {
                message = clientName.getText();
                output.println(message);            



                btnSendM.setEnabled(true);
                btnConn.setEnabled(false);  
                clientName.setEditable(false);
                clientPass.setEditable(false);

                messageBox.append(message + " has entered the chatroom \n");
                btnSendM.setFont(new Font("Verdana", Font.BOLD, 10));
                updateConnList();
            }
            else
                JOptionPane.showMessageDialog(null, "Username Password Incorrect, Please try Again");

1 个答案:

答案 0 :(得分:0)

与您的不完全相同,但这是我的某个应用程序的登录信息。

你的问题可能是你在完成后没有关闭connection

 try {
                String dbName = "ServerSQLite.db";
                connection = DriverManager.getConnection("jdbc:sqlite:" + "serverDatabase/" + dbName);
                Statement statement = connection.createStatement();
                statement.setQueryTimeout(20);  // set timeout to 20 sec.

                ResultSet rs = statement.executeQuery("select * from registratedClients WHERE clientLogin = " + "\'" + loginName + "\'" +"and clientPassword = " + "\'" + password + "\'");
               //Do stuff with resultset
                rs.close();


            } catch (Exception e) {
                // if the error message is "out of memory",
                // it probably means no database file is found
                System.err.println(e.getMessage());
            } finally {
                try {
                    if (connection != null)
                        connection.close();
                } catch (SQLException e) {
                    // connection close failed.
                    System.err.println(e);
                }
            }