0

(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)

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Tanz
  • 41
  • 1
  • 1
  • 8
  • You should learn to use bind variables and prepared statements for a lot of reason. Next you should not use the `Date` class, it is long outdated and poorly designed. Use a `LocalDate`, this is the modern class representing a date without time of day. Pass that to your prepared statement and your issue will be gone. – Ole V.V. Jun 10 '18 at 15:32
  • Could you please post your code as text in the question (format it as code using the `{}` button)? It’s much easier to read and will allow someone to test it and to quote relevant parts of it, modified or unmodified, in an answer. You may also quote the error message as text in the question rather than posting an image. – Ole V.V. Jun 10 '18 at 15:36
  • Do you mean to say that I make an object of LocalDate class and then pass a value to that object? – Tanz Jun 10 '18 at 15:38
  • I'm not posting this from my pc so posting the code is a bit problematic for now – Tanz Jun 10 '18 at 15:39
  • Yes, as in [this answer](https://stackoverflow.com/a/38809770/5772882), for example. Since you get the date from a date chooser, you will probably need to convert first. `jDateChooser1.getDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()` (a bit wordy, but otherwise straightforward). – Ole V.V. Jun 10 '18 at 15:42
  • Okay thanks. I'll try that – Tanz Jun 10 '18 at 15:47

1 Answers1

1

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

StaticBeagle
  • 5,070
  • 2
  • 23
  • 34
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Jun 10 '18 at 15:29
  • I'll keep that in mind next time . Okay , it worked but My question is, in the table, I've set dob as date type and here dob is string type. How is it accepting a string value? Is that because of SimpleDateFormat? – Tanz Jun 10 '18 at 15:31
  • @Tanz In your code in the question the string concatenation `"','" + dob` etc. also converts the `Date` to a string using its `toString` method. The difference is that the code in the answer produces a string in the format that MySQL accepts. You should still prefer bind variables though, they will free you from depending on which string format your database expects. – Ole V.V. Jun 10 '18 at 15:47
  • @OleV.V. thanks for you suggestion. I don't get to work with dates that often so I just went with what I had off the top of my head.I'll revisit my answer soon to incorporate your feedback. – StaticBeagle Jun 10 '18 at 16:16