user3991643
user3991643

Reputation: 17

Getting Incorrect return in java accessing sql database

This is the code im using in java to query the database

                connect = DriverManager.getConnection (url, "user", "pass");
                state = connect.createStatement();
                String meetID = "SELECT GamerTag FROM backup";
                ResultSet rs = state.executeQuery(meetID);

                   while(rs.next()){
                        System.out.println(rs.toString());
                   }

Im not getting the values of the row in the database im getting this instead

com.mysql.jdbc.JDBC4ResultSet@108137c9

Upvotes: 0

Views: 61

Answers (2)

SparkOn
SparkOn

Reputation: 8946

What do you expect rs.toString() should do it will just print the hash of the resultsetObject if you want to get the column values you should do this way

while(rs.next()){
System.out.println(rs.getString("yourFirstColumnName")+"  "+
                           rs.getString("yourSecondColumnName")+"  "+
                          rs.getString("yourThirdColumnName"));
}

Really you should use PreparedStatement. In your case though you are not using any parameterizedQuery but One of the major benefits of using PreparedStatement is better performance. PreparedStatement gets pre compiled.

In database and there access plan is also cached in database, which allows database to execute parametric query written using prepared statement much faster than normal query because it has less work to do. You should always try to use PreparedStatement.

So you can do something like this

String query = "SELECT GamerTag FROM backup"
PreparedStatement st =connect.prepareStatement("query");
ResultSet rs = st.executeQuery();

Upvotes: 0

Andomar
Andomar

Reputation: 238068

You're printing the result of the toString method of the Recordset object, which appears to print out the object's name and hashcode.

Instead, try to print the value of a column. Perhaps using getString:

System.out.println(rs.getString("GamerTag"));

The documentation for Java's recordset looks confusing, you might be better off searching for examples.

Upvotes: 1

Related Questions