Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To make database input using Swing

 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I'm trying to do the recording in the database using Swing. I want to ask user to answer on some math question by setting the value on slider. Then, when he clicks Submit button (I named it "potvrdi") the value from JSlider should be written in database. I wrote this in the init method:

and also:


It doesn't work. Where did I make a mistake?

Is it maybe problem with potvrdiMouseReleased method, should I use other method or to change this?
 
Aron Jhed Amiscosa
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you lack


another stuff. you are looking for an int input to DB right?
int doesnt need single quotes. (' ');

 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure that I understand. I already have execute (as executeUpdate) part in the method, do you mean that I should put execute it in the init?

What about this part with my SQL and my staff in while cycle? What to write there since I have only one SQL statement for inserting?
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this:

and now I can do the input to the database, but it writes many rows at once. I press the button only once and it makes many records with the same slider value. What could be the problem?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to insert the value on button click use the right event handler.

Could you please post the code that handles the button click event?
It seems that you are inserting values in your db table when you change the JSlider value instead...
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that it doesn't insert the value when I change slider's value since it inserts many same rows. It reacts on button. If I select 500 on slider and push the button, it inserts many rows that contain 500.

I didn't wrote button's click event How to write it?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maja neskovic wrote:I didn't wrote button's click event

Which event did you write then ? Which event did you assign to your button ? Which component did you assign the potvrdiStateChanged method to ?

You should use an ActionListener, and call addActionListener on that button.

 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The obvious first step would be to do some logging in the potvrdiMouseReleased method so that you know when it's invoked.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you click the button what happens?
i see MouseEvents, ChangeEvents but not the one you need: ActionEvent

Your button fires, among others, ActionEvents. To Handle an ActionEvent you should write an ActionListener, i mean a class that implements ActionListener. This class could be the same class in which you build the GUI or another one.

I write just a skeleton, an example of the first option.






 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did it!

Here is the method:



Thank you, guys!
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot to close your statements.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that I did the bad copying (didn't get all brackets) because it works

Thanks, Rob
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maja neskovic wrote:I think that I did the bad copying (didn't get all brackets) because it works

Thanks, Rob


No, Rob means you forgot to close the JDBC statement, stmt2.close() and all others.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic