lunes, 20 de febrero de 2012

Conexion Java con PostgreSQL

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class JPostgreSQLFunctionCreator extends javax.swing.JFrame
{
    JLabel lblserver = new JLabel("Servidor");
    JLabel lbldatabase = new JLabel("Base de Datos");
    JLabel lbluser = new JLabel("Usuario");
    JLabel lblpwd = new JLabel("Contraseña");
   
    JTextField txtserver = new JTextField();
    JTextField txtdatabase = new JTextField();
    JTextField txtuser = new JTextField();
    JPasswordField txtpwd = new JPasswordField();
       
    JButton btnConectar = new JButton("Conectar");
    JButton btnSalir = new JButton("Salir");
   
    JLabel lblresultado = new JLabel("Sin conexión");
   
    JTextArea txtquery = new JTextArea();
    JTextArea txtresultado = new JTextArea();
   
    JScrollPane sp_query = new JScrollPane();
    JScrollPane sp_resultado = new JScrollPane();
   
    JLabel lblentrada = new JLabel();
    JLabel lblsalida = new JLabel();
   
    public JPostgreSQLFunctionCreator()
    {
        initComponents();
    }
   
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                // Aplicar HiFiLookAndFeel a nuestra aplicación
                try
                {
                    UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
                }
                catch(Exception e)
                {
                        try
                        {
                            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                        }
                        catch(Exception err)
                        {
                            System.out.println("Error loading myXPStyleTheme: " + err.toString());
                        }
                }
               
                new JPostgreSQLFunctionCreator().setVisible(true);
            }
        });
    }
   
    public void Conectar()
    {
        System.out.println("Checking if Driver is registered with DriverManager.");
       
        try
        {
            Class.forName("org.postgresql.Driver");
        }
        catch(ClassNotFoundException cnfe)
        {
            System.out.println("Couldn't find the driver!");
            System.out.println("Let's print a stack trace, and exit.");
            cnfe.printStackTrace();
            //System.exit(1);
        }
       
        System.out.println("Registered the driver ok, so let's make a connection.");
        Connection c = null;
 
        try
        {
            // The second and third arguments are the username and password,
            // respectively. They should be whatever is necessary to connect
            // to the database.
            c = DriverManager.getConnection("jdbc:postgresql://" + txtserver.getText() + "/" + txtdatabase.getText(),
                                    txtuser.getText(), txtpwd.getText());
        }
        catch (SQLException se)
        {
            System.out.println("Couldn't connect: print out a stack trace and exit.");
            se.printStackTrace();
            //System.exit(1);
        }
 
        if (c != null)
            //System.out.println("Hooray! We connected to the database!");
            lblresultado.setText("Conexión satisfactoria");
        else
            //System.out.println("We should never get here.");
            lblresultado.setText("No se pudo conectar, intente de nuevo");
    }
   
    public void initComponents()
    {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("JConsolidado");
        setResizable(false);
        getContentPane().setLayout(null);
        //setBounds(450,170,500,500);
        setBounds(450,170,330,230);
       
        lblserver.setBounds(10,10,100,20);
        txtserver.setBounds(105,10,150,20);
        lbldatabase.setBounds(10,40,100,20);
        txtdatabase.setBounds(105,40,150,20);
        lbluser.setBounds(10,70,100,20);
        txtuser.setBounds(105,70,150,20);
        lblpwd.setBounds(10,100,100,20);
        txtpwd.setBounds(105,100,150,20);
        btnConectar.setBounds(105,130,70,20);
        btnSalir.setBounds(185,130,70,20);
        lblresultado.setBounds(10,160,250,20);
       
        lblentrada.setBounds(10, 190, 250, 20);
        sp_query.setBounds(10,320,450,120);
       
        lblsalida.setBounds(10,350,450,120);
        sp_resultado.setBounds(10,350,450,120);
       
        sp_query.setViewportView(txtquery);
        sp_resultado.setViewportView(txtresultado);
       
        add(lblserver);
        add(txtserver);
        add(lbldatabase);
        add(txtdatabase);
        add(lbluser);
        add(txtuser);
        add(lblpwd);
        add(txtpwd);
        add(btnConectar);
        add(btnSalir);
        add(lblresultado);
        //add(sp_query);
        //add(sp_resultado);
       
        btnConectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConectarActionPerformed(evt);
            }
        });
       
        btnConectar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnConectarKeyPressed(evt);
            }
        });
       
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });
    }
   
    private void btnConectarActionPerformed(java.awt.event.ActionEvent evt)
    {
        //GEN-FIRST:event_jButton1ActionPerformed
        // TODO: Agrege su codigo aqui:
        Conectar();
    }//GEN-LAST:event_jButton1ActionPerformed
   
    private void btnConectarKeyPressed(java.awt.event.KeyEvent evt)
    {
        //GEN-FIRST:event_jButton4KeyPressed
        // TODO: Agrege su codigo aqui:
       
        int KeyCode = evt.getKeyCode();
               
        if(KeyCode == 10)
        {
            Conectar();
        }
    }//GEN-LAST:event_jButton4KeyPressed
   
    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt)
    {
        //GEN-FIRST:event_jButton1ActionPerformed
        // TODO: Agrege su codigo aqui:
        JOptionPane.showMessageDialog(null, "Thank you for use this app, for more information: \n" + "           ingenierojosefrancisco@gmail.com", "Thank you", JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);
    }//GEN-LAST:event_jButton1ActionPerformed
}

No hay comentarios:

Publicar un comentario