Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inserting data of radio buttons in mysql throwing exception  RSS feed

 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this form and when I am inserting data from it to mysql, a message is printed out Got an exception!
Incorrect integer value: 'Single' for column 'room_type' at row 1


Here is my code:



And here is my database table:





Screenshot-(271).png
[Thumbnail for Screenshot-(271).png]
the form
 
Peter Muster
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless I'm mistaken, rtype and vtype are Strings but you declared the database fields you want to write them into as tinyints. The mysql error code is actually pretty precise on this. "Incorrect integer value: 'Single' for column 'room_type' at row 1". You cannot insert "Single" (which is the roomtype-String) as an Integer.
 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Christian Pflugradt Okay, but shouldn't radio buttons be stored as tinyints(1) in the database? Like a boolean
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Certainly if you want to store a value which is either true or false in a database column, then tinyint(1) is perfectly suitable for that. But it appears from your code that you want to store things like "Suite" into that column. That isn't a value which is either true or false and as Christian already pointed out, tinyint(1) is totally unsuitable for that.

So you should decide what you actually want to store in the database. It seems like in your mind you are mixing up the GUI (which has several radio buttons) with the database (which only has one column to store whatever that group of radio buttons means).
 
Peter Muster
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sophia Green wrote:@Christian Pflugradt Okay, but shouldn't radio buttons be stored as tinyints(1) in the database? Like a boolean

You aren't storing the state of one radio button in the database but you set a String field depending on which radio button is selected:

You are storing this String variable in the database so you will need a varchar field.

This does not mean that this is the best solution. You have multiple choices here.
- You could store each individual radio button state as a tinyint in your table, that means you have one tinyint column for each radio button in your mask
- You can store the state of your radio button group in one int field
- You can store the state of your radio button group in one String field

The first option, storing each radio button in one column is not a way I would recommend here because they are all part of a group. You cannot select both "Deluxe" and "Suite", it'll be exactly one of the five room types available. It won't make sense to store them separately if you have to read and evaluate them all at the same time.

The disadvantage of the second option, using numbers instead of Strings, would be that no one knows what a 3 in that field could mean - perhaps it's equivalent to "Deluxe", who knows?
You could either create a second table that keeps a ROOM_ID and a ROOM_DESCRIPTION for instance to tell everyone that ROOM_ID 3 means "Deluxe" and ROOM_ID 2 means "Suite". That may be too much depending on the size of your database model and content or it may be not. I personally prefer this version if I can expect thousands of entries in your booking table. With a JOIN-statement you can easily get the room description for any row in your table and a number will require less space than a String. Even more important, a database-side mapping would avoid any irritating writing mistakes in the field. What if some rows are inserted that contain "Delux" instead of "Deluxe" because of a writing mistake in another Java class or because a Third Party company with an interface to your database will send incorrect data? This will probably lead to incorrect behaviour in your application and if any other processes than your application are involved you have no way to guarantee that the field will always be set correctly.

To get your (Java) version working, which is perfectly fine for a small application in my opinion, you need to change the columns in your table declaration as follows.

varchar(20) is only a suggestion, obviously it'll depend as how large the Strings can be that you want to store in these fields.
 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Christian Pflugradt Thanks a lot! It works fine now :-)
Could you please check the time field in my form? The correct time is not being entered in the database

Screenshot-(276).png
[Thumbnail for Screenshot-(276).png]
Form with data to insert in mysql
Screenshot-(277).png
[Thumbnail for Screenshot-(277).png]
How it appears in the database
 
Peter Muster
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sophia Green wrote:@Christian Pflugradt Thanks a lot! It works fine now :-)
Could you please check the time field in my form? The correct time is not being entered in the database

Sorry, I can't really tell you, I haven't used java.sql.time yet. If I was you I'd debug the program and set a break point at the line where you create sqlTime. Check if swingTime.getCalendarField() really returns the value you want to have in the database and what parameter java.sql.time expects in the constructor.
 
Dave Tolls
Ranch Hand
Posts: 2835
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you setting the time?
How does that value (note, the 'PM' is incorrect with a 24 hour clock) get from the Swing field to the INSERT statement?
As part of that I would suggest debugging it, so you can see what values are being passed around.
 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed it by converting this to milliseconds since midnight (which is what the Time class works in), so need to break the String down into hours and minutes and convert these to milliseconds. Here is what I did:



Credits: an answer from StackOverflow:-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!