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