aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes To make database input using Swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "To make database input using Swing" Watch "To make database input using Swing" New topic
Author

To make database input using Swing

maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
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?
Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
Posts: 118
you lack


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

maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
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

Joined: Apr 28, 2010
Posts: 134
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

Joined: Apr 10, 2010
Posts: 308
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...


Bye,
Nicola
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
The obvious first step would be to do some logging in the potvrdiMouseReleased method so that you know when it's invoked.


Ping & DNS - updated with new look and Ping home screen widget
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
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

Joined: Apr 28, 2010
Posts: 134
I did it!

Here is the method:



Thank you, guys!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

You forgot to close your statements.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I think that I did the bad copying (didn't get all brackets) because it works

Thanks, Rob
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Oct 27, 2005
Posts: 19541
    
  16

Exactly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To make database input using Swing
 
Similar Threads
multiple processes... only want one
what is the scope of JSP objects ???
No connection to database from JApplet on Web page
Enabled Web start in NetBeans, but lost database connection
JDBC Connection