DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2005
    Posts
    41

    Question For Mysql queries or any database

    Hi everyone.

    I finally have a connection with Mysql and a simple java program. I can select and display everything in the database in the command line console. I can change values in the database if I hard wire them into the code everytime. But I have a GUI interface that I want to be able to type in some text and then press a button and the database value is changed to what I wrote in the text box.

    I have a JTextbox, where I want to get my text from and I have a JButton to press to execute the change. Am I doing it right so far?? I would like to take what ever is in the textbox and insert it into the databse. How can I acheive this? I tried to use a String variable in the java code and insert it into the query statment but that just gives me an error message when I hit the button to insert it into the database.

    This is a snippet of code:

    String v="12";
    st.executeUpdate("UPDATE male " +
    "SET age= v"+
    "WHERE fname='richard'"
    );


    I can understand that the statement sends a string to MySql and I want to insert a variable so it wouldn't work. How would you then make this work??

    airrazor

  2. #2
    Join Date
    Aug 2005
    Posts
    6
    hi arrazor.....

    i think inside the table male u hve declared age as int
    then
    try this..


    String v="12";
    st.executeUpdate("UPDATE male SET age='"+Integer.parseInt((String)v)+"'
    +"WHERE fname='richard'");

    here u hve declared v as String....but u hve to convert it into int before giving this value inside a sql query

    for that we use this

    Integer.parseInt((String)v)


  3. #3
    Join Date
    Oct 2005
    Posts
    1
    Please try:

    if the field age is char or varchar:

    String v="12";
    st.executeUpdate("UPDATE male SET age='" + v + "' WHERE fname='richard'");

    if the field age is int:

    String v="12";
    st.executeUpdate("UPDATE male SET age=" + Integer.parseInt((String)v) + " WHERE fname='richard'");
    MySQL Auto Backup

    Professional MySQL Backup Automation Software on Windows

    http://www.mysqlautobackup.com

    http://www.swordsky.com

  4. #4
    Join Date
    Aug 2005
    Posts
    41

    thanks for your help

    thanks mysqlautobackup and vsorc,
    you were great help. What you suggested worked and I am able to query the database. If i were to query the database for a date and time would I have to do the same thing? If so what syntax would I use?

  5. #5
    Join Date
    Aug 2005
    Posts
    41

    INSERT syntax

    Hey guys,
    to follow up on your helps, I have another question. Am I able to do this:

    st.executeQuery("INSERT INTO Male" +
    "VALUES ('"+Integer.parseInt((String)acc)+"'"+
    ",'"+char.parseInt((String)ident)+"'"+
    ",'"+Date.parseInt((String)birthday)+"'"+
    ",'"+Time.parseInt((String)tfrom)+"'"+
    ",'"+float.parseInt((String)feecost)+"');");

    The Integer works fine but i get an error message for the char "class expected". Do I need to import something? How do the time,date and float look?

  6. #6
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    Quote Originally Posted by airrazor
    Hey guys,
    to follow up on your helps, I have another question. Am I able to do this:

    st.executeQuery("INSERT INTO Male" +
    "VALUES ('"+Integer.parseInt((String)acc)+"'"+
    ",'"+char.parseInt((String)ident)+"'"+
    ",'"+Date.parseInt((String)birthday)+"'"+
    ",'"+Time.parseInt((String)tfrom)+"'"+
    ",'"+float.parseInt((String)feecost)+"');");

    The Integer works fine but i get an error message for the char "class expected". Do I need to import something? How do the time,date and float look?
    char is not a class and you cannot invoke methods on it. Same for float. Date and Time do not have parseInt() methods. Take a look at PreparedStatement instead of writing code in this manner. PreparedStatement will simpify your problems considerably.
    Happiness is good health and a bad memory.

  7. #7
    Join Date
    Oct 2005
    Posts
    6
    you may want to add more exist live connections to your application , try this tool :

    http://www.smartvi.com/sep.htm

    Regards

  8. #8
    Join Date
    Aug 2005
    Posts
    41

    Question what do you mean?

    Hi aniseed,
    What do you mean using PreparedStatement will help simplify my problem? Won't I still have to write the same code when I create the PreparedStatements? Including the formatting?

  9. #9
    Join Date
    Aug 2005
    Posts
    6
    hi


    in this u hve to use

    st.executeUpdate insted of st.executeQuery

    this will solve the problem

Similar Threads

  1. Database Security General Discussion
    By Michael Tzoanos in forum Database
    Replies: 0
    Last Post: 04-12-2002, 11:19 AM
  2. SQL Tutorial (Answer Q's & post your reply)
    By bigbastard4 in forum Database
    Replies: 2
    Last Post: 05-16-2001, 06:24 PM
  3. queries
    By tom in forum Database
    Replies: 22
    Last Post: 04-27-2001, 12:48 AM
  4. "Suspected" DataBase - problem with Log file
    By Nirit Touboul in forum Database
    Replies: 0
    Last Post: 04-01-2001, 07:37 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links