2017-03-11 44 views
0

我在Java兩種方法來執行SELECT查詢。當query1不返回任何值時將執行function2。但我的問題是query 2,我得到一個空的結果集。如果我在運行數據庫工具相同的查詢(query 2),我得到一些行,但在代碼中,則返回null。在java中不止一個選擇查詢 - 不返回任何(來自QUERY2)

public class FetchData2 { 

    private static Connection connection = null; 


    public static Connection getConnection() { 

     System.out.println("initial connection "+connection); 

     if (connection != null) 
      return connection; 
     else { 
      // DB Connection 
      try 
      { System.out.println("i am inside connection");    
       Class.forName("oracle.jdbc.driver.OracleDriver"); 
       connection = DriverManager.getConnection("connection"); 
       System.out.println("Connection estabilished"+connection); 
      }catch (Exception e){ 
       System.out.println("Connection error1: "+e); 
      } 
      return connection; 
     } 
    } 

    public static ArrayList<Elements2> getAllElements2(String sino,String irepno) {  
     connection = FetchData2.getConnection(); 
     ArrayList<Elements2> itemListinsp = new ArrayList<Elements2>();  

     try { 
      Statement statement1=connection.createStatement(); 
      ResultSet rs = statement1.executeQuery("query1"); 


      while(rs.next()) { 
       Elements2 iteminsp=new Elements2(); 
       iteminsp.setParameters(rs.getString("parameters")); 
       iteminsp.setSpecifications(rs.getString("specifications")); 
       iteminsp.setActual1(rs.getString("actual1")); 
       iteminsp.setActual2(rs.getString("actual2")); 
       iteminsp.setActual3(rs.getString("actual3")); 
       iteminsp.setActual4(rs.getString("actual4")); 
       iteminsp.setActual5(rs.getString("actual5")); 
       itemListinsp.add(iteminsp); 
      } 

     con.close(); // added these lines 
     rs.close(); 
     ps.close();   
     } catch (Exception e) { 
      System.out.println("Connection query exection error: "+e); 
     } 

     return itemListinsp; 
    } 

    public static ArrayList<Elements2> getcritElements2(String icode) { 
     connection = FetchData2.getConnection(); 
     System.out.println("I AM IN SECOND FETCH"); 
     ArrayList<Elements2> listcrit = new ArrayList<Elements2>(); 

     System.out.println("incoming icode: "+icode); 

     try { 
      Statement statementinsp=connection.createStatement(); 
      ResultSet rsinsp = statementinsp.executeQuery("query2"); 

      if (!rsinsp.next()) { 
       System.out.println("no data"); 
      } 

      while(rsinsp.next()) { 
       Elements2 inspcrit=new Elements2(); 
       inspcrit.setParameters(rsinsp.getString("parameters")); 
       inspcrit.setSpecifications(rsinsp.getString("specifications")); 
       listcrit.add(inspcrit); 
      }    
     } catch (Exception e) { 
      System.out.println("Connection query exection error: "+e); 
     } 
     System.out.println(listcrit.size()); 
     System.out.println(listcrit.get(0).getParameters()); 
     System.out.println(listcrit.get(0).getSpecifications()); 
     return listcrit; 
    } 
} 
+0

歡迎堆棧溢出!我會想象這95%的代碼與你的問題無關。請創建一個[**最小**,完整且可驗證的示例](http://stackoverflow.com/help/mcve),以說明您的問題。 –

回答

0

您必須關閉ResultSet和你Statement並依次您Connection

增加:(試用資源只適用於Java至少1.7版本,並以正確的順序關閉Connection,ResultSet和Statement(即使try-block內出現異常!) )

public class FetchData2 { 

    public static Connection getConnection() { 

     System.out.println("initial connection "+connection); 

     // DB Connection 
     Connection connection = null; 
     try { 
      System.out.println("i am inside connection");    
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      connection = DriverManager.getConnection("connection"); 
      System.out.println("Connection estabilished"+connection); 
     } catch (Exception e) { 
      System.out.println("Connection error1: "+e); 
     } 
     return connection; 
    } 
} 

public static ArrayList<Elements2> getAllElements2 (String sino,String irepno) {   
    ArrayList<Elements2> itemListinsp = new ArrayList<Elements2>();  

    try (Connection connection = FetchData2.getConnection(); 
     Statement statement1 = connection.createStatement(); 
     ResultSet rs = statement1.executeQuery("query1")) { 

     while(rs.next()) { 
      Elements2 iteminsp = new Elements2(); 
      iteminsp.setParameters(rs.getString("parameters")); 
      iteminsp.setSpecifications(rs.getString("specifications")); 
      iteminsp.setActual1(rs.getString("actual1")); 
      iteminsp.setActual2(rs.getString("actual2")); 
      iteminsp.setActual3(rs.getString("actual3")); 
      iteminsp.setActual4(rs.getString("actual4")); 
      iteminsp.setActual5(rs.getString("actual5")); 
      itemListinsp.add(iteminsp); 
     }   
    } catch (Exception e) { 
     System.out.println("Connection query exection error: "+e); 
    } 
    return itemListinsp; 
} 

public static ArrayList<Elements2> getcritElements2(String icode) { 
    System.out.println("I AM IN SECOND FETCH"); 
    ArrayList<Elements2> listcrit = new ArrayList<Elements2>(); 

    System.out.println("incoming icode: "+icode); 

    try (Connection connection = FetchData2.getConnection(); 
     Statement statementinsp = connection.createStatement(); 
     ResultSet rsinsp = statementinsp.executeQuery("query2")) { 

     if (!rsinsp.isBeforeFirst()) { 
      System.out.println("no data"); 
     } 

     while(rsinsp.next()) { 
      Elements2 inspcrit=new Elements2(); 
      inspcrit.setParameters(rsinsp.getString("parameters")); 
      inspcrit.setSpecifications(rsinsp.getString("specifications")); 
      listcrit.add(inspcrit); 
     }    
    } catch (Exception e) { 
     System.out.println("Connection query exection error: "+e); 
    } 
    System.out.println(listcrit.size()); 
    System.out.println(listcrit.get(0).getParameters()); 
    System.out.println(listcrit.get(0).getSpecifications()); 
    return listcrit; 
} 

}

+0

我已經添加了一個代碼來關閉所有參數。我仍然無效。請檢查我的更新的代碼,幫我找出 – Elei

+0

什麼是'ps.close'?您必須按照正確的順序關閉這三件事 - 首先是ResultSet,然後是Statement,最後是Connection。但是在FetchData2中,您返回舊的關閉連接 - 如果關閉連接,則不起作用。所以你總是必須返回一個新的連接! –

+0

非常感謝您寶貴的解決方案。 :)有用 – Elei