For Mysql queries or any database
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:
st.executeUpdate("UPDATE male " +
"SET age= v"+
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??
i think inside the table male u hve declared age as int
st.executeUpdate("UPDATE male SET age='"+Integer.parseInt((String)v)+"'
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
if the field age is char or varchar:
st.executeUpdate("UPDATE male SET age='" + v + "' WHERE fname='richard'");
if the field age is int:
st.executeUpdate("UPDATE male SET age=" + Integer.parseInt((String)v) + " WHERE fname='richard'");
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?
to follow up on your helps, I have another question. Am I able to do this:
st.executeQuery("INSERT INTO Male" +
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.
Originally Posted by airrazor
Happiness is good health and a bad memory.
you may want to add more exist live connections to your application , try this tool :
what do you mean?
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?
in this u hve to use
st.executeUpdate insted of st.executeQuery
this will solve the problem
By Michael Tzoanos in forum Database
Last Post: 04-12-2002, 12:19 PM
By bigbastard4 in forum Database
Last Post: 05-16-2001, 07:24 PM
Last Post: 04-27-2001, 01:48 AM
By Nirit Touboul in forum Database
Last Post: 04-01-2001, 08:37 AM
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