Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: UPDATE SQL QUERY by getting user variables

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default UPDATE SQL QUERY by getting user variables

    Hi,

    I have a table called 'individuals'. It has 5 entities i.e. ID (int,) name (String), surname (String), Age (Int) and currencyBalance(double). These are stored in a mySql Database.

    I would like to make a GUI that allows the user to enter a value (as currency). This value is then added to the currencyBalance in the database.

    I was thinking of making 2 SQL queries. One that returns the balance as an integer and the other updates the Balance by adding the balance returned to the user input. However how do i enter the user data in the SQL query? The idea is to have a simple working application.

    I think the SQL Query queries should read something like this am i right?

    QUERY 1
    SELECT balance FROM individuals WHERE ID='1'

    Query 2
    UPDATE individuals (SELECT balance FROM individuals WHERE ID='1') SET balance=USERDATAGOESHERE WHERE name='George'

    Any better refinement on the logic? Is there a better and more effective way to do this?

    thanks


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,308
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: UPDATE SQL QUERY by getting user variables

    You probably don't need 2 queries. Based upon the information provided, you can just increment
    UPDATE individuals SET balance=balance+USERDATA WHERE ID='1';

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: UPDATE SQL QUERY by getting user variables

    Thanks for your reply.

    However, how can i get the user data (from a text field) and pass it in the SQL statement. How can i do this? Are you aware of any possible tutorial online or any other means?

    Thanks

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,308
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: UPDATE SQL QUERY by getting user variables

    Not sure exactly where the problem is, getting the text or creating the query? To get the text, just use getText, to create the query use string addition or Using Prepared Statements (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics)

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: UPDATE SQL QUERY by getting user variables

    Hi,

    so would something like this work:

    UPDATE individuals SET balance=balance+ jTextField1.getText() WHERE ID='1'; ?

    Also, will this automatically return the Balance from the database and then add it to the data ii get from the jTextField?

    I am not sure how this is done since i want the application to get the balance that is already in the database first.

    I hope this is final.

    thanks

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,308
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: UPDATE SQL QUERY by getting user variables

    Since balance is probably a number (floating point or otherwise), you need to convert the String to a value (Integer.parseInt or otherwise). This will increment the current balance. Its easiest to give it a try and see how it works, play with the syntax (as the above might not work as is) and the query capability - this will not only give you feel for how to query but help you learn to debug in the process.

Similar Threads

  1. update query is firing first then insert query
    By salmondavid88 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 8th, 2011, 10:15 AM
  2. Query
    By amol_kale in forum JDBC & Databases
    Replies: 0
    Last Post: February 18th, 2011, 06:04 AM
  3. SQL query not working
    By mjpam in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 28th, 2010, 05:15 PM
  4. [SOLVED] Interesing JButton Query
    By ravjot28 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 14th, 2010, 10:19 AM