0

I have a table called ClientsTable and a database called myDb .

How can I print with System.out.prinln() the entire database ? I just want to select all the rows and present all the fields to the screen .

Thanks

EDIT: what should I put instead of rs.getString() , here (since it doesn't compile):

    ResultSet rs = this.m_statement.executeQuery("SELECT * FROM CheckingAccountsTable");
    while (rs.next())
        System.out.println(rs.getString());
Jack cole
  • 447
  • 2
  • 8
  • 24

2 Answers2

3

if you don't know how many colums are in the table, try something like this :

    ResultSet rs = this.m_statement.executeQuery("SELECT * FROM CheckingAccountsTable");
    int columnCount = rs.getMetaData().getColumnCount();
    while (rs.next()) {
        for (int i = 1; i <= columnCount; i++) {
            System.out.print(rs.getString(i));
        }
        System.out.println();
    }
mabroukb
  • 691
  • 4
  • 11
  • You may want to add _another_ for loop before the while loop to print the column names from rs.getMetaData().getColumnName(i) – Chris Nava Aug 08 '12 at 21:01
1

The SQL query would be:

SELECT * FROM DB_TABLE_NAME

Then you can loop through your result set and print it out.

Edit:

You aren't specifying what column you want to print out.

Example below:

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

Second edit:

ResultSet rs = this.m_statement.executeQuery("SELECT * FROM CheckingAccountsTable");
int colCount = rs.getMetaData().getColumnCount();
while (rs.next()){
    for(int i = 1; i < colCount + 1; i++){
       System.out.println(rs.getString(i));
    }
}
edhedges
  • 2,722
  • 2
  • 28
  • 61