Advertisement
Geeo

puto jolu

Jan 29th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. package consultaPostgresqlJavaGuillermo;
  2.  
  3. import java.beans.Statement;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. public class Main {
  10.  
  11. public static void main(String[] args) {
  12.  
  13. try {
  14.  
  15. Class.forName("org.postgresql.Driver");
  16. Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pruebasql", "postgres", "postgres");
  17.  
  18.  
  19. mostrarNotas(c);
  20.  
  21.  
  22. c.close();
  23.  
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. System.err.println(e.getClass().getName() + ": " + e.getMessage());
  27. System.exit(0);
  28. }
  29.  
  30. }
  31.  
  32. private static void mostrarNotas(Connection c) throws SQLException {
  33.  
  34. Statement stmt = (Statement) c.createStatement();
  35. ResultSet rs = ((java.sql.Statement) stmt).executeQuery( "SELECT * FROM alumnos;" );
  36. while ( rs.next() ) {
  37.  
  38. int id = rs.getInt("id");
  39. String nombre = rs.getString("nombre");
  40.  
  41.  
  42. System.out.println( "Id: " + id );
  43. System.out.println( "Nombre: " + nombre );
  44.  
  45.  
  46. }
  47. rs.close();
  48.  
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement