I am inserting data into a form to MySQL database but when I am setting a date in the date field(using JCalendar/JDateChooser) in the form, I am getting a Data Truncation error like Got an exception! Data truncation: Incorrect date value: 'Mar 10, 2016' for column 'arrival_date' at row 1 as the date I entered was that of Mar 10, 2016.
Here is my code:
Connection conn = null;
// the mysql insert statement
String insertTable = "INSERT INTO booking" +
"(fname,lname,contact,email,address, num_persons, arrival_date,departure_date,arrival_time,room_type,view_type,roomno,room_facility )VALUES"
+ "(? , ? , ? , ?,? , ? , ? , ?,? , ? , ? , ?,?)";
try {
// create a mysql database connection
Class.forName("com.mysql.jdbc.Driver").newInstance();
String connectionUrl = "jdbc:mysql://localhost:3306/testing?autoReconnect=true&useSSL=false";
String connectionUser = "root";
String connectionPassword = "admin";
conn = (Connection) DriverManager.getConnection(connectionUrl, connectionUser,connectionPassword);
// create the mysql insert preparedstatement
PreparedStatement preparedStmt = (PreparedStatement) conn.prepareStatement(insertTable);
preparedStmt.setString(1,fnameField.getText() );
preparedStmt.setString(2,(lnameField.getText()));
preparedStmt.setString(3,(contactField.getText()));
preparedStmt.setString(4,(emailField.getText()));
preparedStmt.setString(5,(txtadd.getText()));
preparedStmt.setString(6,(spinner.getValue().toString()));
preparedStmt.setString(7,((JTextField)dateChooser1.getDateEditor().getUiComponent()).getText());
preparedStmt.setString(8,((JTextField)dateChooser2.getDateEditor().getUiComponent()).getText());
preparedStmt.setString(9,(timeSpinner.getValue().toString()));
preparedStmt.setString(10,rtype);
preparedStmt.setString(11,vtype);
preparedStmt.setString(12,(txtroomnum.getText()));
preparedStmt.setString(13,room_facility);
// execute the preparedstatement
preparedStmt.executeUpdate();
conn.close();
And here is my table for the database:
create table booking(
booking_id int unsigned auto_increment not null,
fname varchar(50) not null,
lname varchar(50) not null,
contact int,
email varchar(75),
address varchar(200),
num_persons int,
arrival_date date,
departure_date date,
arrival_time time,
room_type tinyint(1) not null,//for radiobuttons
view_type tinyint(1) not null,//for radiobuttons
roomno int,
room_facility tinyint(1) not null,//for multiple selection of checkboxes(not sure if the data type is correct here)
primary key(booking_id)
);
Here is my form to show the fields and the format in which the date appears :
