Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-14-2008, 11:27 PM   PM User | #1
adi501
New Coder

 
Join Date: Jul 2008
Location: Falls Chuch, VA
Posts: 30
Thanks: 0
Thanked 1 Time in 1 Post
adi501 is an unknown quantity at this point
Method returning Multiple Values

Hello .

I am trying to return multiple values from a method and i am unable to dothat.
Can anybody can help me in this regard.

The following is the Method i have.

Code:
 public double getDistance(String City1 , String City2){

        double lat1=0.0, lat2=0.0, long1=0.0, long2=0.0;
        ResultSet rs = null;
        Connection conn = null;
        MakeConnection oMakeConnection = new MakeConnection();
        conn = oMakeConnection.getConnection();
        String sql = null;
        sql = "SELECT LATITUDE, LONGITUDE from zipcodes WHERE city = ?";
        java.sql.PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        try {
            pstmt.setString(1,City1);
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        try {
            rs=pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        try {
            if(rs.next())
            {
              long1 = rs.getDouble("longitude");
              lat1  = rs.getDouble("latitude");
            }
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        String sql1 = null;
        sql1 = "SELECT LATITUDE,LONGITUDE from zipcodes WHERE city = ? ";
        pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        try {
            pstmt.setString(1,City2);
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        try {
            rs=pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        try {
            if(rs.next())
        {
          long2 = rs.getDouble("longitude");
          lat2  = rs.getDouble("latitude");
            }
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return long1;
        return long2;
        return lat1;
        return lat2;
    }
I cant return long1, long2, lat1, lat2.

I want all the values. How can i do that ??

Waiting for the reply.
Thanks in Advance.
adi501 is offline   Reply With Quote
Users who have thanked adi501 for this post:
troublemaker (07-15-2008)
Old 07-15-2008, 03:28 AM   PM User | #2
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
First, why do you want to do that in a method named getDistance? Map coordinates don't equate to distance. As for getting the values, you would be much better off to use get/set methods for each property here. For example you could declare instance variables for the class and access them as follows:

PHP Code:
private double long1;

public 
void setLong1(double long1)
{
    
this.long1 long1;
}

public 
double getLong1()
{
    return 
long1;

EDIT: For the record, I'm not aware of a way to return multiple values in java, or of a situation in which it would be appropriate.
brad211987 is offline   Reply With Quote
Users who have thanked brad211987 for this post:
troublemaker (07-15-2008)
Old 07-15-2008, 05:47 AM   PM User | #3
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
nothing is passed by reference in java, everything by value, well not all...
you could try something like this


Code:
StrinfBuffer sb = new StringBuffer();
public double getDistance(String City1 , String City2, sb)
{
  ....
  sb.append ....
  // no need for return sb is by reference
  // so it comes out filled as you did fill it
}

... writing this out of head, so ...
BubikolRamios is offline   Reply With Quote
Old 07-15-2008, 05:19 PM   PM User | #4
jerry62704
Senior Coder

 
jerry62704's Avatar
 
Join Date: Oct 2007
Location: Springfield, IL
Posts: 1,049
Thanks: 9
Thanked 82 Times in 82 Posts
jerry62704 is on a distinguished road
You can return a collection, but you need to back up and ask why you want one function to do three things.
__________________
.
.
...and gladly would he learn and gladly teach

Visit www.LiberalsWin.com for humor and the unique Bush/Obama Approval Polls
jerry62704 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:45 AM.


Advertisement
Log in to turn off these ads.