![Antiguo](http://static.forosdelweb.com/fdwtheme/images/statusicon/post_old.gif)
04/06/2009, 04:10
|
| | Fecha de Ingreso: junio-2009
Mensajes: 59
Antigüedad: 15 años, 8 meses Puntos: 0 | |
Respuesta: como trabajar con mysql y netbeans Aqui te paso mi clase cliente con el metodo NuevoCliente que inserta en la BD package comercialelectrica;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Pablo
*/
public class Cliente {
public static final String DriverClass = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "mysql";
public static final String url = "jdbc:mysql://localhost/COMERCIAL_ELECTRICA";
static{
try {
Class.forName(DriverClass);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
System.out.println("No puedo cargar el driver JDBC de la BD");
}
}
public void NuevoCliente(String nombre,String dni,String apellidos,String direccion,String telefono,double fianza,String notas) throws SQLException{
Connection con = null;
try {
con = DriverManager.getConnection(url, user, password);
//con.setAutoCommit(false);
String sql = "INSERT INTO cliente(NOMBRE,APELLIDOS,DIRECCION,TELEFONO,FIANZA ,DNI,NOTAS) VALUES('"+nombre+"','"+apellidos+"','"+direccion+" ','"+telefono+"','"+fianza+"','"+dni+"','"+notas+" ')";
Statement stm = con.createStatement();
stm.executeUpdate(sql);
stm.close();
}
catch (SQLException ex) {
ex.printStackTrace();
throw new SQLException(ex);
}
finally{
try {
if (con != null) {
con.close();
}
}
catch (SQLException ex1) {
ex1.printStackTrace();
}
}
} |