Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

what's wrong with this java statement?

Thread view: 
Nick - 18 Apr 2006 01:59 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.