473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to return multiple values in java in below case

1
Please tell me how to return two values in java

Expand|Select|Wrap|Line Numbers
  1. public byte isFirstvalue(String A,
  2.                               String B, String C, Date date) throws Exception {
  3.         CallableStatement cs = null;
  4.         Connection connection = null;
  5.         ResultSet rsResult = null;
  6.         byte isFirstvalue= -1;
  7.         byte isSecondvalue = -1;
  8.         try {
  9.             connection = getConnection();
  10.             cs = connection.prepareCall("{ call IsProc(?,?,?,?) }");
  11.             cs.setString(1, A);
  12.             cs.setString(2, B);
  13.             cs.setString(3, C);
  14.             cs.setDate(4, date);
  15.             rsResult = cs.executeQuery();
  16.             if(rsResult.next()){
  17.                 isFirstvalue= rsResult.getByte("IsFirstvalue");
  18. isSecondValue = rsResult.getByte("IsSecondValue ");
  19.             }
  20.         }
  21.         catch (Exception ex) {
  22.             String errorMessage = ex.getMessage();
  23.             log.error(errorMessage);
  24.             throw ex;
  25.         }
  26.         finally {
  27.             closeResultSet(rsResult);
  28.             closeStatement(cs);
  29.             closeConnection(connection);
  30.         }
  31.         return isFirstValue ;
  32.         return isSecondValue 
  33.  
  34.     }
Sep 11 '13 #1
2 3998
chaarmann
785 Expert 512MB
You have 3 options:
1.)
return an object array:
Expand|Select|Wrap|Line Numbers
  1. return new Object[] {new Integer(isFirstValue), new Integer(isSecondValue)};
2.)
return an instance of an inner class:
Expand|Select|Wrap|Line Numbers
  1. private static class Results{} {
  2.    public final int isFirstValue;
  3.    public final int isSecondValue;
  4.    private Results(int isFirstValue, int isSecondValue) {
  5.       this.isFirstValue = isFirstValue;
  6.       this.isSecondValue = isSecondValue;
  7.    }
  8. }
Then return by:
Expand|Select|Wrap|Line Numbers
  1. return new Results(isFirstValue , isSecondValue);
If "results" would be the returned object, you can access the two results with "results.isFirstValue" and "results.isSecondvalue". I agree that it's a lot of boilerplate code to write, but the advantage is: more clear than accessing by index, for example "object[1]"; it can be inlined by compiler; no in/outboxing.

3.)
For fastest speed, put both integers (32 bits) together in a long (64 bits) and return that long. The lower 32 bits of the long represents the first integer and the higher 32 bits of the long represents the second integer. Use shifting and bit functions to extract the original integers from the long. (For example "originalInteger = longresult & 0xFFFF" returns the first integer.

I assume from the name "isFirstValue" that you really want to store a boolean and not an integer. If that is the case, then you can put both values in an integer, using the first bit for the first boolean and the second bit for the second boolean. then you can retrieve them later by a simple bit-test.
Example:
Expand|Select|Wrap|Line Numbers
  1. int isFirstValueBit = (isFirstValue ? 0 : 1);
  2. int isSecondValueBit = (isSecondValue ? 0 : 2);
  3. int resultInt = isFirstValue + isSecondvalue;
  4. return resultInt;
  5. ...
  6. if (resultInt & 1 != 0) then System.out.println("isFirstValue was set");
  7. if (resultInt & 2 != 0) then System.out.println("isSecondValue was set");
Sep 12 '13 #2
Nepomuk
3,112 Expert 2GB
Actually, if you want to use Apache Commons, you can use something similar to the second solution mentioned by chaarmann without the boilerplate code: The Pair class. Here's an example:
Expand|Select|Wrap|Line Numbers
  1. import org.apache.commons.lang3.tuple.Pair;
  2. //...
  3. Pair<Integer, String> getTwoValues() {
  4.   return Pair.of(1, "two");
  5. }
  6.  
  7. void useThePair() {
  8.    Pair<Integer, String> pair = getTwoValues();
  9.    System.out.println("The first value is " + pair.getLeft());
  10.    System.out.println("The second value is " + pair.getRight());
  11. }
That should be typesafe compared to the array of Objects, shorter than the boilerplate code solution and easier to read than the 64-bit integer. Of course, depending on your problem it might still not be the best solution.
Sep 12 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Lupe | last post by:
hi, if someone can help me I would be grateful when I do def function kjklj llklç return variableA, variableB
0
by: Jaye Gallagher | last post by:
Hi there, I'm coming from a MS-SQL/MySQL background, and am trying to understand the way Postgres phrases "stored procedure" type stuff. What is mystifying me, in particular, is the fact that...
1
by: turtle | last post by:
I need to write an update query that will return the earliest date to a table based on the data of a different table. I have a labor table that looks like this TableLabor JobCode WorkORder ...
4
by: Aaron | last post by:
can a method return multiple values? pseudo code public string method1() { //db pull select col1, col2, from tb1 s1 = col1;
3
by: smen | last post by:
hiye, can stored procedure return multiple @output's? thanks for replying...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
1
by: deepadaffine | last post by:
I am doing a program that calculate velocity and acceleration. I get all my input in main and then i call a sub function giving the input values as parameter and calculate velocity and...
1
by: v4u2chat | last post by:
Do I need to extend any of classes from AXIS to return multiple values? I'm exposing the following method as web service through AXIS to return multiple values. public ContactAddress...
4
by: ashokbio | last post by:
I want to return values of two arguments through a function via same argument. Example: (a, b) = getvalue(x, y) Can anyone help?
2
ADezii
by: ADezii | last post by:
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum. Original Article by Allen Browne ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.