P.Szczur P.Szczur - 6 months ago 15
SQL Question

java msql insert record to table

I'm trying add record to database . I can't find mistake in this function . anyone can help ?

public static void insertTable(int idEmployee , String First_Name , String Last_Name , String Password , int Sex , String Mail , int Employee_idEmployee ) throws Exception {

Connection conn = (Connection) getConnection() ;
try {
PreparedStatement insertStatement = (PreparedStatement) conn.prepareStatement("INSERT INTO employee (?,?,?,?,?,?,?) VALUES (?,?,?,?,?,?,?)");
insertStatement.setInt(1, idEmployee);
insertStatement.setString(2, First_Name);
insertStatement.setString(3, Last_Name );
insertStatement.setString(4, Password);
insertStatement.setInt(5, Sex);
insertStatement.setString(6, Mail);
insertStatement.setInt(7 ,Employee_idEmployee);

insertStatement.close();
conn.close() ;

System.out.println("it's ok");

} catch (SQLException e) {

System.out.println(e.getMessage());

}
}


and in Main

insertTable(4 , "test" , "test" , "test" , 1 , "test" , 4 );


this is prtSc table :

enter image description here

Answer

Specifying column names is part of the fix.

You are still missing the execution line

you have no insertStatement.execute(); call

Comments