My question is this: How to insert data into both a parent and a daughter table.
My problem is that in my Java project I have as a parent class a Persona
and its two child tables Cliente
and Empleado
.
Class Cliente
has three extra data to the parent class that are fechaAlta
, two boleanos estudiante
and nuevoCliente
. That's where the problem arises, at the time of inserting only one person I have no problem, and my code is as follows:
private static final String SQL_INSERT = "INSERT INTO persona (nombre, apellidos, telefono, calle, colonia, idTipoAcceso) VALUES (?, ?, ?, ?, ?, ?)";
private static final Conexion conn = Conexion.getInstance();
@Override
public boolean insert(Persona c) {
PreparedStatement ps;
try {
ps = conn.getConn().prepareStatement(SQL_INSERT);
ps.setString(1, c.getNombre());
ps.setString(2, c.getApellidos());
ps.setString(3, c.getTelefono());
ps.setString(4, c.getCalle());
ps.setString(5, c.getColonia());
ps.setInt(6, c.getTipoDeAcceso());
if (ps.executeUpdate() > 0) {
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
conn.cerrarConexion();
}
return false;
}
But to insert from the Client class I do not know how to perform the query to the DB, since I must enter 7 data to the table Father Persona
and the three remaining to the daughter table Cliente
plus idCliente
that is autogenerated and the idPersona
that I do not know where to get it.
I hope you have given me to understand, greetings!