Tanz
Tanz

Reputation: 41

Inputting 'Date' from JFrame to MySQL database

(I'm using Netbeans IDE 8.2) I'm making a jFrame form to register a new user which will accept some values from the user at run time and insert them in a database that I've created in MySQL. Here's the design view (ignore the text boxes at the top right corner)- Design view

And here's the code I've written using JDBC- Code

Now, the problem I'm facing is in inputting the date of birth . It shows this error- The error

And this is the database and the table-

"Customer " table

All I get from the error is that MySQL can't accept the value entered and I'm guessing this might be due to some implicit data conversion that I'm unaware of. I can't understand how to fix this. Please help ! (I've not done any data validation yet)

Upvotes: 0

Views: 2127

Answers (1)

StaticBeagle
StaticBeagle

Reputation: 5175

Edit (Thanks to the suggestions by Ole V.V.)

Date is long outdated and poorly designed so a better choice would've been LocalDate. That being said the solution would be to use prepared statements and:

jDateChooser1.getDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

Please consider posting actual code as compared to a picture of the code. That being said, to insert Date literals to mysql, they need to be in the formats described here. So you just need to do a bit more manipulation on the date. Let's use YYYY-MM-DD. Assuming you are using a jDateChooser:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dob = sdf.format(jDateChooser1.getSelectedDate().getTime());

As a side note, right now you are building the query by hand and concatenating the values, this is open for SQL injection. Please consider using Prepared Statements

Upvotes: 1

Related Questions