gpt4 book ai didi

java - 从属性中获取 Null 值

转载 作者:行者123 更新时间:2023-12-01 15:01:15 25 4
gpt4 key购买 nike

在动态网络应用程序中,我遇到了一些问题,因此我发布了我的代码:

这是我的 Controller (servlet):

        String select = request.getParameter("select");  // getting proper value
String search = request.getParameter("search"); // getting proper value
request.setAttribute("select", select);
request.setAttribute("search", search);
System.out.println("Select : "+select+" Search : "+search);

int page = 1;
int recordsPerPage = 20;
if(request.getParameter("page") != null)
page = Integer.parseInt(request.getParameter("page"));
SearchDAO searchDAO=new SearchDAO();
List<User> list=searchDAO.searchAllUsers((page-1)*recordsPerPage,recordsPerPage,select,search);
int noOfRecords = searchDAO.getNoOfRecords();
System.out.println("4> NoOfRecords : "+noOfRecords);
int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
System.out.println("5> NoOfPages : "+noOfPages);
request.setAttribute("searchList", list);
System.out.println("6> List : "+list);
request.setAttribute("noOfPages", noOfPages); // Getting null value, 0
request.setAttribute("currentPage", page); // Getting null value, 0
RequestDispatcher view = request.getRequestDispatcher("/jsp/Securex_Anti_Theft_SearchUserList.jsp");
view.forward(request, response);

这是我的 DAO(简单的 java 类):

public class SearchDAO {
private int noOfRecords;
Connection connection;
Statement stmt;

public List<User> searchAllUsers(int offset ,int noOfRecords,String select,String search){
private static Connection getConnection() throws SQLException,ClassNotFoundException{
Connection con = ConnectionFactory.getInstance().getConnection();
return con; //ConnectionFactory is class for making the connection to DB.
}
String query="select SQL_CALC_FOUND_ROWS * from info where '"+select+
"' like '%"+search+"%' order by serialNo asc limit
" + offset + " , " + noOfRecords;

List<User> list1 = new ArrayList<User>();
User user1=null;

try {
connection = getConnection();
stmt = connection.createStatement();
ResultSet rs=stmt.executeQuery(query);
System.out.println("1> :"+rs);
while(rs.next()){
user1=new User();
user1.setSerial(rs.getInt(1));
System.out.println("I'm inside a loop");
user1.setName(rs.getString(2));
user1.setEmail(rs.getString(3));
list1.add(user1);
}

rs.close();
rs = stmt.executeQuery("SELECT FOUND_ROWS()");
System.out.println("2> :" +rs);
if(rs.next())
this.noOfRecords = rs.getInt(1);
System.out.println("3> :" +this.noOfRecords);

} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally
{
try {
if(stmt != null)
stmt.close();
if(connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

return list1;

}

public int getNoOfRecords() {
return noOfRecords;
}
}

输出是:

1> :com.mysql.jdbc.JDBC4ResultSet@1af8502
2> :com.mysql.jdbc.JDBC4ResultSet@455aa8
3> :0
4> NoOfRecords : 0
5> NoOfPages : 0
6> List : []

我有相同的 servlet 和类来选择所有用户,并且工作正常,但从这里获取空值。即使 ResultSet 返回值,即 com.mysql.jdbc.JDBC4ResultSet@88d319,但无法从 DAO 获取值servlet。

//属性中获取空值,对我来说一切都应该很好,但仍然得到空值,请随时指出我的错误,如果我做了什么

最佳答案

您的 DAO 很可能返回空列表。所有其他代码看起来都很好。所以,首先我会检查这个调用

searchDAO.searchAllUsers((page-1)*recordsPerPage,recordsPerPage,select,search);

page 变量的值是多少?其他参数?请在以下时间后记录查询:

 String query="select SQL_CALC_FOUND_ROWS * from info where '"+select+
"' like '%"+search+"%' order by serialNo asc limit
" + offset + " , " + noOfRecords;

并针对数据库单独运行它,它应该会给你一个答案。

关于java - 从属性中获取 Null 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13606815/

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