Salve amici,
vorrei far eseguire più PreparedStatement nella stessa servlet, in questo modo:
codice:
conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(path);
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO utente VALUES(?,?,?,?,?)");
pstmt.setString(1,null);
pstmt.setString(2,request.getParameter("txtCognome"));
pstmt.setString(3,request.getParameter("txtNome"));
pstmt.setString(4,request.getParameter("txtProfessione"));
pstmt.setString(5,request.getParameter("txtMail"));
pstmt.executeUpdate();
// Recupera l'ID dell'ultimo utente inserito
int autoIncKeyFromApi = -1;
ResultSet rs = pstmt.getGeneratedKeys();
if (rs.next())
autoIncKeyFromApi = rs.getInt(1);
rs.close();
// SE INSERISCO QUESTA NUOVA PreparedStatement HO I PROBLEMI!!!!!!
pstmt.clearParameters();
pstmt.execute("INSERT INTO appartiene VALUES(?,?,?,?)");
pstmt.setInt(1,autoIncKeyFromApi);
pstmt.setInt(2,1);
pstmt.setInt(3,1);
pstmt.setString(4,"ciao");
pstmt.executeUpdate();
conn.close();
}
catch(ClassNotFoundException cnfe) {
out.println("Errore " + cnfe.getMessage());
}
catch(SQLException sqle) {
out.println("Errore " + sqle.getMessage());
}
Se inserisco la seconda PreparedStatement ho il seguente errore:
Errore Syntax error or access violation message from server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?)' at line 1"
Dov'è l'errore?????
Grazie