what's wrong with this java statement?  
Author Message
Nick





PostPosted: 2006-4-18 8:59:00 Top

java-programmer, what's wrong with this java statement? 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





PostPosted: 2006-4-18 9:07:00 Top

java-programmer >> what's wrong with this java statement? I think you will have to use prepared statement for using variables
like that.

Nick wrote:
> 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()+"')");

 
Roy Epperson





PostPosted: 2006-4-18 12:11:00 Top

java-programmer >> what's wrong with this java statement? Nick wrote:
> 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()+"')");
>

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





PostPosted: 2006-4-18 13:55:00 Top

java-programmer >> what's wrong with this java statement? please check the spaces

 
 
Venkatesh





PostPosted: 2006-4-18 16:25:00 Top

java-programmer >> what's wrong with this java statement? 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()+"')");

 
 
Vova Reznik





PostPosted: 2006-4-18 21:43:00 Top

java-programmer >> what's wrong with this java statement? Nick wrote:
> 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()+"')");
>

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





PostPosted: 2006-4-18 21:57:00 Top

java-programmer >> what's wrong with this java statement? thanks!
 
 
Nick





PostPosted: 2006-4-18 21:57:00 Top

java-programmer >> what's wrong with this java statement? thanks!