Guest User

Untitled

a guest
Nov 21st, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. String driverJDBC= "com.mysql.jdbc.Driver";
  2.  
  3. Class.forName(driverJDBC);
  4.  
  5. package hi;
  6.  
  7. import java.io.*;
  8. import java.sql.*;
  9.  
  10. public class Hi {
  11.  
  12. import java.sql.*;
  13. import java.io.*;
  14. public class Mostrar {
  15.  
  16. public static void main(String[] args) throws SQLException, IOException{
  17. BufferedReader key= new BufferedReader (new InputStreamReader(System.in));
  18. String user, password;
  19. String driverJDBC= "com.mysql.jdbc.Driver";
  20. System.out.println("Usuario");
  21. user=key.readLine();
  22. System.out.println("contraseña");
  23. password=key.readLine();
  24.  
  25. try {
  26. Class.forName(driverJDBC);
  27. Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/agenda", user, password);
  28. Statement st = conexion.createStatement();
  29. st.executeUpdate("DROP TABLE IF EXISTS personal;");
  30. st.executeUpdate("CREATE TABLE personal (`Identificador` int(11) NOT NULL AUTO_INCREMENT, `Nombre` varchar(50) NOT NULL, `Apellidos` varchar(50) NOT NULL, `Telefono` varchar(9) DEFAULT NULL, `Email` varchar(60) DEFAULT NULL, PRIMARY KEY (`Identificador`));");
  31. st.executeUpdate("INSERT INTO personal (`Identificador`, `Nombre`, `Apellidos`, `Telefono`, `Email`) VALUES (1, 'José', 'Martínez López', '968112233', 'jose@martinezlopez.com'), (2, 'María', 'Gómez Muñoz', '911876876', 'maria@gomezoliver.com'), (3, 'Juan', 'Sánchez Fernández', '922111333', 'juan@sanchezfernandez.com'), (4, 'Ana', 'Murcia Rodríguez', '950999888', 'ana@murciarodriguez.com');");
  32. ResultSet rs = st.executeQuery("SELECT * FROM personal;");
  33.  
  34. if (rs != null) {
  35. System.out.println("El listado de persona es el siguiente:");
  36.  
  37. while (rs.next()) {
  38. System.out.println(" ID: " + rs.getObject("Identificador"));
  39. System.out.println(" Nombre completo: " + rs.getObject("Nombre") + " " + rs.getObject("Apellidos"));
  40. System.out.println(" Contacto: " + rs.getObject("Telefono") + " " + rs.getObject("Email"));
  41. System.out.println("- ");
  42. }
  43. rs.close();
  44. }
  45. st.close();
  46.  
  47. }
  48. catch(SQLException s)
  49. {
  50. System.out.println("Error: SQL.");
  51. System.out.println("SQLException: " + s.getMessage());
  52. }
  53. catch(Exception s)
  54. {
  55. System.out.println("Error: Varios.");
  56. System.out.println("SQLException: " + s.getMessage());
  57. }
  58. System.out.println("FIN DE EJECUCIÓN.");
  59. }
  60.  
  61. }
Add Comment
Please, Sign In to add comment