在结果集开始之前

时间:2018-04-08 16:32:57

标签: java mysql jdbc

这里我试图获得基于idno1的名称,mat,eng,sci标记,但我无法获得所需的输出。如果我使用while(rs.next()){}方法,它指向SQL中的最后一行。那么有人可以帮助我。我已经附上了我的代码

public class display extends javax.swing.JFrame 
{
String url = "jdbc:mysql://localhost:3306/person?useSSL=false";
String name = "Vzlys";
String Pass = "Vzlys@1995";
String query = "select * from details where idno=?";
String query1 = "select * from details";
String id1,name1,mat1,eng1,sci1;
Connection con = null;
ResultSet rs = null;
PreparedStatement st = null;
private void closeActionPerformed(java.awt.event.ActionEvent evt) {                                      
    this.dispose();
}                                     
 private void submitActionPerformed(java.awt.event.ActionEvent evt) {                                       
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(url,name,Pass);
        st  = con.prepareStatement(query);
        id1 = iddis.getText();
        rs = st.executeQuery(query1);
            while(rs.next())
            {
                if(id1 == rs.getString(1))
                {
                    data = 1;
                }
            }
            if(data==1)
            {
                name1 = rs.getString(2);
                mat1 = rs.getString(3);
                eng1 = rs.getString(4);
                sci1 = rs.getString(5);
            }
            else
            {
                JOptionPane.showMessageDialog(null,"Invalid idno");//IT IS ONLY PRINTING THIS
            }
            namedis.setText(name1);
            matdis.setText(mat1);
            engdis.setText(eng1);
            scidis.setText(sci1);
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null,e);
    }
}

1 个答案:

答案 0 :(得分:0)

rs.next循环遍历数据库。所以你可以通过这种方式找到匹配的行。之后,您可以在此query上执行ResultSet,并接收您执行query的行的颜色。