gpt4 book ai didi

java - 准备好的语句似乎在 java servlet 中总是返回 null

转载 作者:行者123 更新时间:2023-11-29 04:49:17 24 4
gpt4 key购买 nike

我正在做一个项目,用户应该在表单中填写他想要的标准,然后根据这些标准,servlet 从数据库返回适当的结果;我正在使用 mysql 服务器。我的问题是我为此使用的准备好的语句(因为用户可以使用可变数量的条件进行搜索)总是返回 null,所以我得到一个

Exception: null java.lang.NullPointerException at    
Servlet_GrowerResults.doGet(Servlet_GrowerResults.java:102)

这是 GetData 类中的 fetchGrowers 方法,它将 ResultSet 返回给 Servlet_GrowerResults

private PreparedStatement stmt_growers=null;
private ResultSet rs_growers;
private final String getGrowersQuery="select * from grower where name like ? and surname like ? and area like ? and partnership like ?;";

public ResultSet fetchGrowers(String criteria_name,String criteria_surname,String criteria_area, String criteria_partnership){
try{
stmt_growers=dbcon.getConnection().prepareStatement(getGrowersQuery);
stmt_growers.setString(1, criteria_name);
stmt_growers.setString(3, criteria_surname);
stmt_growers.setString(5, criteria_area);
stmt_growers.setString(7, criteria_partnership);

rs_growers=stmt_growers.executeQuery();
return rs_growers;
}
catch(Exception e9){
errorMessage = "Error while getting all growers from database!<br>" + e9.getMessage();
return null;
}

这是初始化 ResultSet 的行。还有其他 else if 来覆盖所有 criteria 提交的情况

   try{
getData.getDBcon().open();
ResultSet rsAreas=getData.fetchGrowerAreas();
ResultSet rsPartnership=getData.fetchGrowerPartnership();
ResultSet rsGrowersReturned;

String sqlWildcard="%";

//Cases of criteria submited by the user,
//in order to send the apropriate parameters
//to the PreparedStatement

//1st case: searching without names criteria
if( (criteriaNames.length()>0) && (!(criteriaAreas.length()>0)) && (!(criteriaPartnerships.length()>0)) ){
rsGrowersReturned=getData.fetchGrowers(sqlWildcard,sqlWildcard,criteriaAreas,criteriaPartnerships);
}

这是异常指向的行。更具体地说,第 102 行是:while(rsGrowersReturned.next())

 if((sorting.equals("περιοχή"))){
while(rsAreas.next()){
out.println("<table>");
String area=rsAreas.getString("area");
area = new String(area.getBytes("ISO-8859-1"), "ISO-8859-7");
out.println("<tr><th>"+rsAreas.getString("area")+"</th></tr>");

/*line 102*/ while(rsGrowersReturned.next()){
if(rsGrowersReturned.getString("area").equals(rsAreas.getString("area"))){
String name=rsGrowersReturned.getString("name");
name = new String(name.getBytes("ISO-8859-1"), "ISO-8859-7");
String surname=rsGrowersReturned.getString("surname");
surname = new String(surname.getBytes("ISO-8859-1"), "ISO-8859-7");
String partnership=rsGrowersReturned.getString("partnership");
partnership = new String(partnership.getBytes("ISO-8859-1"), "ISO-8859-7");

out.println("<tr><td>"+name+" "+surname+"</td>");
out.println("<td>"+partnership+"</td></tr>");
}//end of neaced if
}//end of second while
}//end of first while
out.println("</table>");
}//end of if

我可以看到确实出现了问题,因为在到达第二个区域之前,它会按预期打印第一个区域,然后出现异常!!!

我尝试了直接对mysql服务器进行sql查询,是可以的,就是找不到问题所在!每一个答案将不胜感激,谢谢。

最佳答案

您需要按顺序为您的 PrepareStatement 设置输入参数:

stmt_growers.setString(1, criteria_name);
stmt_growers.setString(2, criteria_surname);
stmt_growers.setString(3, criteria_area);
stmt_growers.setString(4, criteria_partnership);

关于java - 准备好的语句似乎在 java servlet 中总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13788864/

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