0

I have a method :

public void dbQuery(String query, String what) {
    try {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        if(what.equals("temp_table")) {
            String temporary_table = rs.getString("table_name_temp");
            System.out.println(temporary_table);
            return;
        }

        }

    catch (Exception e) {
        e.printStackTrace();
        }

    finally {
        if (rs != null) rs.close();
        if (stmt != null) stmt.close();
        }

}



String query = "EXEC temptables.dbo.create_temp_cdr 'SIP'";
String temporary_table = db.dbQuery(query,"temp_table");

How do I get the return of a void to use it in another db.dbQuery() ?

PS : I need a value from dbQuery() so I can construct another query to call dbQuery() again

pufos
  • 2,890
  • 8
  • 34
  • 38
  • I showed you how to do it here: http://stackoverflow.com/questions/10617128/java-sql-connections-via-class/10617251#10617251 – duffymo May 16 '12 at 14:19
  • @duffymo that's just chinese ... I want to take it slow – pufos May 16 '12 at 14:20
  • 1
    No, it's real code. Read it, especially the map() method. It shows you how to load a ResultSet into an object in a general way and return it. – duffymo May 16 '12 at 14:21
  • @duffymo i'm not to good at java so I don;t know what you did there means ... – pufos May 16 '12 at 14:22
  • So we see. You've got to be able to read about ten lines of code. – duffymo May 16 '12 at 14:22
  • I can learn very quick but not that quick .. I came from php .. so.. – pufos May 16 '12 at 14:24
  • @pufos: If you're not ready for that code, you shouldn't be even *trying* to handle database connections and the like. Learn Java from scratch, taking small steps. Learn the basics of the language (like method return types). – Jon Skeet May 16 '12 at 14:32
  • There is no return from the void. [Ominous voice] – Andy Thomas May 16 '12 at 14:40
  • with java everything has to be hard man .. Why there is no class to connect to database and share the same code for the query process .. like php`s PDO .. what is wrong with this ? I wanted to make a function to handle all query but is seems imposible or very mega eta complicated ... geez – pufos May 16 '12 at 14:42
  • 2
    @pufos: You don't know enough Java to accurately judge what's hard and what isn't, I'm afraid. Without knowing PHP, I'd find doing anything in that hard too. The solution isn't to complain about the language - it's to learn it. – Jon Skeet May 16 '12 at 14:45

6 Answers6

9

How do I get the return of a void to use it in another db.dbQuery() ?

What do you mean by "the return of a void"? If you mean "the return value" then there isn't one - that's the point of the method being declared as void.

If you want to return a value, then don't make it a void method... given this:

String temporary_table = db.dbQuery(query,"temp_table");

it looks like you just want to change dbQuery to return String. Note that you'll need to work out what to return if what isn't temp_table. (You should also fix your exception handling - just printing out the stack trace to stdout and then continuing regardless is almost never the right approach.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    Man i came from php and now ... java is blowing my mind ... :| – pufos May 16 '12 at 14:17
  • I need a value from `dbQuery()` so I can construct another `query` to call `dbQuery()` again – pufos May 16 '12 at 14:18
  • 1
    @pufos: What's mind-blowing about it? This seems pretty straightforward to me. If you're attempting Java by trial and error, I'd strongly suggest that you get a book (or at least read a tutorial) instead... something which will cover what the different bits of a method declaration mean. – Jon Skeet May 16 '12 at 14:19
  • @pufos: Does PHP provides capability of returning on void – Kunal May 16 '12 at 14:19
  • 1
    in php there is only `function` and it will return anything – pufos May 16 '12 at 14:21
  • But in Java, you must specify a return type, and a return type of `void` means that you will not (and cannot) return anything. – Louis Wasserman May 16 '12 at 14:24
  • @pufos: I explained it in my answer: change the method so it has a return type of `String`. If you don't know enough Java to do that yet, you shouldn't be anywhere *near* database code... – Jon Skeet May 16 '12 at 14:30
  • So can you help me ? I get the point .. but now can you help me ? – pufos May 16 '12 at 14:37
  • 4
    @pufos: The most useful thing I can now is do whatever I can to persuade you *not* to keep working on this code without reading a book or tutorial on Java. If "change the method so it has a return type of String" isn't clear enough, then just giving you the code would only help you for about 5 minutes before you came to the next bit of Java you don't understand. **Learn the language properly.** – Jon Skeet May 16 '12 at 14:44
2

You can either have your method not be void, and have a return type, then you can have the line

return temporary_table;

which would return the temporary_table variable.

Another way would be to pass by reference to the method. For example, you can pass a StringBuilder object to the method. Any changes to this object in the method will then also apply after the method has returned.

public void addToString(StringBuilder query, String toAdd) {
    query.append(toAdd);
}

A call to this method of

StringBuilder query = new StringBuilder("start");
System.out.println(query.toString());
addToString(query, "end");
System.out.println(query.toString());

would have the output of:

start
startend

0

You can't return a value from a method that is defined as void - by definition this means it doesn't return any value.

If you see a return; statement in a void method, it basically means that this will end the current method and return processing back to the caller.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
0

I'd make your method string instead of bool. Void won't return anything ever.

Frantumn
  • 1,725
  • 7
  • 36
  • 61
0

A void method is something that does not return anything. But, your requirement seems like you have to use value generated in this method in the previous layer. One obvious solution for this is to change it's return type from void to String. But, if you don't want to do this , simply pass one more string parameter to your method. String is an object, so when you are passing it to a method, you are actually passing reference of that object to it. Simply set your generated value in this string and you will be able to access it in previous layer with that string variable(It's sort of similar to Pass by Reference in C). I hope this workaround will solve your problem. Please correct me if I am wrong.

Sumit Desai
  • 1,542
  • 9
  • 22
0

In fact he wants to have something similar like functions in VB or so. First, create your void as usual.

private  Void getheightoflistview(ListView nameOfListView){
   //do whatever you want to do
    int numberOfItems = nameOfListView.getCount();
    //You want to return the numberOfItems 
}

So if you want to return an Integer value, just replace the Void with Integer

private  Integer getheightoflistview(ListView nameOfListView){
   //do whatever you want to do
    int numberOfItems = nameOfListView.getCount();
    //You want to return the numberOfItems 
    return numberOfItems; //this is the Integer you want to return

}

and add the return value

frans
  • 17
  • 3