gpt4 book ai didi

java - 从 mysql 选择数据但过程抛出 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 20:59:19 25 4
gpt4 key购买 nike

我正在尝试从 mysql 中选择一些行并将其放入 ArrayList 中在java类中

这是查询:

"SELECT user_id,book_id,preference FROM rating LIMIT %d OFFSET %d"

我尝试在将数据存储到 ArrayList 之前验证数据

ratesDATA bk = new ratesDATA();
bk.setUser_id(rs.getLong("user_id"));
bk.setBook_id(rs.getLong("book_id"));
bk.setPref(rs.getDouble("preference"));
System.out.println("TTTTNNNN"+rs.getLong("user_id")+"/"+rs.getLong("book_id")+"/"+rs.getDouble("preference"));

list.add(bk);

这是我在 GlassFish Server 日志中发现的内容:(我使用 servlet 将行数限制为 10 行)

-------- 10 正确行然后异常 ------

Infos:   TTTTNNNN1/0/4.0
Infos: TTTTNNNN1/3/3.0
Infos: TTTTNNNN1/4/3.0
Infos: TTTTNNNN1/5/1.0
Infos: TTTTNNNN1/6/4.0
Infos: TTTTNNNN1/8/2.0
Infos: TTTTNNNN2/0/3.0
Infos: TTTTNNNN2/1/3.0
Infos: TTTTNNNN2/4/2.0
Infos: TTTTNNNN2/7/1.0
java.lang.NullPointerException
at com.vaannila.ratesDATA.loadList(ratesDATA.java:134)
at com.servlet.nextRatesPage.doGet(nextRatesPage.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)

这是程序的完整代码:

public void loadList(int firstrow,int rowcount) 
{

com.mysql.jdbc.PreparedStatement ps = null;



list= new ArrayList<ratesDATA>();
ResultSet rs = null;

String url="jdbc:mysql://127.0.0.1:3306/pfadb";
String name="root";
String pw="root";
String driver="com.mysql.jdbc.Driver";


Connection connexion=null;


try
{
Class.forName(driver).newInstance();
connexion=DriverManager.getConnection(url,name,pw);
String SQL_SUBLIST = "SELECT user_id,book_id,preference FROM rating LIMIT %d OFFSET %d";

//String q = String.format(SQL_SUBLIST, firstrow, rowcount);
String q = String.format(SQL_SUBLIST, rowcount, firstrow);

Statement commande=connexion.createStatement();
rs=commande.executeQuery(q);
while (rs.next())
{
ratesDATA bk = new ratesDATA();
bk.setUser_id(rs.getLong("user_id"));
bk.setBook_id(rs.getLong("book_id"));
bk.setPref(rs.getDouble("preference"));
System.out.println("TTTTNNNN"+rs.getLong("user_id")+"/"+rs.getLong("book_id")+"/"+rs.getDouble("preference"));

list.add(bk);

}

}

catch(Exception e)
{
e.printStackTrace();

}
finally
{
try
{
connexion.close();
ps.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}








}

这是 servlet 的完整代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int page = 1;
try{
int recordsPerPage = 10;
if(request.getParameter("page") != null)
page = Integer.parseInt(request.getParameter("page"));
ratesDATA dao = new ratesDATA();
dao.loadList((page-1)*recordsPerPage,recordsPerPage);

List<ratesDATA> list = dao.getList();



int noOfRecords = dao.getNoOfRecords();
int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);

request.setAttribute("rateslist", list);
request.setAttribute("noOfPages", noOfPages);
request.setAttribute("currentPage", page);
RequestDispatcher view = request.getRequestDispatcher("adminRates.jsp");
view.forward(request, response);


} catch (Exception ex) {
System.out.println("TTTTTTTTTTTTTTTT"+ex.getMessage());

}
}

最佳答案

在关闭连接之前关闭语句。

<小时/>

ps指的是什么?

    ps.close();

关于java - 从 mysql 选择数据但过程抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37368941/

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