JDBC结果集相关

时间:2017-01-28 11:01:22

标签: java database jdbc resultset

我在JDBC中使用了这段代码(在NetBeans IDE 8.1中使用过),它不会从第二个结果集中提取数据。尝试过几个答案,但到目前为止还没有解决方案........

我写的代码粘贴在下面:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    Connection conn= null;
    Statement stmt1= null;
    Statement stmt2= null;
    try (PrintWriter out = response.getWriter()) {
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head><title>Servlet test_servlet</title>");
        out.println("<link href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\" rel=\"stylesheet\">");
        out.println("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js\"></script>");
        out.println("<script src=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script>");
        out.println("</head><body>");
        out.println("<div class=\"container-fluid\">");

        //Register Driver
        out.println("<h5>Trying to register</h5>");
        Class.forName("oracle.jdbc.driver.OracleDriver");
        out.println("<h5>Registered successfully</h5>");

        //Open a connection
        out.println("<h5>Trying to connect to the XE database...</h5>");
        conn=DriverManager.getConnection("jdbc:oracle:thin:@Toshiba:1521:xe","SYSTEM","oraclee");
        out.println("<h5>Connected Successfully</h5>");

        //Executing queries
        stmt1=conn.createStatement();
        stmt2=conn.createStatement();

        String [] subj=new String[5];
        int i=0;
        ResultSet rst1=stmt1.executeQuery("select * from TESTINGTABLE");
        while(rst1.next()){
            //int numb=rst.getInt(1);
            subj[i]=rst1.getString("E_NAME").toString();
            out.println("Hello "+subj[i]);
            out.println("<br>");
            i++;
        }
        rst1.close();

        ResultSet rst2=stmt2.executeQuery("select * from TESTTABLE");
        while(rst2.next()){
            String antt=rst2.getString(1).toString();
            out.println("Hi "+antt+"<br>");
        }
        rst2.close();
        out.println("<h5>Result set object closed</h5>");
        stmt2.close();
        stmt1.close();
        out.println("<h5>Statement object closed</h5>");
        conn.close();
        out.println("<h5>Connection object closed</h5>");
        out.println("</div>");
        out.println("</body>");
        out.println("</html>");
    }catch(SQLException se){
        se.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
    }
}

它为我提供的输出是:

Trying to register
Registered successfully
Trying to connect to the XE database...
Connected Successfully
Hello Joe
Hello Chris
Hello James
Hello Eve
Hello Thomas

它不显示第二个结果集中的数据。

我在这个程序中使用的两个表格如下:

1&GT; TESTINGTABLE只有一个列(E_NAME),其中包含5个值 乔,克里斯,詹姆斯,夏娃,托马斯

2 - ; TESTTABLE只有一列(E_NAME),其中包含1个值 玛丽亚姆

1 个答案:

答案 0 :(得分:2)

在这一行:

ResultSet rst2=stmt2.executeQuery("select * from TESTTABLE'");
                                                          ^ -- here

尝试删除SQL查询的最后一个',如此

ResultSet rst2=stmt2.executeQuery("select * from TESTTABLE");