Hi, I'm creating a method to add an employee to the database, specifically to the table used, where one of its fields is the branch to which it belongs. In the event that the branch does not exist, I must insert it in the branch table.
The problem is that I miss a general error.
The code of the methods that intervene is:
public void aniadirEmpleado(Empleado arg0) throws ExcepcionDeAplicacion {
String sql1 = "Insert into sucursal values (?,?,?,?)";
String sql2 = "Insert into empleado values (?,?,?,?,?,?,?,?)";
Connection con =null;
PreparedStatement stm = null;
try{
con= DriverManager.getConnection(getPropiedad("url"));
con.setAutoCommit(false);
if(existeGenerico("sucursal","ID_SUCURSAL",arg0.getSucursal().getId())){
stm=con.prepareStatement(sql2);
stm.setString(1, arg0.getId());
stm.setString(2, arg0.getNombre());
stm.setString(3, arg0.getApellidos());
stm.setString(4, arg0.getTrabajo());
String sexo= (new StringBuffer().append(arg0.getSexo())).toString();
stm.setString(5, sexo);
stm.setDate(6, calendarToDate(arg0.getFechaNacimiento()));
stm.setDouble(7, arg0.getSalario());
stm.setString(8, arg0.getSucursal().getId());
stm.executeUpdate();
}else{
stm=con.prepareStatement(sql1);
stm.setString(1, arg0.getSucursal().getId());
stm.setString(2, "");
stm.setString(3, "");
stm.setString(4, "");
stm.executeUpdate();
stm=con.prepareStatement(sql2);
stm.setString(1, arg0.getId());
stm.setString(2, arg0.getNombre());
stm.setString(3, arg0.getApellidos());
stm.setString(4, arg0.getTrabajo());
String sexo= (new StringBuffer().append(arg0.getSexo())).toString();
stm.setString(5, sexo);
stm.setDate(6, calendarToDate(arg0.getFechaNacimiento()));
stm.setDouble(7, arg0.getSalario());
stm.setString(8, arg0.getSucursal().getId());
stm.executeUpdate();
}
stm.close();
con.commit();
}catch(Exception e){
e.printStackTrace();
try{
if(con != null){
con.rollback();
}
}catch(SQLException exc){
exc.printStackTrace();
}
throw new ExcepcionDeAplicacion();
}
finally{
try{
if(con != null){
con.close();
}
}catch (SQLException e1){
e1.printStackTrace();
throw new ExcepcionDeAplicacion();
}
}
}
public static Date calendarToDate(Calendar c){
Date d = new Date(c.getTimeInMillis());
return d;
}
public boolean existeGenerico(String tabla, String nombreCampo, String valorCampo){
String sql1 = "SELECT * FROM " + tabla + " WHERE ? = ?";
Connection con =null;
PreparedStatement stm = null;
boolean existe = false;
try{
con= DriverManager.getConnection(getPropiedad("url"));
stm=con.prepareStatement(sql1);
stm.setString(1, nombreCampo);
stm.setString(2, valorCampo);
ResultSet rs = stm.executeQuery();
if(rs.next()){
existe = true;
}else{
existe=false;
}
rs.close();
stm.close();
}catch (SQLException e){
e.printStackTrace();
}
finally
{
try
{
if(con != null) con.close();
}
catch(SQLException e2)
{
e2.printStackTrace();
}
}
return existe;
}
The error that jumps me is:
java.sql.SQLException: General error at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(Unknown Source) at sol.GestorBD.aniadirEmpleado(GestorBD.java:334)
With other methods the bd works correctly so it's connection problem.
Greetings and thanks in advance.