i can execute the following statement properly
stmt.execute("insert into computer values('fd','mic',123,'fds')");
but when i substitute the parameters into the statement errors
occour,what's wrong with the following statement?
*jmodel ,jbrand, jprice, jdesc are text field
stmt.execute("insert into computer values ('"
+jmodel.toString()+"','"+jbrand.toString()+"',"+Integer.parseInt(jprice.toString())+",'"+jdesc.toString()+"')");
TheOne - 18 Apr 2006 02:06 GMT
I think you will have to use prepared statement for using variables
like that.
> i can execute the following statement properly
>
[quoted text clipped - 8 lines]
>
> +jmodel.toString()+"','"+jbrand.toString()+"',"+Integer.parseInt(jprice.toString())+",'"+jdesc.toString()+"')");
Roy Epperson - 18 Apr 2006 05:11 GMT
> i can execute the following statement properly
>
[quoted text clipped - 8 lines]
>
> +jmodel.toString()+"','"+jbrand.toString()+"',"+Integer.parseInt(jprice.toString())+",'"+jdesc.toString()+"')");
To use ' " and several other characters in a value, they must be
escaped. Prepare statements take care of the escaping for you; but
require more code. But it simplifies the escaping and
PreparedStatements can be reused if doing a lot of inserts at one time.
There is a good example on the JDBC tutorial on java.sun.com. Watchout
- SQL indicies are 1 based not 0 based as in Java.....
sush - 18 Apr 2006 06:54 GMT
please check the spaces
Venkatesh - 18 Apr 2006 09:24 GMT
Hi,
The problem is because of usage of "toString()" .... U should use
getText() to get the text present in a text field.
Try executing this statement and it should work:
stmt.execute("insert into computer values ('"
+jmodel.getText()+"','"+jbrand.getText()+"',"+Integer.parseInt(jprice.getText())+",'"+jdesc.getText()+"')");
Nick - 18 Apr 2006 14:57 GMT
Vova Reznik - 18 Apr 2006 14:42 GMT
> i can execute the following statement properly
>
[quoted text clipped - 8 lines]
>
> +jmodel.toString()+"','"+jbrand.toString()+"',"+Integer.parseInt(jprice.toString())+",'"+jdesc.toString()+"')");
What was the error(s)?
Was it SQLException? (because fields in data base are too short and
string returning by JTextField::toString() usually very long)
Was it NumberFormatException?
(because you didn't override one of your text field toString() to
return numeric string?)
You have already one response about using text fields:
instead of toString use getText
Nick - 18 Apr 2006 14:57 GMT