Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - what's wrong with this java statement?

 
Thread Tools Search this Thread
Old 04-18-2006, 01:59 AM   #1
Default 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()+"',"+In teger.parseInt(jprice.toString())+",'"+jdesc.toStr ing()+"')");


Nick
  Reply With Quote
Old 04-18-2006, 02:06 AM   #2
TheOne
 
Posts: n/a
Default Re: 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()+"',"+In teger.parseInt(jprice.toString())+",'"+jdesc.toStr ing()+"')");




TheOne
  Reply With Quote
Old 04-18-2006, 05:11 AM   #3
Roy Epperson
 
Posts: n/a
Default Re: 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()+"',"+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
  Reply With Quote
Old 04-18-2006, 06:54 AM   #4
sush
 
Posts: n/a
Default Re: what's wrong with this java statement?
please check the spaces



sush
  Reply With Quote
Old 04-18-2006, 09:24 AM   #5
Venkatesh
 
Posts: n/a
Default Re: 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()+"',"+Inte ger.parseInt(jprice.getText())+",'"+jdesc.getText( )+"')");



Venkatesh
  Reply With Quote
Old 04-18-2006, 02:42 PM   #6
Vova Reznik
 
Posts: n/a
Default Re: 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()+"',"+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
  Reply With Quote
Old 04-18-2006, 02:57 PM   #7
Nick
 
Posts: n/a
Default Re: what's wrong with this java statement?
thanks!


Nick
  Reply With Quote
Old 04-18-2006, 02:57 PM   #8
Nick
 
Posts: n/a
Default Re: what's wrong with this java statement?
thanks!


Nick
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46