javadatabaseodb

Trying to get data from an odb databes in Java


        ResultSet rs = sql.executeQuery
        ("SELECT HP FROM PKMN WHERE (ID == basicnumber)"); 

That is the code I want to use, but I have a mistake I do not see...

Explanation:

I am accessing my .odb Database from Java. I want a user to Input an ID (saved in int basicnumber), add it to the query and get the value from the column hp, specific to the ID. This value has to be saved in an int hp and NOT prompted.

how do I get there?


Solution

  • First, you need to concatenate your basicnumber to the query:

    ResultSet rs = sql.executeQuery
        ("SELECT HP FROM PKMN WHERE ID = " + basicnumber);
    

    then you can retrieve the result and store it into a variable:

    int hp = rs.getInt(1); 
    

    See the ResultSet API to explore more ways to retrieve values from the resultset.