Netbeans - netbeans y postgresSQL

 
Vista:
sin imagen de perfil

netbeans y postgresSQL

Publicado por Jose  (1 intervención) el 09/03/2010 20:29:52
Buenas tardes, amigos necesito una gran ayuda de ustedes lo que pasa es que estoy haciendo un pequeño sistema en java y estoy usando netbeans 6.5 estoy trabajando en ubuntu y mi base de datos es postgres, nunca he manejado bases de datos en java y bueno, leyendo un poco supe (eso creo) hacer la conexion entre netbeans y postgres aqui les dejo el codigo:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* formulario.java
*
* Created on 09/03/2010, 01:38:05 PM
*/

package pruebacliente;
import java.sql.*;
/**
*
* @author jose
*/
public class formulario extends javax.swing.JFrame {

/** Creates new form formulario */
public formulario() {
initComponents();
}

/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("guardar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jLabel1.setText("Nombre:");

jLabel2.setText("Apellido:");

jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(164, 164, 164)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jTextField2))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(130, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(70, 70, 70))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

}

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new formulario().setVisible(true);
}

});

String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://localhost:5432/cliente";
String user = "postgres";
String password = "boeing727-200";

try{
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user , password);
Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT * FROM datos");

while (rs.next()){
System.out.println("NOMBRE " + rs.getString("nom_cli"));
System.out.println("APELLIDO " + rs.getString("apell_cli"));
}

stmt.close();
con.close();

}

catch ( Exception e ){
System.out.println(e.getMessage());
}
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration

}

creo que esta bien, pero ahora necesito que me digan: en donde debo colocar ese codigo??, yo lo tengo en el formulario donde quiero hacer la consulta , es decir estoy trabajando con SWING de netbeans y creo que alli debe ir el codigo de conectar la base de datos, lo otro que necesito es que me orienten : en donde debo colocar el codigo y cual es el codigo para que yo pueda ingresar el nombre y apellido de un cliente a mi base de datos??? mi base de datos se llama cliente, mi tabla datos y los campos son nom_cli y apell_cli lo que quiero aprender es como insertar informacion desde mi programa hecho en java a mi bases de datos que es postgres, ya la base de datos esta hecha por que la cree con DRUID osea el SCRIP lo creo DRUID por lo tanto solo necesito es saber cual es el codigo para registrar clientes a la base de datos gracias de verdad.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder