java如何返回多个值,如何让Java方法返回多个值?

博主在使用jdbc库操作数据库时,遇到Java方法返回多个值的问题。在通过表单获取用户输入的ID并查询数据库后,不知如何返回查询结果。解决方案是返回ResultSet,若担心数据层暴露,可将数据复制到List of Maps或List of Customer对象中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I was just wondering whether there's a way to make a Java method return multiple values.

I'm creating an application that uses the jdbc library to work with a database. I can successfully enter values into the database but I need a way to return them, and this is where I'm a bit stuck. I creating a form into which the user enters a specific value (an ID number) which is then passed to by Database class which carries out my database work.

Database newQuery = new Database();

newQuery.getCust(c_ID); //uses the GetCust method in my class,

//passing it the ID of the customer.

The getCust() method in my Database class creates the following query:

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

I need a way to return the results that are stored in Customer back. Any ideas?

解决方案

You can't exactly return multiple values from a method in Java, but you can always return a container object that holds several values. In your case, the easiest thing to do would be to return the ResultSet, Customer.

If you're concerned about exposing your data layer to your UI, you can copy the data from the ResultSet into a structure that is less specific to the database, either a List of Maps, or perhaps a List of Customer objects, where Custom is a new class that represents your business entity.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值