我正在编写一个Webapp,其中有数据必须从数据库中提取并显示在文本框中。一切都很好。但我需要在No Data Available
这些文本框中显示if(!rs.next())
的值,目前我正在尝试以下代码。
package org.DAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.bean.UserBean;
import org.code.general.DBConnection;
import org.code.general.GetTheUserDetails;
public class GetDataDAO {
DBConnection dbConnection = new DBConnection();
GetTheUserDetails getTheUserDetails = new GetTheUserDetails();
public List<UserBean> list(String systemUserName) throws Exception {
List<UserBean> userBeans = new ArrayList<UserBean>();
try {
Connection conn = dbConnection.getConn();
Statement stmt = dbConnection.getStmt();
ResultSet rs = dbConnection.getRs();
String excelPath = dbConnection.getExcelPath();
String queryString = null;
dbConnection.createClassForNameForExcel();
conn = DriverManager.getConnection(
"jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" + excelPath + "; READONLY=FALSE;");
System.out.println("Connecting to database…");
System.out.println("Oracle JDBC Driver Registered!");
if (conn != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
stmt = conn.createStatement();
queryString = "Select * from [" + dbConnection.getSheetPath()
+ "$] where STATE IS NULL and [Case Owner] = '"
+ getTheUserDetails.getUserNameDetails(systemUserName) + "'";
System.out.println("Query is " + queryString);
rs = stmt.executeQuery(queryString);
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("bno of cols are " + rsmd.getColumnCount());
while (rs.next()) {
if (rs.next()) {
UserBean userBean = new UserBean();
userBean.setCaseNumber(rs.getString(1));
userBean.setCaseOwner(rs.getString(2));
userBean.setStatus(rs.getString(4));
userBean.setIssue(rs.getString(5));
userBean.setReason(rs.getString(6));
userBean.setDateOpened(rs.getString(7));
userBean.setAge(rs.getInt(8));
userBeans.add(userBean);
} else {
UserBean userBean = new UserBean();
userBean.setCaseNumber("None");
userBean.setCaseOwner("None");
userBean.setStatus("None");
userBean.setIssue("None");
userBean.setReason("None");
userBean.setDateOpened("None");
userBean.setAge(Integer.parseInt("None"));
userBeans.add(userBean);
}
}
rs.close();
conn.commit();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return userBeans;
}
}
根据我的理解,我写过尝试以下算法,
if(rs.next)
then create the bean and add the values from database,
else
create the bean and add the values as None
请让我知道我哪里出错了,我该如何解决这个问题。
由于
答案 0 :(得分:1)
if(rs.next)
then create the bean and add the values from database,
else
set the bean as null
在jsp中检查bean是否为空,然后在文本框下为No Data Available
。理想情况下,您应该在servlet或控制器中执行此操作,因为jsp应该尽可能轻便
答案 1 :(得分:1)
每次迭代代码都跳过一行:
while (rs.next()) {
if (rs.next())
如果结果集为0或1行,则只需使用rs.next()
:
if (rs.next())
{
// Use 'rs'.
}
else
{
// "None".
}
此外:
null
的{{1}}检查是多余的,因为DriverManager.getConnection()
如果失败则会引发异常。conn
,Connection
和Statement
。答案 2 :(得分:1)
public boolean next()
将光标移动到下一行。如果结果集中没有更多行,则此方法返回false。
因此,当您拨打rs.next()
两次,一次在while
,一次在if
时,您每次都会错过一行。
尝试删除if
条件
答案 3 :(得分:1)
如果行为空,则必须具有这样的逻辑:
rs = stmt.executeQuery(queryString);
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("bno of cols are " + rsmd.getColumnCount());
while (rs.next()) {
UserBean userBean = new UserBean();
userBean.setCaseNumber(rs.getString(1));
userBean.setCaseOwner(rs.getString(2));
userBean.setStatus(rs.getString(4));
userBean.setIssue(rs.getString(5));
userBean.setReason(rs.getString(6));
userBean.setDateOpened(rs.getString(7));
userBean.setAge(rs.getInt(8));
userBeans.add(userBean);
}
if(userBeans.size() == 0) {
UserBean userBean = new UserBean();
userBean.setCaseNumber("None");
userBean.setCaseOwner("None");
userBean.setStatus("None");
userBean.setIssue("None");
userBean.setReason("None");
userBean.setDateOpened("None");
userBean.setAge(Integer.parseInt("None"));
userBeans.add(userBean);
}
rs.close();
conn.commit();
conn.close();
但是如果行不为空,只包含空值,则可以使用如下逻辑:
rs = stmt.executeQuery(queryString);
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("bno of cols are " + rsmd.getColumnCount());
while (rs.next()) {
UserBean userBean = new UserBean();
String value = rs.getString(1) == null ? "None" : rs.getString(1);
// you do this for all the columns, from 1 to 8
userBean.setCaseNumber(rs.getString(1));
userBean.setCaseOwner(rs.getString(2));
userBean.setStatus(rs.getString(4));
userBean.setIssue(rs.getString(5));
userBean.setReason(rs.getString(6));
userBean.setDateOpened(rs.getString(7));
userBean.setAge(rs.getInt(8));
userBeans.add(userBean);
}
rs.close();
conn.commit();
conn.close();