Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Scanner;
  7.  
  8. public class Principal {
  9. Scanner teclado = new Scanner(System.in);
  10. Principal conexion = new Principal();
  11. Connection connect = conexion.conectar();
  12.  
  13. public Connection conectar() {
  14. Connection conexion = null;
  15.  
  16. try {
  17. conexion = DriverManager.getConnection(
  18. "jdbc:mysql://127.0.0.1:3306/m_Prog_Ejer1?useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC",
  19. "m", "1234");
  20. } catch (SQLException e) {
  21. e.printStackTrace();
  22. }
  23.  
  24. return conexion;
  25. }
  26.  
  27. public static void main(String[] args) throws SQLException {
  28. Principal conexion = new Principal();
  29.  
  30. conexion.listarMenu();
  31. }
  32.  
  33. public void listarMenu() throws SQLException {
  34. int opcion = 0;
  35. boolean salir = false;
  36. do {
  37. do {
  38.  
  39. System.out.println("** Elige un opcion **");
  40. System.out.println("1. Anyadir persona");
  41. System.out.println("2. Eliminar persona");
  42. System.out.println("3. Listar personas");
  43. System.out.println("4. Salir");
  44. while (!teclado.hasNextInt()) {
  45. teclado.next();
  46. System.out.println("Debe ser entero");
  47. }
  48. opcion = teclado.nextInt();
  49. } while (opcion < 1 || opcion > 4);
  50.  
  51. switch (opcion) {
  52. case 1:
  53. insertarPersonas();
  54. break;
  55. case 2:
  56. eliminarPersonas();
  57. break;
  58. case 3:
  59. listarPersonas();
  60. break;
  61. case 4:
  62. salir = true;
  63. break;
  64. default:
  65. }
  66. } while (salir == false);
  67.  
  68. }
  69.  
  70. public void insertarPersonas() throws SQLException {
  71. Statement stmt = connect.createStatement();
  72. String nombre;
  73. String apellido;
  74. System.out.println("Indicame el nombre:");
  75. nombre = teclado.next();
  76. System.out.println("Indicame el apellido");
  77. apellido = teclado.next();
  78. stmt.executeUpdate("insert into personas(nombre,apellido) values ('" + nombre + "','" + apellido + "')");
  79. System.out.println("Filas afectadas " + stmt.getUpdateCount());
  80. }
  81.  
  82. public void eliminarPersonas() throws SQLException {
  83. Statement stmt = connect.createStatement();
  84. String nombre;
  85. String apellido;
  86. System.out.println("Indicame el nombre:");
  87. nombre = teclado.next();
  88. System.out.println("Indicame el apellido");
  89. apellido = teclado.next();
  90. stmt.executeUpdate("delete from personas(nombre,apellido) values ('" + nombre + "','" + apellido + "')");
  91. System.out.println("Filas afectadas " + stmt.getUpdateCount());
  92. }
  93.  
  94. public void listarPersonas() throws SQLException {
  95. Statement stmt = connect.createStatement();
  96. ResultSet resultado = stmt.executeQuery("select id,nombre,apellido from personas");
  97. while (resultado.next()) {
  98. System.out.println("Fila");
  99. System.out.println(resultado.getInt("id"));
  100. System.out.println(resultado.getString("nombre"));
  101. System.out.println(resultado.getString("apellido"));
  102. }
  103.  
  104. stmt.close();
  105. connect.close();
  106. }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement