• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

To make database input using Swing

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did it!

Here is the method:



Thank you, guys!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You forgot to close your statements.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic