2017-07-30 15 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()ノートがResultSetのカーソルは最初に最初の行の前に配置され、メソッドの最初の呼び出しは、次の第1の行を現在行になり、2回目の呼び出しは、第二を行います現在の行をローにするなど、)。 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; 
    } 

} 
関連する問題