Advertisement
JVFabia

JDBC

Oct 5th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. package org.forge;
  2.  
  3. import java.sql.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         // nombre = Juan Perez
  9.         // rut=  25.542.569-8
  10.         // user = jperez
  11.         // password = P25.54
  12.         // url = forgedb.netbyteoss.com
  13.         // puerto = 5443
  14.         // base de datos = forge_alumnos
  15.         try {
  16.             try {
  17.                 Class.forName("org.postgresql.Driver");
  18.             } catch (ClassNotFoundException ex) {
  19.                 System.out.println("Error, el driver de PostgresSQL no se ha cargado : " + ex);
  20.             }
  21.             Connection conn = null;
  22.             // Conectamos con la base de datos
  23.             conn = DriverManager.getConnection(  "jdbc:postgresql://forgedb.netbyteoss.com:5443/forge_alumnos",                    "jperez", "P25.54");
  24.             // Consulta a la bd (string)
  25.             String consulta = "select * from ejercicios";
  26.             // Preparacion de consulta y enlace a conexion
  27.             PreparedStatement ps = conn.prepareStatement(consulta);
  28.             // Variable que almacenara el retorno de nuestra consulta
  29.             ResultSet rs = ps.executeQuery();
  30.             // Para recorrer las tuplas en este caso ocuparemos while
  31.             while (rs.next()){
  32.                 //De acuerdo al tipo de dato de atributo en la bd usamos los metodos de rs al ser strin rs.getString(nombre atributo)
  33.                 System.out.println(" Nombre : " +  rs.getString("nombre") );
  34.             }
  35.  
  36.         } catch (java.sql.SQLException sqle) {
  37.             System.out.println("Error:" + sqle);
  38.         }
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement