![]() |
|
|
|||||||
![]() |
Java - what's wrong with this java statement? |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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()+"',"+In teger.parseInt(jprice.toString())+",'"+jdesc.toStr ing()+"')"); Nick |
|
|
|
|
#2 |
|
Posts: n/a
|
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()+"',"+In teger.parseInt(jprice.toString())+",'"+jdesc.toStr ing()+"')"); TheOne |
|
|
|
#3 |
|
Posts: n/a
|
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()+"',"+In teger.parseInt(jprice.toString())+",'"+jdesc.toStr ing()+"')"); > 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..... Roy Epperson |
|
|
|
#4 |
|
Posts: n/a
|
please check the spaces
sush |
|
|
|
#5 |
|
Posts: n/a
|
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()+"',"+Inte ger.parseInt(jprice.getText())+",'"+jdesc.getText( )+"')"); Venkatesh |
|
|
|
#6 |
|
Posts: n/a
|
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()+"',"+In teger.parseInt(jprice.toString())+",'"+jdesc.toStr ing()+"')"); > 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 Vova Reznik |
|
|
|
#7 |
|
Posts: n/a
|
thanks!
Nick |
|
|
|
#8 |
|
Posts: n/a
|
thanks!
Nick |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| xml file reading in java /deserialization in java | sowmyask | Software | 0 | 04-15-2009 08:03 AM |
| Live projects on C, C++, Java, .NET for final year students | sheetal7853@gmail.com | Software | 0 | 10-05-2008 04:20 AM |
| Java Error: java.lang.ClassFormatError: oracle/jdbc/ttc7/TTC7Protocol | Shalz | Software | 0 | 03-08-2008 06:05 PM |
| Java Beginners | Still Bill | Software | 3 | 02-19-2008 10:13 AM |
| netflix claims that wrong dvd was returned | Tonerhead | DVD Video | 0 | 06-24-2004 06:11 PM |