Pergunta

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());
Foi útil?

Solução

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

Outras dicas

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));
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top