java中的多个select查询 - 不返回任何查询(来自query2)

时间:2017-03-11 08:21:12

标签: java

我在java中有两个方法来执行select查询。当function2没有返回任何值时,query1将执行。但我的问题是query 2,我得到一个空的结果集。如果我在数据库工具中运行相同的查询(query 2),我会得到一些行,但在代码中,它返回null。

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;
    }
}

1 个答案:

答案 0 :(得分:0)

您必须按此顺序关闭<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="modal fade" id="modal1" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h3 class="modal-title text-center" style="color:#333366;"><strong>LOGIN TO FOOD SAVIOR</strong></h3> </div> <div class="modal-body"> <div class="form-group"> <input type="text" placeholder="Username" name="username" class="form-control" style="height:40px;" required> </div> <div class="form-group"> <input type="password" placeholder="Password" name="password" class="form-control" style="height:40px;" required> </div> <div class="text-center"> <input type="submit" value="LOGIN" name="login_btn" id="login_btn" onclick="login()" class="btn btn-primary" style="background-color:#333366;height:40px;color:#ffffff"> </div> <div class="alert alert-danger alert-dismissable fade in"> <button type="button" class="close" data-hide="alert">&times;</button> <p> You Entered Wrong Detail. Enter Again </p> </div> <p id="wrongpassword"></p> <h4 class="text-center"><a href="#">Forget Password</a></h4> </div> <div class="modal-footer"> <h4 class="text-center">Don't have Account yet? Let's <a href="#modal2" data-toggle="modal">Sign up</a> its fun and easy!</h4> <div class="text-center"><button type="button" class="btn btn-success" data-dismiss="modal"><p>Close</p></button> </div> </div> </div> </div> </div> <a href="#modal1" data-toggle="modal"> <h5>Login</h5> </a>ResultSet以及Statement

添加:(try-with-resources仅适用于Java至少版本1.7,并以正确的顺序为您关闭Connection,ResultSet和Statement(即使在try-中有异常)阻止发生!))

Connection

}