Hi,
I try to insert a new post into a MySQL table.
I have done this before with success. This time I am trying to do it a little different.
In my other case I had a input from fields corresponding every column in my table.
Now I have input fields to, but not for the ID. This is set to be Auto Increment in MySQL.
The problem is that I get error when I try to insert a post. It complains about the number of columns.
I understand this to be it needs the exact amount of columns inserted as the table has.
Here is my insert code:
Any suggestions?Code:Connection connection = Connector.getConnection();
PreparedStatement ps = null;
String query = "INSERT INTO ContactTable(FirstName, LastName, Address, Zip, City, Phone, MobilePhone, Date)"
+" VALUES(?,?,?,?,?,?,?,?,?)";
try {
ps = connection.prepareStatement(query);
ps.setString(1, firstname);
ps.setString(2, lastname);
ps.setString(3, address);
ps.setString(4, zip);
ps.setString(5, city);
ps.setString(6, city);
ps.setString(7, phone);
ps.setString(8, mobile);
ps.setDate(9, date);
ps.executeUpdate();
} catch (SQLException ex){
Logger.getLogger(ContactTable.class.getName()).log(Level.SEVERE, null, ex);
} finally{
try {
connection.close();
} catch (SQLException ex) {
Logger.getLogger(ContactTable.class.getName()).log(Level.SEVERE, null, ex);
}
}
