Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package consultaPostgresqlJavaGuillermo;
- import java.beans.Statement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class Main {
- public static void main(String[] args) {
- try {
- Class.forName("org.postgresql.Driver");
- Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pruebasql", "postgres", "postgres");
- mostrarNotas(c);
- c.close();
- } catch (Exception e) {
- e.printStackTrace();
- System.err.println(e.getClass().getName() + ": " + e.getMessage());
- System.exit(0);
- }
- }
- private static void mostrarNotas(Connection c) throws SQLException {
- Statement stmt = (Statement) c.createStatement();
- ResultSet rs = ((java.sql.Statement) stmt).executeQuery( "SELECT * FROM alumnos;" );
- while ( rs.next() ) {
- int id = rs.getInt("id");
- String nombre = rs.getString("nombre");
- System.out.println( "Id: " + id );
- System.out.println( "Nombre: " + nombre );
- }
- rs.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement