代码之家  ›  专栏  ›  技术社区  ›  djalell abed

与MySQL连接时,结果集返回空

  •  -1
  • djalell abed  · 技术社区  · 7 年前

    我正试图使用MySQL在数据库中执行一个查询 PreparedStatement ResultSet .

    问题是我在MySQL中得到的结果集是空的,否则我在Derby上得到的结果是正确的。

    Connection con= null;
    ResultSet reslt =null;
    PreparedStatement ps = null;
    try 
    {
        //Class.forName("org.apache.derby.jdbc.ClientDriver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SAIID","SAIID","SAIID"); 
        //con = DriverManager.getConnection("jdbc:derby://localhost:1527/pavillons","saiid","saiid"); 
        String Query =" SELECT * FROM ETUDIANT_PAV WHERE PAVILLONS = ? AND CHAMBRE = ? "
    
        ps = con.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ps.setString(1, "A");
        ps.setString(2, "1");
        reslt = ps.executeQuery();
        //String thequeryresult= reslt.getString("NOM_PRENOM");
        //System.out.println ("this is the query result"+thequeryresult);
        JOptionPane.showMessageDialog(null, "Query Executed");
        //con.close();
    
    
    }
    catch (Exception ex) 
    {
        JOptionPane.showMessageDialog(null, ex.getMessage());
    
    }
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   AxelH    7 年前

    看到这句话:

    reslt = ps.executeQuery();
    //String thequeryresult= reslt.getString("NOM_PRENOM");
    

    为此,请使用 ResultSet.next() true 直到没有更多行可读取。

    reslt = ps.executeQuery(); 
    while(reslt.next()){ //read all lines
        System.out.println(reslt.getString("NOM_PRENOM"));
    }
    
        2
  •  -1
  •   djalell abed    7 年前
    String selctedItemPAV = indextostring(jComboBox1.getSelectedIndex()); 
           String selctedItemCH = jList1.getSelectedValue();
           String Query =" SELECT * FROM etudiant_pav WHERE PAVILLONS = ? AND CHAMBRE = ? " ;
           rst = theSelectQuery(Query,selctedItemPAV ,selctedItemCH); //this is the posted function 
             DefaultListModel listModel = new DefaultListModel();
             jList3.setModel(listModel);
             System.out.println (selctedItemCH + "  doppppppppp  " +selctedItemPAV + jComboBox1.getSelectedIndex());
    
              System.out.println ("doppppppppp222222222");
         if (rst.isBeforeFirst())
         {
              System.out.println ("ttttttttttttttttttttttttttttttttttttttttttttt");    //the excution stops here in application.....
             jList3.setEnabled(true);
             listModel.clear();
             jLabel1.setVisible(false);
            while (rst.next()) 
            {
                 String aff="hhh";
                 aff= rst.getString("NOM_PRENOM");
                 System.out.println ("dooooooooo"+aff);
                listModel.addElement(aff);
             }
            }
         else 
         {
             jLabel1.setVisible(false);
              jList3.setEnabled(false);
            JOptionPane.showMessageDialog(null, "la chambre est vide ");
         }
    
         rst.close();
      }
      catch (Exception ex){
      }