1

My question is this, what is the most efficient way to use the toString method to format data from a ResultSet?

Here is the method that I have thus far:

public void query()
{
    Statement stmt = null;
    ResultSet rs = null;

    try
    {
        //Create database connection
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/registrar",
                "abt1s", "abt1s");
        stmt = conn.createStatement();

        //create SQL statement
        String st = "SELECT * FROM  student WHERE studentID = '" + this.getStudentID() + " '";

        rs = stmt.executeQuery(st);

        String studentData = rs.toString();
        System.out.println(studentData);

        rs.close();
        stmt.close();
        conn.close();
    }catch(Exception e)
            {
                System.err.println("ERROR: Either cannot connect to the DB " + " or error with the SQL statement");
            }

}
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Nathan Jester
  • 11
  • 1
  • 2
  • What do you want to see on output? Why do you believe this is not efficient enough for your uses? – Mike Samuel Feb 10 '12 at 05:29
  • 2
    `"SELECT * FROM student WHERE studentID = '" + this.getStudentID() + " '";` is a potential [SQL injection risk](https://www.owasp.org/index.php/Preventing_SQL_Injection_in_Java). Please get in the habit of using prepared statements instead. – Mike Samuel Feb 10 '12 at 05:30
  • 2
    Your code leaves SQL connections open when a single statement fails with a `SQLException`. Maybe put the close code in a `finally` block. – Mike Samuel Feb 10 '12 at 05:32
  • I understand about prepared statements. I will make that change. the code that I have returns the hash of the ResultSet. What I need to do is, print the data contained in the rs using the overridden toString method. That is not included in the example, but I think I have coded that correctly. – Nathan Jester Feb 10 '12 at 05:34
  • On what class is your `toString` method defined? The implementation of `ResultSet` returned is determined by the JDBC driver, not your code, so you can pass it to a `static` method that turns it into a `String` but unless you are implementing your own `Driver` wrapper, you have little control over what `rs.toString` does. – Mike Samuel Feb 10 '12 at 05:36
  • I have defined the toString method in a class called Graduate. The query method that I submitted is also declared in this class. The entire project consists of three classes that all inherit from a Student class. I have overridden the toString method in each of these classes to suit there need. They also inherit the toString from the Student superclass – Nathan Jester Feb 10 '12 at 05:45
  • `toString()` is for debugging, not meeting system design criteria. Use a dedicated method, or at least dedicated code. – user207421 Oct 17 '21 at 09:10

2 Answers2

1

You can go row by row through the ResultSet and print whatever you want:

// Fetch each row from the result set
while (rs.next()) {
    // Get the data from the row using the column index
    String s = rs.getString(1);

    // Get the data from the row using the column name
    s = rs.getString("col_string");
}
Dan
  • 724
  • 1
  • 9
  • 19
  • I have tried this and it does get the data from the result set. I will go back to using this. How would I format this using the toString method? – Nathan Jester Feb 10 '12 at 05:37
  • Because the ResultSet class is not your own class, you can't change it's methods. The best you could do is write a static method that takes in a ResultSet parameter and returns a string. – Dan Feb 10 '12 at 05:44
  • Basically, I'm supposed to implement the toString method to format the data and print to the screen. I'm just having a hard time seeing how that is done. – Nathan Jester Feb 10 '12 at 05:51
1

You need to convert the ResultSet into a Student class before you can use Student.toString to generate the proper output.

"Retrieving and Modifying Values from Result Sets" explains one way of extracting data from a ResultSet.

Alternatively, you can look at an Object-Relational-Mapping (ORM) which eases creating domain objects (like Student) from database rows. What Java ORM do you prefer, and why? has useful pointers to discussions of the various ORMs and how to get started with them.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245