Inicio > java > Insertar Datos Java – Oracle

Insertar Datos Java – Oracle

Viernes, 5 de Junio de 2009 robertogt Dejar un comentario Ir a comentarios

CONEXION

public class MyConection {
public MyConection() {
}
public static Connection getConnection() {
Connection conn = null;
Driver d = null;
Class clase = null;

try {
clase = Class.forName(“oracle.jdbc.driver.OracleDriver”);
conn = DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:bases”,”system”,”cracker”);
} catch (Exception ex) {
ex.printStackTrace();
}
return conn;
}
}

INSERTAR

public String inserta() {
// Add event code here…
boton1.setValue(“hola mundo!!!!”);
MyConection miConexion = new MyConection();
Connection conn = miConexion.getConnection();
try{
Statement st = conn.createStatement();
PreparedStatement ps = conn.prepareStatement(“INSERT INTO estudiante VALUES(?,?)”);
//st.execute(“INSERT INTO estudiante VALUES(“+getT_id().getValue().toString()+”,’”+getT_nombre().getValue().toString()+”‘)”);
//st.close();
ps.setString(1,getT_id().getValue().toString());
ps.setString(2,getT_nombre().getValue().toString());
ps.executeUpdate();
ps.close();
conn.close();
}catch(Exception e){e.printStackTrace();}

return null;
}

CONSULTAS

public String consulta() {
// Add event code here…
b_buscar.setValue(“Buscando…”);
MyConection miConexion = new MyConection();
Connection conn= miConexion.getConnection();
Estudiante estudiante;
try{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(“select * from estudiante”);
while(rs.next()){
estudiante = new Estudiante(rs.getLong(“id”),rs.getString(“nombre”));
getListaEstudiantes().add(estudiante);
}

}catch(Exception ex){
ex.printStackTrace();
}
return null;
}

MODIFICAR

public String ModificarNombre() {
// Add event code here…
MyConection miConexion = new MyConection();
Connection conn = miConexion.getConnection();
String nombreNuevo = t_nombre.getValue().toString();
String idEst = t_id.getValue().toString();

String consulta =”UPDATE estudiante SET nombre=’”+nombreNuevo+”‘ WHERE id=”+idEst;
try{
PreparedStatement pst = conn.prepareStatement(consulta);
pst.executeUpdate();
pst.close();

}catch(Exception ex){
ex.printStackTrace();
}
System.out.println(“modificando!”);
return null;
}

Categories: java Tags:
  1. Sin comentarios aún.
  1. Sin trackbacks aún.