我正在尝试将数据库中的数据显示到数据表中,但没有任何结果。我使用带有精确选择语句的JDBC测试了查询,以查看它是否返回任何行,并且始终有效。但是,当我尝试将数据库中的数据放入数据表时,没有任何结果。我什至只是为了填充数据表而制作了伪造的伪数据。我一定在未知的index.xhtml文件中做错了什么。我该怎么办?任何帮助将不胜感激?
编辑:首先,我使用Primefaces及其数据表示例,然后我使用了像这里一样的简单jsf样式数据表,而当我尝试对数据库进行操作时,这两个都不起作用
UserDAO.java
@ManagedBean(name = "userDAO")
@SessionScoped
public class UserDAO {
private static final String USERNAME = "something";
private static final String PASSWORD = "something";
private static final String CONN_STRING =
"jdbc:sqlserver://petunia.arvixe.com.....something";
public List<Report> getUserList() {
List<Report> list = new ArrayList<Report>();
PreparedStatement ps = null;
Connection con = null;
ResultSet result = null;
try {
con = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
String sql = "SELECT id, tag,asset_name, model, ser_no, value, \n" +
" asset_condition, asset_type FROM assets";
String sql1 = "SELECT name, address, city,state,zip, phone, district FROM location";
ps = con.prepareStatement(sql);
ps = con.prepareStatement(sql1);
result = ps.executeQuery();
while (result.next()) {
Report rep = new Report();
rep.setId(result.getInt("id"));
rep.setTag(result.getString("tag"));
rep.setName(result.getString("asset_name"));
rep.setModel(result.getString("model"));
rep.setSerial(result.getString("ser_no"));
rep.setValue(result.getFloat("value"));
rep.setCondition(result.getString("asset_condition"));
rep.setType(result.getString("asset_type"));
rep.setLocationName(result.getString("name"));
rep.setAddress(result.getString("address"));
rep.setCity(result.getString("city"));
rep.setState(result.getString("state"));
rep.setZip(result.getString("zip"));
rep.setPhone(result.getString("phone"));
rep.setDistrict(result.getInt("district"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
}
嗯,您正在举报:
Report rep = new Report();
...但不将其添加到您的列表中。添加此:
Report rep = new Report();
list.add(rep);
我想你的问题是这段代码:
ps = con.prepareStatement(sql);
ps = con.prepareStatement(sql1);
result = ps.executeQuery();
您正在立即用语句sql
覆盖sql1
语句。
我想,您可能想用THEIRassets
s选择location
。在SQL中,您可以使用JOIN
来实现。
如果使用此语法
String sql = "SELECT id, tag,asset_name, model, ser_no, value, \n" +
" asset_condition, asset_type FROM assets";
String sql1 = "SELECT name, address, city,state,zip, phone, district FROM location";
ps = con.prepareStatement(sql);
ps = con.prepareStatement(sql1);
result = ps.executeQuery();
结果您将只执行第二个查询。我认为您的情况是尝试从表资产和位置两者中获取结果,因此您应该在两个表之间使用联接。