spitti84
spitti84

Reputation: 121

Returning a value from a sql select after a try catch

I want this method to return ap string after reading from the table, but it seems like it returns only the initial value I gave it, any thoughts?

public String ReadAp(String val) {

    SQLHelper sql = new SQLHelper();
    String apStr = "Test ap initial";
   try
       {
           sql.Init("DB_Name");

           ResultSet rsData = sql.RunSelect("select AP from DB_Name.myTable where     

MD='"+val+"'");

           apStr = rsData.getString("AP");

       }
   catch(Exception exInit)
       {

           System.out.println("Excepted while attempting to connet to: ");
       }



return apStr;

}

Upvotes: 0

Views: 89

Answers (1)

David Hofmann
David Hofmann

Reputation: 5775

After you get a ResultSet Object in jdbc you are always supposed to do

if (rs.next()) {
   // rs.getString(...
}

that is because the query can return no values therefore the need to check if there is a next row, and if you don't do next() each time (even for the first one) you will get nothing.

Upvotes: 1

Related Questions