简体   繁体   English

SQL查询返回“ oracle.jdbc.driver.OracleResultSetImpl@48f675”而不是我的预期数据

[英]SQL query returning “oracle.jdbc.driver.OracleResultSetImpl@48f675” instead of my expected data

I have successfully connected to my database, but when trying to return SQL query results to my index.jsp page using a Java servlet forward request receive the output of" oracle.jdbc.driver.OracleResultSetImpl@48f675" instead of the expected data results. 我已经成功连接到数据库,但是当尝试使用Java servlet转发请求将SQL查询结果返回到我的index.jsp页面时,接收到“ oracle.jdbc.driver.OracleResultSetImpl@48f675”的输出,而不是预期的数据结果。 ( Note: I am receiving expected results from my SQL query in the console view of Eclipse; however, the data is not shown on my jsp page as I am expecting. Instead the ResultSetImp message is shown. Any insight anyone can share as to why I am getting this ResultSetImp instead of my expected data would be appreciated. Also does anyone care to explain what the ResultSetImp is saying...my searches have not turned up anything useful, or at least that I understand. Code is below: 注意:我从Eclipse的控制台视图中的SQL查询中收到了预期的结果;但是,数据没有按我的预期显示在我的jsp页面上。而是显示ResultSetImp消息。任何人都可以分享有关其原因的见解我得到的是ResultSetImp而不是我期望的数据,也有人愿意解释ResultSetImp在说什么...我的搜索没有发现任何有用的东西,或者至少我不了解,代码如下:

package com.database;
import java.sql.*;


public class DBConnection {

public static void main(String[] args) 
throws ClassNotFoundException, SQLException{

}

    public static ResultSet queryExecute() throws ClassNotFoundException, SQLException
    {

    Class.forName("oracle.jdbc.OracleDriver");  
    Connection conn = DriverManager.getConnection("**Connection string here**");

    if (conn == null)
    {
        System.out.println("Database connection not successfull");
    }
    else 
    {
        System.out.println("Database connection success!");
        //System.out.println(conn);
    }


        Statement stmt = conn.createStatement();
        //ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM Request natural join RequestTerm");
        ResultSet rs = stmt.executeQuery("select * from (select QIPNUMBER,model,SERIALNUMBER,YEAROFMFG,DATERECEIVED,TERM,DEALERNAME from Request natural join RequestTerm)WHERE ROWNUM <= 20");
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnsNumber = rsmd.getColumnCount(); 
        //List<String> displayRecords = new ArrayList<String>(); try store result set into an array object


        while (rs.next()) {
            // Iterate through the data in the result set and display it. 

           //int count = rs.getInt(1);
          while (rs.next()) {
          //Print one row          
          for(int i = 1 ; i <= columnsNumber; i++){

                System.out.print(rs.getString(i) + " " + "    "); //Print one element of a row -- print vs println

          }

            System.out.println(); //Move to the next line to print the next row.  
            //System.out.println("Number of row:"+count);
              }
        }

    rs.close();
    stmt.close();
    conn.close();
    return rs;

    }

} 

My servlet code: 我的servlet代码:

package com.srccodes.example;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.database.DBConnection; 导入com.database.DBConnection;

   /**
    * Servlet implementation class HelloWorld
    */
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
   public static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
  public HelloWorld() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  response.setContentType("text/html");
  PrintWriter printWriter  = response.getWriter();
  printWriter.println("<h1>Hello from the doGet function!</h1>");
}


/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    try {
        //ResultSet display = DBConnection.queryExecute();
        request.setAttribute("display", DBConnection.queryExecute());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    request.getRequestDispatcher("/index.jsp").forward(request, response);

    /*String nextJSP = "/result.jsp";
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
    dispatcher.forward(request,response);*/
}

} }

Finally the statement I use to call my servlet within my index.jsp: 最后,我用来在index.jsp中调用我的servlet的语句:

<%= request.getAttribute("display")%> I know this statement is a scriplet and is frowned upon; <%= request.getAttribute(“ display”)%>我知道此语句是一个摘要,并不为人所知; however, I am trying to get data to populate to my jsp first and then move from there. 但是,我试图先将数据填充到我的jsp中,然后再从那里移动。

When you are returning Object of ResultSet, you will get the reference of the same. 当您返回ResultSet的Object时,您将获得相同的引用。 you wont get the values in it as you desire. 您将无法如愿获得价值。

You will have to do something like this (if you really want to use scriplet): 您将必须执行以下操作(如果您确实要使用Scriplet):

<%
ResultSet rs = (ResultSet)request.getAttribute("display");

ResultSetMetaData rsmd = rs.getMetaData();
        int columnsNumber = rsmd.getColumnCount(); 

        while (rs.next()) {

          for(int i = 1 ; i <= columnsNumber; i++){
                out.print(rs.getString(i) + " " + "    " + "<br/>");
          }

            out.print("<br/>"); //Move to the next line to print the next row.  
            }
        }

    rs.close();
%>

A more appropriate way to this will be to use a bean: 一种更合适的方法是使用bean:

class MyBean {
 int QIPNUMBER;
 String model;
 .....
 //getters and setters


}

now in your class create list of MyBean and populate the objects, one Row = One object. 现在在您的类中创建MyBean列表并填充对象,一个Row =一个对象。

List<MyBean> lst = new ArrayList<MyBean>();
MyBean myBean;
while (rs.next()) {
          myBean = new MyBean();
          myBean.setModel(rs.getString("model"));
          ....
          lst.add(myBean);
}
.....

return lst;

After this add the list to request in Servlet not the resultSet. 此后,在Servlet中添加要请求的列表,而不是resultSet。

request.setAttribute("display", DBConnection.queryExecute());
// queryExecute now returns List<MyBean>

Now in your JSP if you want to use scriplet, then : 现在,如果要使用scriplet,请在JSP中执行以下操作:

<%
   List<MyBean> lst = (ArrayList<MyBean>)request.getAttribute("display");
   for(MyBean mb : lst){
       out.print(mb.getModel() + "<br/>");
       .....
   }
%>

You are passing an instance of a ResultSet back to the JSP: 您正在将ResultSet的实例传递回JSP:

http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

As you are using Oracle, what is returned is the Oracle implementation of the ResultSet interface ie oracle.jdbc.driver.OracleResultSetImpl 使用Oracle时,返回的是ResultSet接口的Oracle实现,即oracle.jdbc.driver.OracleResultSetImpl

Your JSP code is simply calling the toString method of a oracle.jdbc.driver.OracleResultSetImpl object which, because it has not been overridden, is simply invoking the version of toString in the java.lang.Object class - which explains the output: 您的JSP代码只是调用oracle.jdbc.driver.OracleResultSetImpl对象的toString方法,该对象由于尚未被覆盖而只是调用java.lang.Object类中的toString版本-解释了输出:

oracle.jdbc.driver.OracleResultSetImpl@48f675 oracle.jdbc.driver.OracleResultSetImpl@48f675

You need to pull the necessary values out of the ResultSet object and return them accordingly. 您需要从ResultSet对象中拉出必要的值,并相应地返回它们。 See this article on how to do just that: 请参阅这篇文章,了解如何做到这一点:

http://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html http://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM