-2
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    try
    {
        Class.forName("java.sql.Driver");  
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "parin");
        Statement stmt=con.createStatement();


        String query="Select * from Liblogin;";
        ResultSet rs=stmt.executeQuery(query);
        String username=rs.getString("username");
        String password=rs.getString("password");


        rs.close();
        stmt.close();
        con.close();


        String enteredUsername=t1.getText().toString();
        String enteredPassword = new String(t2.getText());
        if(enteredUsername.contentEquals(username)&&enteredPassword.contentEquals(password))
        {
            Homepage a=new Homepage();
            a.setVisible(true); 
            this.dispose();
        }
        else
        {
            JOptionPane.showMessageDialog(this,"Incorrect name and password.");
        }
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(this, e);
    }
}             

我正在尝试从 mysql 数据库中检索我的密码和用户名。但由于某些异常(Sql 异常:在结果集开始之前)而无法这样做。

4

1 回答 1

1

您需要在尝试从 ResultSet 行中读取值之前调用rs.first();or 。rs.next();

http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSet.html#first%28%29

rs.next();在处理 ResultSet 中的所有行的 while 循环中调用特别方便。

// Get a result set from SQL query
while (rs.next()) {
   // Process this row
}
于 2013-09-18T15:29:51.907 回答