⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coneccion.java

📁 ejemplo de java para conexion a base de datos
💻 JAVA
字号:
/* * coneccion.java * * Created on 16 de junio de 2004, 11:30 PM *//** * * @author  GeBuSe */import java.sql .*;import javax.swing.JOptionPane ;public class coneccion extends javax.swing.JDialog  {  private Connection conn; String nombre_clave;String nombre_usuario;String g;/** Creates new form coneccion */        public coneccion(java.awt.Frame parent, boolean modal){        super(parent, modal);        initComponents();        this.setBounds(0, 0, 500, 400);         String g;           g="jdbc:mysql://localhost/tarea";         nombre.setText(g);        //asegurar la coneccion....       }        public Connection recupera_coneccion(){     return(conn);           }                /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    private void initComponents() {//GEN-BEGIN:initComponents        botonconec = new javax.swing.JButton();        cancelar = new javax.swing.JButton();        nombre = new javax.swing.JTextField();        label2 = new java.awt.Label();        usuario = new javax.swing.JTextField();        clave = new javax.swing.JTextField();        label4 = new java.awt.Label();        label5 = new java.awt.Label();        getContentPane().setLayout(null);        addWindowListener(new java.awt.event.WindowAdapter() {            public void windowClosing(java.awt.event.WindowEvent evt) {                closeDialog(evt);            }        });        botonconec.setText("conectar");        botonconec.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                botonconecActionPerformed(evt);            }        });        getContentPane().add(botonconec);        botonconec.setBounds(30, 160, 90, 30);        cancelar.setText("cancelar");        cancelar.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                cancelarActionPerformed(evt);            }        });        getContentPane().add(cancelar);        cancelar.setBounds(160, 160, 100, 30);        nombre.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                nombreActionPerformed(evt);            }        });        getContentPane().add(nombre);        nombre.setBounds(10, 30, 210, 20);        label2.setText("datos de la base de datos");        getContentPane().add(label2);        label2.setBounds(240, 30, 130, 20);        getContentPane().add(usuario);        usuario.setBounds(20, 100, 90, 20);        clave.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                claveActionPerformed(evt);            }        });        getContentPane().add(clave);        clave.setBounds(20, 130, 90, 20);        label4.setText("nombre de usuario");        getContentPane().add(label4);        label4.setBounds(250, 100, 120, 20);        label5.setText("clave");        getContentPane().add(label5);        label5.setBounds(250, 130, 70, 20);        pack();    }//GEN-END:initComponents    private void nombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nombreActionPerformed        // Add your handling code here:    }//GEN-LAST:event_nombreActionPerformed    private void claveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_claveActionPerformed        // Add your handling code here:    }//GEN-LAST:event_claveActionPerformed    private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformeddispose();        // Add your handling code here:    }//GEN-LAST:event_cancelarActionPerformed    private void botonconecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonconecActionPerformed try{         nombre_usuario=usuario.getText();       nombre_clave=clave.getText();      g=nombre.getText();      cargaDriver();       conectar();      dispose(); } catch(Exception e){}          }//GEN-LAST:event_botonconecActionPerformed    /** Closes the dialog */    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog       setVisible(false);        dispose();    }//GEN-LAST:event_closeDialog        /**     * @param args the command line arguments     */    public static void main(String args[]) {        new coneccion(new javax.swing.JFrame(), true).show();    }public void cargaDriver() throws Exception    {        try        {            Class.forName("com.mysql.jdbc.Driver").newInstance();             System.out.println("Creada Instancia");        }        catch (Exception ex)        {            JOptionPane.showMessageDialog(null, "Error en la coneccion");        }    }        public void conectar() throws Exception    {        try        {                            conn = DriverManager.getConnection(g,nombre_usuario,nombre_clave);         JOptionPane.showMessageDialog(null, "Conectado");        }        catch (SQLException ex)        {            System.out.println("SQLException: " + ex.getMessage());             System.out.println("SQLState: " + ex.getSQLState());             System.out.println("VendorError: " + ex.getErrorCode());         }    }         // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JButton botonconec;    private javax.swing.JButton cancelar;    private javax.swing.JTextField clave;    private java.awt.Label label2;    private java.awt.Label label4;    private java.awt.Label label5;    private javax.swing.JTextField nombre;    private javax.swing.JTextField usuario;    // End of variables declaration//GEN-END:variables    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -