Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.ArrayList;
- public class SelectTable {
- public static void main(String[] args) throws Exception {
- Connection c = null;
- Statement stat = null;
- try {
- // Cargar controlador de datos, base de datos
- Class.forName("org.postgresql.Driver");
- // Conéctese a la dirección de la base de datos, nombre, contraseña
- c= DriverManager
- .getConnection("jdbc:postgresql://127.0.0.1:5432/postgres", "postgres", "maxhito");
- c.setAutoCommit(false);
- System.out.println("Opened database successfully");
- stat = c.createStatement();
- // Consulta la base de datos
- ResultSet rs = stat.executeQuery("SELECT * FROM BBQ;");
- while ( rs.next() ) {
- System.out.println(rs.getInt("id"));
- System.out.println(rs.getString("name"));
- System.out.println(rs.getInt("age"));
- System.out.println(rs.getString("address"));
- System.out.println();
- }
- rs.close();
- stat.close();
- c.close();
- } catch (ClassNotFoundException e) {
- System.err.println( e.getClass().getName()+": "+ e.getMessage() );
- System.exit(0);
- }
- System.out.println("Operation done successfully");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement