/*
Przygotowanie bazy danych:

drop table Names;
create table Names ( id int primary key, name varchar(20) );

begin
  insert into Names values( 1, 'Asia' );
  insert into Names values( 2, 'Basia' );
  insert into Names values( 3, 'Kasia' );
end;

select * from Names;
 */

import java.sql.*;

public class GetData {

    /**
	 * @param args
	 */
    public static void main(String[] args) throws ClassNotFoundException {
        Connection connection = null;
        Statement stmt = null;
        try {
            Class.forName("org.hsqldb.jdbcDriver");
            connection = DriverManager.getConnection("jdbc:hsqldb:hsqldbtest.db", "sa", "");
            String qs = "select id, name from names order by id";
            stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(qs);
            System.out.println("id" + "\t" + "name");
            while (rs.next()) {
                System.out.println(rs.getInt("id") + "\t" + rs.getString("name"));
            }
            rs.close();
            stmt.close();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
            while (sqle != null) {
                String logMessage = 
                    "\n SQL Error: " + sqle.getMessage() + "\n\t\t" + 
                    "Error code: " + sqle.getErrorCode() + "\n\t\t" + 
                    "SQLState: " + sqle.getSQLState() + "\n";
                System.out.println(logMessage);
                sqle = sqle.getNextException();
            }
        } finally {
            try {
                if (connection != null)
                    connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
