Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void conexion()
- {
- if (connection != null)
- {
- return;
- }
- String url = "jdbc:postgresql://localhost:5432/ConexionJAVA";
- String user="postgres";
- String password = "sekizou123";
- try
- {
- try {
- Class.forName("org.postgresql.Driver").newInstance();
- } catch (InstantiationException | IllegalAccessException ex) {
- Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
- }
- connection = DriverManager.getConnection(url, user, password);
- if (connection != null)
- {
- //System.out.println("Conectando a Base de Datos...");
- JOptionPane.showMessageDialog(null, "Conectando a Base de Datos...", "Mensaje de Conexión", PLAIN_MESSAGE);
- }
- }
- catch (ClassNotFoundException | SQLException e)
- {
- //System.out.println("Problemas de Conexión");
- JOptionPane.showMessageDialog(null, "Problemas de Conexión", "Mensaje de Conexión", WARNING_MESSAGE);
- }
- }
- public void Actualizar()
- {
- conexion();
- String CadenaCedula=txtced.getText();
- int cedula=Integer.parseInt(CadenaCedula);
- String nombre=txtnom.getText();
- String apellido=txtape.getText();
- String direccion=txtdir.getText();
- try
- {
- String sql="UPDATE FROM RC SET(cedula="+cedula+"),(nombre="+nombre+"),(apellido="+apellido+"),(direccion="+direccion+")";
- st = connection.createStatement();
- st.executeUpdate(sql);
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if(connection!=null)
- {
- connection.close();
- }
- }
- catch (SQLException ex)
- {
- Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public void Eliminar() {
- conexion();
- String CadenaCedula=txtced.getText();
- int cedula=Integer.parseInt(CadenaCedula);
- try
- {
- st = connection.createStatement();
- st.executeUpdate("DELETE FROM RC WHERE cedula = " + cedula);
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if(connection!=null)
- {
- connection.close();
- }
- }
- catch (SQLException ex)
- {
- Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public void Insertar()
- {
- conexion();
- String CadenaCedula=txtced.getText();
- int cedula=Integer.parseInt(CadenaCedula);
- String nombre=txtnom.getText();
- String apellido=txtape.getText();
- String direccion=txtdir.getText();
- try
- {
- st = connection.createStatement();
- int columna = st.executeUpdate("INSERT INTO RC VALUES ('"+cedula+"', '"+nombre+"', '"+apellido+"', '"+direccion+"')");
- if (columna == 1)
- {
- System.out.println("Se agregó el registro de manera exitosa");
- }
- else
- {
- System.out.println("Ocurrio un problema al agregar el registro");
- }
- }
- catch (Exception e)
- {
- System.out.println("Error de conexion");
- }
- finally
- {
- try {
- if(connection!=null)
- {
- connection.close();
- }
- } catch (SQLException ex) {
- Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement