2012-12-28 111 views
1

我有一個代碼,它將從公司表中讀取行並插入到JComboBox中。運行java代碼時無法從mysql數據庫檢索行

當APP在調試模式下運行時,結果集將填充數據。但是在正常執行時,結果集爲空

我正在使用Netbeans IDE 7.0.1進行開發,而phpmyadmin mysql數據庫版本爲5.1.37

下面是我的代碼:

 boolean isvalue = false; // variable to identify if the company name found or not. 
     ResultSet rs = null; 
     try { 
      st = con.createStatement(); 
      if(con == null) { 
       logger.error("Database Connection Not available."); 
       throw new NullPointerException(); 
      } 
      //Set the company name to combo box 
      rs = st.executeQuery("Select comp_name from company"); 
      while (rs.next()) { 
       comboCompanyName.addItem(rs.getString("comp_name")); 
       isvalue = true; //Set true if found 
      } 
     } catch (SQLException ex) { 
      System.out.println("SQLError found while updating information." + ex.getMessage()); 
     } catch (Exception ex) { 
      System.out.println("Error found while updating information." + ex.getMessage()); 
     } 
     if (!isvalue) //Check company information available 
     { 
      JOptionPane.showMessageDialog(null, "System could not found any company information.", "Error", JOptionPane.WARNING_MESSAGE); 
     } 

幫我出這一點。 在此先感謝。

+0

'comboCompanyName.addItem(rs.getString(「comp_name」));'comboCompanyName'後面的任何其他代碼? –

+0

您的空檢查是在您訪問該變量之後,因此您不會記​​錄任何內容。 – unholysampler

+0

信息'找不到數據'來自哪裏?我無法在您的代碼中找到它。 – jlordo

回答

0

我已經找到了問題。

由於連接問題沒有正確關閉。即以前的連接沒有正確關閉,我也使用了靜態連接對象。

0

Instade使用則isValue變量,你可以試試:

PreparedStatement statement = con.prepareStatement("SELECT comp_name FROM company"); 
    ResultSet result = statement.executeQuery(); 
    int i=0; 
    while (result.next()) {       
     jComboBox1.addItem(result.getString(1)); 
     i++; 
     } 
    if(i==0){ 
    JOptionPane.showMessageDialog(null, "System could not found any company information.", "Error",JOptionPane.WARNING_MESSAGE); 
    } 
+0

如何幫助? –

+0

@Rajshri嘗試但我無法得到任何輸出。 – Dhinakar

+0

什麼是錯誤? – Rajshri