0

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
2
  • Did you try rs.getString("GamerTag") ? Commented Aug 30, 2014 at 14:28
  • You might want to go through the JDBC tutorial before continuing: docs.oracle.com/javase/tutorial/jdbc/index.html Commented Aug 30, 2014 at 14:34

2 Answers 2

1

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.

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

2 Comments

Thanks im adding it to an arrayList so do i add rs.getString("GamerTag") or rs.toString()? Your way printed the right result
rs.toString() returns a string that contains the name of the recordset class and the hashtag of the object. It's only useful for debugging.
0

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();

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.