Click to See Complete Forum and Search --> : For Mysql queries or any database


airrazor
10-08-2005, 01:21 AM
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

vsorc
10-10-2005, 09:11 AM
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)

:)

mysqlautobackup
10-12-2005, 04:37 AM
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'");

airrazor
10-15-2005, 01:38 PM
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
10-25-2005, 03:59 PM
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?

aniseed
10-29-2005, 10:16 AM
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.

btree
10-30-2005, 04:00 AM
you may want to add more exist live connections to your application , try this tool :

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

Regards

airrazor
11-02-2005, 05:25 AM
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?

vsorc
12-14-2005, 11:06 AM
hi :)


in this u hve to use

st.executeUpdate insted of st.executeQuery

this will solve the problem