2009-11-15 47 views
1

我只是想知道是否有办法让Java方法返回多个值。如何让Java方法返回多个值?

我正在创建一个使用jdbc库来处理数据库的应用程序。我可以成功地将值输入数据库,但我需要一种方法来返回它们,这就是我有点卡住的地方。我创建了一个表单,用户输入一个特定的值(一个ID号),然后由数据库类传递给我,这个数据库类执行我的数据库工作。

Database newQuery = new Database();  
newQuery.getCust(c_ID);   //uses the GetCust method in my class, 
           //passing it the ID of the customer. 

在我的数据库类getCust()方法创建以下查询:

ResultSet Customer = stat.executeQuery("SELECT * FROM Persons WHERE Cust_ID=C_ID"); 

我需要一种方法来恢复存储在客户回结果。有任何想法吗?

+0

可能重复的[如何从Java方法返回多个对象?](http://stackoverflow.com/questions/457629/how-to-return-multiple-objects-from-a-java-method) – ripper234 2012-01-04 11:26:00

回答

7

为什么不直接返回Customer,或者创建一个包含您想返回的所有值的小类并返回该类?

+3

因为语言不需要妨碍你的代码,尤其是“更高级别”的语言。 – Mikle 2011-06-02 22:25:54

7

不能准确地从Java中的某个方法返回多个值,但始终可以返回一个容纳多个值的容器对象。在你的情况下,最简单的事情就是返回ResultSet,Customer。

如果您担心将数据层暴露给用户界面,可以将数据从ResultSet复制到一个不太特定于数据库的结构中,可以是地图列表,也可以是客户对象列表,其中Custom是代表您业务实体的新类。

+1

返回ResultSet似乎是一个简单的解决方案,但它有点问题。因为ResultSet应该(即必须)在使用后关闭,但是谁负责?调用函数?当PreparedStatement发挥作用时它变得更糟,因为那个应该被关闭,但是这样做也会关闭ResultSet ... – 2009-11-15 12:49:26

0

每位客户都可能会附带一个介绍一种采用多个参数的方法的小界面。然后传递实现此接口的对象以将结果传递给它。

实现它的对象当然可以与调用getCustomer所属的方法相同,所以它只是传递对'this'的引用并将参数分配给字段,您可以期望在所有字段中设置它们回报。

0

考虑使用对象/关系映射库。它将处理将您需要从JDBC ResultSet返回到单个Java bean对象中的多个数据值打包的细节。

选择哪一个是另一个讨论。很多聪明人使用Hibernate。 Java平台包括JPA。使用现成的产品可以帮助您避免发明自己的产品,这就是设计自己的对象和产品组合的最终目的。

3

所以你的实际问题是,你不知道如何设置SQL查询中的值/参数?唯一正确的方法是使用PreparedStatement

String sql = "select * from Customers where Cust_ID = ?"; 
preparedStatement = connection.prepareStatement(sql); 
preparedStatement.setLong(custId); 
resultSet = preparedStatement.executeQuery(); 

它不仅简化了设置在一个SQL查询Java对象(StringLongIntegerDateInputStream等),但大多数importantingly它可以使你免于SQL Injectionrisks。此外,它也比预编译的Statement快。

至于您的代码逻辑,您应该始终在finally区块中按相反顺序关闭DB资源,以避免发生异常时的资源泄漏。这里有一个基本的例子,如何获得Customer正确的JDBC方式:

public Customer find(Long customerId) throws SQLException { 
    String sql = "SELECT id, name, age FROM customer WHERE id = ?"; 
    Connection connection = null; 
    PreparedStatement preparedStatement = null; 
    ResultSet resultSet = null; 
    Customer customer = null; 

    try { 
     connection = getConnectionSomehow(); 
     preparedStatement = connection.prepareStatement(sql); 
     preparedStatement.setLong(custId); 
     resultSet = preparedStatement.executeQuery(); 
     if (resultSet.next()) { 
      customer = new Customer(); 
      customer.setId(resultSet.getLong("id")); 
      customer.setName(resultSet.getString("name")); 
      customer.setAge(resultSet.getInteger("age")); 
     } 
    } finally { 
     if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {} 
     if (preparedStatement != null) try { preparedStatement.close(); } catch (SQLException ignore) {} 
     if (connection != null) try { connection.close(); } catch (SQLException ignore) {} 
    } 

    return customer; 
} 

您可能会发现this tutorial有用的,以获得更多的见解和实例。

0

除了使用Hibernate - 看看Spring。它支持连接池等,并允许您完全从代码中提取JDBC。

它会返回给您一个地图列表或您自定义类型的列表(取决于您如何调用它)。