Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用PreparedStatement创建列表?_Java_Hibernate_Jdbc - Fatal编程技术网

Java 如何使用PreparedStatement创建列表?

Java 如何使用PreparedStatement创建列表?,java,hibernate,jdbc,Java,Hibernate,Jdbc,我有以下代码: -------阶级------------ 私有类SystemHealthAlert实现工作{ 列表系统健康警报列表; 私有字符串查询字符串; //私有java.util.Date startDate; //private java.util.Date endDate; @凌驾 public void execute(连接)抛出SQLException{ PreparedStatement ps=连接.prepareStatement(查询字符串); int指数=1; 结果集rs

我有以下代码:

-------阶级------------

私有类SystemHealthAlert实现工作{
列表系统健康警报列表;
私有字符串查询字符串;
//私有java.util.Date startDate;
//private java.util.Date endDate;
@凌驾
public void execute(连接)抛出SQLException{
PreparedStatement ps=连接.prepareStatement(查询字符串);
int指数=1;
结果集rs=ps.executeQuery();
int columnCount=rs.getMetaData().getColumnCount();
while(rs.next())
{   
//String[]行=新字符串[columnCount];
//结果集(索引、元素);

//为了(int i=0;i定义一个类/bean来保存给定行中的数据。循环遍历您的行,并为您拥有的每行创建该类的一个实例。将这些实例添加到一些列表中。返回这3个实例的列表。

在方法
执行
中,您应该用的实例填充
列表systemHealthAlertList
de>MonitorAlertInstance
。在
while
循环中创建一个新的
MonitorAlertInstance
实例,从中检索数据:

//You don't need this line, remove it
//int columnCount = rs.getMetaData().getColumnCount();
while(rs.next()) {
    //create a new instance of MonitorAlertInstance per ResultSet row
    MonitorAlertInstance monitor = new MonitorAlertInstance();
    //set the fields from the ResultSet in your MonitorAlertInstance fields
    //since I don't know the fields of this class, I would use field1 and field2 as examples
    monitor.setField1(rs.getInt(1));
    monitor.setField2(rs.getString(2));
    //and on...
    systemHealthAlertList.add(monitor);
}

除此问题外,在使用变量之前,还应初始化
列表systemHealthAlertList
变量:

systemHealthAlertList = new ArrayList<MonitorAlertInstance>();
while(rs.next()) {
    //content from previous code...
}
systemHealthAlertList=new ArrayList();
while(rs.next()){
//以前代码中的内容。。。
}

OK,那么OP只需要填充实例并返回包含它们的列表。我需要添加MonitorAlertInstance类中的所有字段吗?@Novis在类中填充必要的字段。如果这意味着要填充每个字段,那么是的,您应该添加所有字段。填充必要的字段是什么意思?我在Monitor中有13个字段ALertInstance类。@Novis我的意思是,如果只从数据库中检索两个字段,则只需在类中填充两个字段。如果从数据库中检索13个字段,则必须填充类中的13个字段。我从DB中获取了两个值,并在while循环中设置了这两个字段。但在li中会出现空指针异常ne:systemHealthAlertList.add(监视器);有什么想法吗?
//You don't need this line, remove it
//int columnCount = rs.getMetaData().getColumnCount();
while(rs.next()) {
    //create a new instance of MonitorAlertInstance per ResultSet row
    MonitorAlertInstance monitor = new MonitorAlertInstance();
    //set the fields from the ResultSet in your MonitorAlertInstance fields
    //since I don't know the fields of this class, I would use field1 and field2 as examples
    monitor.setField1(rs.getInt(1));
    monitor.setField2(rs.getString(2));
    //and on...
    systemHealthAlertList.add(monitor);
}
systemHealthAlertList = new ArrayList<MonitorAlertInstance>();
while(rs.next()) {
    //content from previous code...
}