elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Security Series.XSS. [Cross Site Scripting]


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  AYUDA error de conexion a base de datos
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: AYUDA error de conexion a base de datos  (Leído 1,489 veces)
silfer_1908

Desconectado Desconectado

Mensajes: 15


Ver Perfil
AYUDA error de conexion a base de datos
« en: 9 Noviembre 2019, 01:27 am »

Al hacer una conexion con mi base de datos en posgresSQL , primero si la conecto aparece red conectada de manera correcta , pero al ingresar datos me aparece error de conexion
Me podrian ayudar?

Código
  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.logging.Level;
  7. import java.util.logging.Logger;
  8. import javax.swing.JOptionPane;
  9.  
  10. /**
  11.  *
  12.  * @author andy
  13.  */
  14. public class InterfazInicio extends javax.swing.JFrame {
  15.    private  Connection connection = null;
  16.    private ResultSet  rs = null;
  17.    private Statement s=null;
  18.    /**
  19.      * Creates new form InterfazInicio
  20.      */
  21.    public InterfazInicio()
  22.    {
  23.        initComponents();
  24.    }
  25.     public void conexion (){
  26.    if (connection != null){
  27.        return;
  28.    }
  29.    String url = "jdbc:postgresql://127.0.0.1:5432/neveria";
  30.    String password = "1234";
  31.    try {
  32.        Class.forName("org.postgresql.Driver");
  33.        connection = DriverManager.getConnection(url,"andres",password);
  34.        if(connection !=null){
  35.  
  36.            System.out.println("Base de datos conectada");
  37.        }        
  38.    }catch(Exception e){
  39.  
  40.        System.out.println("Base de datos no conectada");
  41.    }  
  42.  
  43. }
  44.  
  45.    @SuppressWarnings("unchecked")
  46.    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  47.    private void initComponents() {
  48.  
  49.        jTextField1 = new javax.swing.JTextField();
  50.        jButton1 = new javax.swing.JButton();
  51.        jTextField2 = new javax.swing.JTextField();
  52.        jButton2 = new javax.swing.JButton();
  53.  
  54.        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  55.  
  56.        jTextField1.addActionListener(new java.awt.event.ActionListener() {
  57.            public void actionPerformed(java.awt.event.ActionEvent evt) {
  58.                jTextField1ActionPerformed(evt);
  59.            }
  60.        });
  61.  
  62.        jButton1.setText("Registrar");
  63.        jButton1.addActionListener(new java.awt.event.ActionListener() {
  64.            public void actionPerformed(java.awt.event.ActionEvent evt) {
  65.                jButton1ActionPerformed(evt);
  66.            }
  67.        });
  68.  
  69.        jButton2.setText("Consultar");
  70.  
  71.        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  72.        getContentPane().setLayout(layout);
  73.        layout.setHorizontalGroup(
  74.            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  75.            .addGroup(layout.createSequentialGroup()
  76.                .addGap(32, 32, 32)
  77.                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  78.                    .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
  79.                    .addComponent(jTextField1))
  80.                .addGap(65, 65, 65)
  81.                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  82.                    .addComponent(jButton1)
  83.                    .addComponent(jButton2))
  84.                .addContainerGap(71, Short.MAX_VALUE))
  85.        );
  86.        layout.setVerticalGroup(
  87.            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  88.            .addGroup(layout.createSequentialGroup()
  89.                .addGap(27, 27, 27)
  90.                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  91.                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  92.                    .addComponent(jButton1))
  93.                .addGap(43, 43, 43)
  94.                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  95.                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  96.                    .addComponent(jButton2))
  97.                .addContainerGap(55, Short.MAX_VALUE))
  98.        );
  99.  
  100.        pack();
  101.    }// </editor-fold>                        
  102.  
  103.    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  104.        conexion();
  105.            try{
  106.                 String name = jTextField1.getText();
  107.                s = connection.createStatement();
  108.                int z = s.executeUpdate ("INSERT INTO cliente (nombre) VALUES('" +name+ "')");
  109.                if(z == 1){
  110.                  System.out.println("Se agrego registro correctamente");
  111.              } else{
  112.                  System.out.println("Intente de nuevo");
  113.              }  
  114.          }catch(SQLException e){
  115.              System.out.println("Error al conectar con la base de datos");
  116.          }
  117.    }                                        
  118.  
  119.    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
  120.        // TODO add your handling code here:
  121.    }                                          
  122.  
  123.    /**
  124.      * @param args the command line arguments
  125.      */
  126.    public static void main(String args[]) {
  127.        /* Set the Nimbus look and feel */
  128.        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  129.        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  130.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  131.          */
  132.        try {
  133.            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  134.                if ("Nimbus".equals(info.getName())) {
  135.                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
  136.                    break;
  137.                }
  138.            }
  139.        } catch (ClassNotFoundException ex) {
  140.            java.util.logging.Logger.getLogger(InterfazInicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  141.        } catch (InstantiationException ex) {
  142.            java.util.logging.Logger.getLogger(InterfazInicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  143.        } catch (IllegalAccessException ex) {
  144.            java.util.logging.Logger.getLogger(InterfazInicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  145.        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  146.            java.util.logging.Logger.getLogger(InterfazInicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  147.        }
  148.        //</editor-fold>
  149.  
  150.        /* Create and display the form */
  151.        java.awt.EventQueue.invokeLater(new Runnable() {
  152.            public void run() {
  153.                new InterfazInicio().setVisible(true);
  154.            }
  155.        });
  156.    }
  157.  
  158.    // Variables declaration - do not modify                    
  159.    private javax.swing.JButton jButton1;
  160.    private javax.swing.JButton jButton2;
  161.    private javax.swing.JTextField jTextField1;
  162.    private javax.swing.JTextField jTextField2;
  163.    // End of variables declaration                  
  164. }
  165.  
  166.  
  167.  


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Ayuda con conexion con Base de datos
Programación Visual Basic
Sakoda 1 1,390 Último mensaje 13 Septiembre 2005, 00:46 am
por Slasher-K
ayuda en conexion a base de datos con access
Java
mojittoo 1 2,122 Último mensaje 16 Junio 2009, 03:40 am
por h0oke
Ayuda con conexión a base de datos!
PHP
kodeone 3 2,143 Último mensaje 24 Febrero 2011, 17:38 pm
por kodeone
Error en la conexión a Base de datos
PHP
Azure 1 1,411 Último mensaje 16 Septiembre 2013, 01:30 am
por ~ Yoya ~
la conexion con la base de datos me da un error pero el programa sigue funcionando igual
Java
chupachota 3 3,410 Último mensaje 12 Noviembre 2021, 17:21 pm
por rub'n
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines