Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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
[1227 byte] By [airrazor] at [2007-11-11 7:00:12]
# 1 Re: For Mysql queries or any database
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)

:)
vsorc at 2007-11-11 23:47:57 >
# 2 Re: For Mysql queries or any database
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'");
mysqlautobackup at 2007-11-11 23:48:57 >
# 3 Re: For Mysql queries or any database
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?
airrazor at 2007-11-11 23:50:01 >
# 4 Re: For Mysql queries or any database
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?
airrazor at 2007-11-11 23:51:03 >
# 5 Re: For Mysql queries or any database
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.
aniseed at 2007-11-11 23:52:07 >
# 6 Re: For Mysql queries or any database
you may want to add more exist live connections to your application , try this tool :

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

Regards
btree at 2007-11-11 23:53:06 >
# 7 Re: For Mysql queries or any database
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?
airrazor at 2007-11-11 23:54:04 >
# 8 Re: For Mysql queries or any database
hi :)

in this u hve to use

st.executeUpdate insted of st.executeQuery

this will solve the problem
vsorc at 2007-11-11 23:55:08 >