0

I have a method, which simply return a string value. It is first required to connect to a mysql database @ db4free.net (I have check and made sure that there are no problems whilst connecting). Then there is a PreparedStatement In which the query SELECT * FROM ip is executed. ResultSet stores the query result but when I do result.getString(1) it returns nothing. Here's my code below (The class Client extends Connector btw)

public String ObtainServerIP(){
    if(MYSQLConnection!=null){
        try{
            PreparedStatement state = MYSQLConnection.prepareStatement("SELECT * FROM "+TableName);
            result = state.executeQuery();
            result.next();
            return result.getString(1);
        }catch(Exception e){
            return ""+e;
        }
    }
    return "wot m8";
}

The code above is in the Connector class.

The code below is what needs to print the stuff out. (I've even tried index 0 on result set, still nothing).

public void run(){
    while(AppRunning){
        if(DatabaseConnection){
            System.out.println(ObtainServerIP());
            AppRunning=false;
        }
    }
}

Yes the loop is working fine, DatabaseConnection is true and the loop makes it ways into the if statement. However nothing print's out. Even an exception isn't being printed out. Here's a snapshot from my MYSQL Database:

enter image description here

7
  • what output are you getting? use column name to make it clear what column you want to fetch from result set. Commented Jul 19, 2014 at 22:09
  • @Braj Nothing really. Except for the "connected" output from another method to make sure the connection is fine. Commented Jul 19, 2014 at 22:10
  • debug the code and add some more SOP and look where it breaks. Commented Jul 19, 2014 at 22:11
  • Wait..hwow do I open the debug perspective again... Commented Jul 19, 2014 at 22:16
  • @Braj the debugger shows nothing. It works just fine, the code isn't breaking. Commented Jul 19, 2014 at 22:18

1 Answer 1

1

It's better create a String before if statement and the parameter for getString must be the attribute of the table. If you want to obtain only 1 row use if statement for result.next(), in other case use while. Try this:

public String ObtainServerIP(){
String text;

if(MYSQLConnection!=null){
    try{
        PreparedStatement state = MYSQLConnection.prepareStatement("SELECT * FROM "+TableName);
        result = state.executeQuery();
        if (result.next()) {
            text = result.getString("InternetProtocol");
        }
    }catch(Exception e){
        text = e.getMessage();
    }
}
return text;

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.