2017-07-30 26 views
0

這是使用PrepareStatement從數據庫進行的簡單SELECT查詢,但查詢不返回任何結果。我嘗試沒有它也不工作。使用PrepareStatment的Java SQL SELECT查詢

try { 
    // Class.forName("org.apache.derby.jdbc.ClientDriver"); 
    con = DriverManager.getConnection("jdbc:derby://localhost:1527/pavillons","saiid","saiid"); 
    Query =" SELECT NOM_PRENOM FROM SAIID.ETUDIANT_PAV WHERE PAVILLONS = ? AND CHAMBRE = ? " ; 
    ps = con.prepareStatement(query); 
    ps.setString(1, pav); 
    ps.setString(2, ch); 

    reslt = ps.executeQuery(); 
    afuf= reslt.getString("NOM_PRENOM"); 
    System.out.println ("dooooooooo"+afuf); 
    JOptionPane.showMessageDialog(null, "Query Executed"); 
    return reslt;  
} catch (Exception ex) { 
    JOptionPane.showMessageDialog(null, ex.getMessage()); 
    return reslt; 
} 
+0

什麼是 「不工作」 的呼叫是什麼意思? –

+0

我沒有得到我的結果auery –

回答

2

的第一行之前的ResultSet開始(ResultSet.next()筆記一個結果光標最初位於第一行之前;該方法的第一呼叫下使第一行的當前行;第二呼叫使第二排當前行,等等)。加入rs.next()

if (reslt.next()) { 
    afuf= reslt.getString("NOM_PRENOM"); 
} 
+0

謝謝我以前遇到這個問題,我修好了,這是一個函數的一部分,我在另一個函數中使用reslt.next(),但仍然是同樣的結果沒有結果 –

0
try 
    { 
     ResultSet Rst = null; 
     String selctedItemPAV = jComboBox1.getSelectedItem().toString(); 
     String selctedItemCH = jList1.getSelectedValue().toString(); 
     final String Query =" SELECT NOM_PRENOM FROM SAIID.ETUDIANT_PAV WHERE PAVILLONS = ? AND CHAMBRE = ? " ; 
     Rst = theSelectQuery(Query,selctedItemPAV ,selctedItemCH); 
     DefaultListModel listModel = new DefaultListModel(); 
     jList3.setModel(listModel); 
     System.out.println ("doppppppppp"); 
     listModel.clear(); 
     System.out.println ("doppppppppp222222222"); 
    while (Rst.next()) 
    { 
      String aff="hhh"; 
      aff= Rst.getString("NOM_PRENOM"); 
      System.out.println ("dooooooooo"+aff); 
     listModel.addElement(aff); 
     } 
    } 
    catch (Exception ex){ 
    } 
public ResultSet theSelectQuery (String query , String pav, String ch) throws SQLException 
{ 
    Connection con= null; 
    Statement st= null; 
    ResultSet reslt =null; 
    PreparedStatement ps = null; 
    try 
    { 
     //Class.forName("org.apache.derby.jdbc.ClientDriver"); 
     con = DriverManager.getConnection("jdbc:derby://localhost:1527/pavillons","saiid","saiid"); 
     //st = con.createStatement(); 
     //reslt = st.executeQuery(query); 
     ps = con.prepareStatement(query); 
     ps.setString(1, pav); 
     ps.setString(2, ch); 
     String afuf="hhh"; 
     reslt = ps.executeQuery(); 
     afuf= reslt.getString("NOM_PRENOM"); 
     //if (reslt == null) System.out.println(query); 
     System.out.println ("dooooooooo"+afuf); 
     JOptionPane.showMessageDialog(null, "Query Executed"); 
     return reslt; 

    } 
    catch (Exception ex) 
    { 
    JOptionPane.showMessageDialog(null, ex.getMessage()); 
    return reslt; 
    } 

}