1
I have in my system the following code:
public int insert_dependente(Dependente dependente) {
ResultSet r;
int result = 0;
if (!(dependente == null)) {
try {
Connection conn = new Conexao().getConnection();
String sql = "insert into Dependente "
+ "(IdAssoc,"
+ "NomeDep,"
+ "SobrenomeDep,"
+ "RgDep,"
+ "CpfDep,"
+ " DtNascDep,"
+ " emailDep,"
+ " tipoDep,"
+ " DtCriacaoDep)"
+ " VALUES (" + dependente.getAssociado() + ","
+ "'" + dependente.getNome() + "',"
+ "'" + dependente.getSobrenome() + "',"
+ "'" + dependente.getRg() + "',"
+ "'" + dependente.getCpf() + "',"
//+ new java.sql.Date(dependente.getNascimento().getTime()).toString() + ","
+ "'" + dependente.getEmail() + "',"
+ "'" + dependente.getTipoDep() + "'";
//+ new java.sql.Date(new java.util.Date().getTime()).toString() + ")";
Statement state;
state = conn.createStatement();
state.execute(sql);
state.close();
String sql2 = "SELECT IdDep FROM Dependente WHERE CpfDep = " + dependente.getCpf();
state = conn.createStatement();
r = state.executeQuery(sql2);
while (r.next()) {
result = r.getInt("idDep");
}
r.close();
state.close();
conn.close();
} catch (SQLException ex) {
Logger.getLogger(DependenteDAO.class.getName()).log(Level.SEVERE, null, ex);
}
}
return result;
}
My problem is that when the system gets on the line
state.execute(sql);
The code does not run in my database, and soon after the system jumps straight to the line
return result;
I’ve tried everything, but I can’t find where my mistake is. Does anyone have any idea what it might be?
If you are going for the return it is because some exception is thrown, no? What is being written in the log?
– Bruno César
I even looked at the log to see if there was any Exception, but apache does not accuse any.
– Raphael Rosa
Placed a breakpoint in the catch to be able to follow and see what the exception is?
– Bruno César
Set up the log in your application because there is definitely an exception being released, as the coloegas mentioned. Without this exception it becomes difficult to know the problem. It can be by inconsistent data, some column or table with wrong names and so on.
– EduardoFernandes
@Raphaelrosa by the two commented lines is likely to be giving error, since it generates a statement invalid. I have not tested, but try to uncomment the lines and test again or, continuing error, then yes inform in your question the error.
– Bruno César
Please read this: http://pt.wikipedia.org/wiki/Injeção_de_SQL and then take a look at this: https://xkcd.com/327/
– Victor Stafusa