0

How can I return two different type of columns (int , string) separately in a MySQL table? I have tried using this code but I can't figure it out. Please kindly, can you tell me what's wrong?

please notes :::i want to return keyid_col and key_col

Here is my code :::

 public  void retrieve_keyword () throws SQLException{

    try {
        ResultSet results = statement.executeQuery
                ("SELECT keyword_id,key_words  FROM keywordlist");  
        try {

            while ( results.next() ) {
                int numColumns =
                        results.getMetaData().getColumnCount();
                int keyid_col = results.getInt("keyword_id");
                String key_col = results.getString("key_words");

                if (numColumns == 2) {
                    System.out.println(results.getObject(numColumns));
                }

                for ( int i = 1 ; i <= numColumns ; i++ ) {
                    System.out.println( "COLUMN " + i + " = " + results.getObject(i) );
                }        
            }
        } 

        catch(Exception ew){
            System.out.println( ew );
        }
    }catch(Exception ew){
        System.out.println( ew );
    }
}
Freeman
  • 83
  • 1
  • 3
  • 9

6 Answers6

2

Java doesn't support this out of the box. You'll need to create a data structure and use it. For example:

 public class retType {
     public retType( int val1, string val2 ) {
           this.val1 = val1; 
           this.val2 = val2;
     }
     public int val1;
     public string val2;
 }

 public retType someFunc()
 {
     int i = 1;
     string j = "example";
     return new retType(i, j);
 }
Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39
1

You can return an Array, or a List (or other collection oriented data structures), or you can return an instance of a type (class or struct) that has two properties, one for each thing you wish to return.

Languages like Java, Javascript, C# etc. don't allow multiple returned items. You have to return something that contains whatever is relevant for you.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
1

You could use a Pair class.

class Pair<T1,T2> {
  private final T1 i1;
  private final T2 i2;

  public Pair(T1 i1, T2 i2) {
    this.i1 = i1;
    this.i2 = i2;
  }

  public T1 getI1() {
    return i1;
  }

  public T2 getI2() {
    return i2;
  }

  public String toString () {
    return "{" + i1 + "," + i2 + "}";
  }

}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

In Java, and most languages, you cannot return two values from one method. Alternatives would be to pass in an object, and change the values within the object, or wrap the return values as an object. Or you could return some collection, like a List, Set, or Map.

CodeChimp
  • 8,016
  • 5
  • 41
  • 79
0

Your choices are to define a class that has the fields that you want (as in as Captain Skyhawk's example), or to return an Object[] which can simply carry an Integer and a String object inside of it. An example of that is

Object objArrayReturnValue[] = { new Integer(51), "abcd" };
return objArrayReturnValue;

This is easy but also 'lazy' in that most people would discourage its use. When you return an object array you are forfeiting a lot of the benefits of a (mostly) strongly typed language like Java.

Monolithguy
  • 313
  • 6
  • 17
0

I do this using a HashMap:

public Map<String, String> getUser() throws SQLException {
  Map<String, String> users = new HashMap<>();
  ...do query...
  users.put(p_key,username);
  return users; 
}

If a native collection does not do it for you, than you will need a custom object to do it. i.e return a class.

Robert H
  • 11,520
  • 18
  • 68
  • 110