gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:41:40 26 4
gpt4 key购买 nike

我已成功连接到数据库,但是当尝试使用 Java servlet 转发请求将 SQL 查询结果返回到我的 index.jsp 页面时,收到“oracle.jdbc.driver.OracleResultSetImpl@48f675”的输出,而不是预期的数据结果。 (注意:我在 Eclipse 的控制台 View 中收到来自 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;

}

}

我的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;

导入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);*/
}

}

最后,我用来在 index.jsp 中调用 servlet 的语句:

<%= request.getAttribute("display")%>我知道这种说法是潦草的,会让人皱眉;但是,我试图先将数据填充到我的 jsp 中,然后从那里移动。

最佳答案

当您返回 ResultSet 对象时,您将获得该对象的引用。您不会如您所愿地获得其中的值。

你必须做这样的事情(如果你真的想使用 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();
%>

更合适的方法是使用 bean:

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


}

现在在您的类中创建 MyBean 列表并填充对象,一行 = 一个对象。

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

return lst;

在此之后添加要在 Servlet 中请求的列表,而不是结果集。

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

现在在您的 JSP 中,如果您想使用 scriplet,那么:

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

关于java - SQL查询返回 "oracle.jdbc.driver.OracleResultSetImpl@48f675"而不是我预期的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24612224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com