Advertisement
Guest User

JDBC SQL

a guest
Jan 24th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. public void conexion()
  2. {
  3. if (connection != null)
  4. {
  5. return;
  6. }
  7. String url = "jdbc:postgresql://localhost:5432/ConexionJAVA";
  8. String user="postgres";
  9. String password = "sekizou123";
  10. try
  11. {
  12. try {
  13. Class.forName("org.postgresql.Driver").newInstance();
  14. } catch (InstantiationException | IllegalAccessException ex) {
  15. Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
  16. }
  17. connection = DriverManager.getConnection(url, user, password);
  18. if (connection != null)
  19. {
  20. //System.out.println("Conectando a Base de Datos...");
  21. JOptionPane.showMessageDialog(null, "Conectando a Base de Datos...", "Mensaje de Conexión", PLAIN_MESSAGE);
  22.  
  23. }
  24.  
  25. }
  26. catch (ClassNotFoundException | SQLException e)
  27. {
  28. //System.out.println("Problemas de Conexión");
  29. JOptionPane.showMessageDialog(null, "Problemas de Conexión", "Mensaje de Conexión", WARNING_MESSAGE);
  30.  
  31. }
  32. }
  33.  
  34. public void Actualizar()
  35. {
  36. conexion();
  37. String CadenaCedula=txtced.getText();
  38. int cedula=Integer.parseInt(CadenaCedula);
  39. String nombre=txtnom.getText();
  40. String apellido=txtape.getText();
  41. String direccion=txtdir.getText();
  42. try
  43. {
  44. String sql="UPDATE FROM RC SET(cedula="+cedula+"),(nombre="+nombre+"),(apellido="+apellido+"),(direccion="+direccion+")";
  45. st = connection.createStatement();
  46. st.executeUpdate(sql);
  47. }
  48. catch(SQLException e)
  49. {
  50. e.printStackTrace();
  51. }
  52. finally
  53. {
  54. try
  55. {
  56. if(connection!=null)
  57. {
  58. connection.close();
  59. }
  60.  
  61. }
  62. catch (SQLException ex)
  63. {
  64. Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
  65. }
  66. }
  67. }
  68.  
  69. public void Eliminar() {
  70. conexion();
  71. String CadenaCedula=txtced.getText();
  72. int cedula=Integer.parseInt(CadenaCedula);
  73. try
  74. {
  75.  
  76. st = connection.createStatement();
  77. st.executeUpdate("DELETE FROM RC WHERE cedula = " + cedula);
  78. }
  79. catch(SQLException e)
  80. {
  81. e.printStackTrace();
  82. }
  83. finally
  84. {
  85. try
  86. {
  87. if(connection!=null)
  88. {
  89. connection.close();
  90. }
  91.  
  92. }
  93. catch (SQLException ex)
  94. {
  95. Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
  96. }
  97. }
  98. }
  99.  
  100. public void Insertar()
  101. {
  102. conexion();
  103. String CadenaCedula=txtced.getText();
  104. int cedula=Integer.parseInt(CadenaCedula);
  105. String nombre=txtnom.getText();
  106. String apellido=txtape.getText();
  107. String direccion=txtdir.getText();
  108. try
  109.  
  110. {
  111. st = connection.createStatement();
  112. int columna = st.executeUpdate("INSERT INTO RC VALUES ('"+cedula+"', '"+nombre+"', '"+apellido+"', '"+direccion+"')");
  113.  
  114. if (columna == 1)
  115. {
  116. System.out.println("Se agregó el registro de manera exitosa");
  117. }
  118. else
  119. {
  120. System.out.println("Ocurrio un problema al agregar el registro");
  121. }
  122.  
  123. }
  124. catch (Exception e)
  125. {
  126. System.out.println("Error de conexion");
  127. }
  128. finally
  129. {
  130. try {
  131. if(connection!=null)
  132. {
  133. connection.close();
  134. }
  135. } catch (SQLException ex) {
  136. Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
  137. }
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement