-
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
-
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)
-
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'");
-
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?
-
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?
-
 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.
-
you may want to add more exist live connections to your application , try this tool :
http://www.smartvi.com/sep.htm
Regards
-
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?
-
hi 
in this u hve to use
st.executeUpdate insted of st.executeQuery
this will solve the problem
Similar Threads
-
By Michael Tzoanos in forum Database
Replies: 0
Last Post: 04-12-2002, 11:19 AM
-
By bigbastard4 in forum Database
Replies: 2
Last Post: 05-16-2001, 06:24 PM
-
Replies: 22
Last Post: 04-27-2001, 12:48 AM
-
By Nirit Touboul in forum Database
Replies: 0
Last Post: 04-01-2001, 07:37 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
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
|
Bookmarks