How can I get the Java method to return multiple values?

I'm just wondering if there is a way to get the Java method to return multiple values.

I am 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 that is where I got a little stuck. I create a form in which the user enters a specific value (identification number), which is then passed to the Database class, which does my job with the database.

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?

+1
java jdbc
source share
6 answers

Why not just return the Client or create a small class with all the values โ€‹โ€‹you want to return in it and return this class?

+7
source share

You cannot exactly return multiple values โ€‹โ€‹from a method in Java, but you can always return a container object that contains multiple values. In your case, the easiest way would be to return ResultSet, Customer.

If you are interested in exposing your data layer to your user interface, 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, which represents your business object.

+7
source share

So, your actual problem is that you did not know how to set the values โ€‹โ€‹/ parameters in the SQL query? The only right way to do this is PreparedStatement .

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

It not only simplifies the installation of Java objects ( String , Long , Integer , Date , InputStream , etc.) in the SQL query, but, most importantly, it will save you from SQL Injection risks . Further, it is also faster than a Statement , because it is precompiled.

As for your code logic, you should always close database resources in the reverse order in the finally block to avoid resource leaks in case of exceptions. Here is a basic example of how to get Customer right JDBC path:

 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; } 

You can find this tutorial to get more ideas and examples.

+3
source share

Each client can be accompanied by a small interface that describes a method that takes several arguments. Then you pass in an object that implements this interface to get the result.

The object that implements it can, of course, be the same as the method that calls getCustomer , so it simply passes a reference to 'this' and assigns arguments to the fields that you would expect when they were set; everything is returned.

0
source share

Consider using a library of objects / relational mappings. It will handle the packing details of multiple data values โ€‹โ€‹that need to be returned from the JDBC ResultSet into a single Java bean.

Which choice to choose is another discussion. Many smart people use Hibernate . The Java platform includes JPA . Using one of the shelves will allow you not to invent your own, which will create your own combination of objects and collections.

0
source share

In addition to using Hibernate, take a look at Spring. It supports pooling, etc. And it allows you to completely abstract JDBC from your code.

It will either return a list of maps to you, or a list of your custom type (depending on how you call it).

0
source share

All Articles