Resultset的getObject()方法 - 如何正确使用它?

时间:2022-06-02 20:57:13

I make a database query and store Account objects in the ResultSet. Here is the code:

我进行数据库查询并在ResultSet中存储Account对象。这是代码:

try {
    ResultSet rs = queryDatabase();
    int i=0;
    while (rs.next()) {
        Account account= rs.getObject(i, Account); //ERROR
        accounts.add(account);
        i++;
    } 
} catch (Exception e) {
}

This code returns 3 objects and stores them in the rs. Then I want to get those objects in the ResultSet and put them into an ArrayList as you see in the code. But it gives an error in the specified line saying that ; expected. How can I use getObject method properly?

此代码返回3个对象并将它们存储在rs中。然后我想在ResultSet中获取这些对象,并将它们放入ArrayList中,如代码中所示。但它在指定的行中表示错误;预期。如何正确使用getObject方法?

2 个解决方案

#1


3  

ResultSet.getObject (and the other getXxx methods) will retrieve the data from the current row of the ResultSet and starts in index 1. You have set your i variable with 0 value.

ResultSet.getObject(以及其他getXxx方法)将从ResultSet的当前行检索数据并从索引1开始。您已将i变量设置为0值。

Just change this

只是改变这个

int i=0;

To

int i=1;

Also, getObject needs a single param, but you're incorrectly sending two:

此外,getObject需要一个param,但你错误地发送了两个:

Account account= rs.getObject(i, Account);

Probably you were trying to use ResultSet#getObject(int, Class) (available from Java 7), but you have to take into account that your Account class can't be magically converted from a database column to an instance of this object.

您可能正在尝试使用ResultSet#getObject(int,Class)(可从Java 7获得),但您必须考虑到您的Account类无法从数据库列神奇地转换为此对象的实例。

Looks like it would be better to review JDBC trial first, then retry to solve your problem.

看起来最好首先查看JDBC试用版,然后重试以解决您的问题。

Here's another good source to review: Using Customized Type Mappings

这是另一个很好的资源来源:使用自定义类型映射

#2


1  

Our object:

我们的目标:

import java.io.Serializable;
...
class Account implements Serializable{
   public String data;
}

How to get our object from bd:

如何从bd获取我们的对象:

while (rs.next()) {
        Object accountJustObject = rs.getObject(i); 
        Account account = (Account)accountJustObject;
        accounts.add(account);
        i++;
} 

How to save our object:

如何保存我们的对象:

public void InsertAccount(int id, Account newaccount){
 reparedStatement insertNew = conn.prepareStatement(
  "INSERT INTO root(id,account) VALUES (?,?)";
   insertNew.setInt(1, id);             //INT   field type
   insertNew.setObject(2, newaccount);  //OTHER field type
   insertNew.executeUpdate();  
 )
}

Tested under H2 database.

在H2数据库下测试。

#1


3  

ResultSet.getObject (and the other getXxx methods) will retrieve the data from the current row of the ResultSet and starts in index 1. You have set your i variable with 0 value.

ResultSet.getObject(以及其他getXxx方法)将从ResultSet的当前行检索数据并从索引1开始。您已将i变量设置为0值。

Just change this

只是改变这个

int i=0;

To

int i=1;

Also, getObject needs a single param, but you're incorrectly sending two:

此外,getObject需要一个param,但你错误地发送了两个:

Account account= rs.getObject(i, Account);

Probably you were trying to use ResultSet#getObject(int, Class) (available from Java 7), but you have to take into account that your Account class can't be magically converted from a database column to an instance of this object.

您可能正在尝试使用ResultSet#getObject(int,Class)(可从Java 7获得),但您必须考虑到您的Account类无法从数据库列神奇地转换为此对象的实例。

Looks like it would be better to review JDBC trial first, then retry to solve your problem.

看起来最好首先查看JDBC试用版,然后重试以解决您的问题。

Here's another good source to review: Using Customized Type Mappings

这是另一个很好的资源来源:使用自定义类型映射

#2


1  

Our object:

我们的目标:

import java.io.Serializable;
...
class Account implements Serializable{
   public String data;
}

How to get our object from bd:

如何从bd获取我们的对象:

while (rs.next()) {
        Object accountJustObject = rs.getObject(i); 
        Account account = (Account)accountJustObject;
        accounts.add(account);
        i++;
} 

How to save our object:

如何保存我们的对象:

public void InsertAccount(int id, Account newaccount){
 reparedStatement insertNew = conn.prepareStatement(
  "INSERT INTO root(id,account) VALUES (?,?)";
   insertNew.setInt(1, id);             //INT   field type
   insertNew.setObject(2, newaccount);  //OTHER field type
   insertNew.executeUpdate();  
 )
}

Tested under H2 database.

在H2数据库下测试。