Guest User

Untitled

a guest
Jan 29th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. package IntroduccionJDBC;
  2.  
  3. import java.sql.*;
  4. import java.util.TimeZone;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7.  
  8. /**
  9. *
  10. * @author marti
  11. */
  12. public class IntroduccionJDBC {
  13. public static void main(String[] args) {
  14. try {
  15. String url = "jdbc:mysql://localhost:3306/sga?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=" + TimeZone.getDefault().getID();
  16. Class.forName("com.mysql.cj.jdbc.Driver");
  17.  
  18. Connection conexion=(Connection)DriverManager.getConnection(url, "root", "admin");
  19. Statement sentencia=conexion.createStatement();
  20.  
  21. String sql="select id_persona, nombre ,apellido from persona";
  22. ResultSet resultado=sentencia.executeQuery(sql);
  23.  
  24. while(resultado.next()){
  25. System.out.println("Id: "+resultado.getInt(1));
  26. System.out.println("Nombre: "+ resultado.getString(2));
  27. System.out.println("Apellido: "+resultado.getString(3));
  28. System.out.println("____________");
  29. }
  30.  
  31. resultado.close();
  32. sentencia.close();
  33. conexion.close();
  34.  
  35. } catch (ClassNotFoundException | SQLException ex) {
  36. ex.printStackTrace();
  37. }
  38.  
  39. }
  40. }
Add Comment
Please, Sign In to add comment