aspose file tools*
The moose likes JDBC and the fly likes Prepared  statements ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Prepared  statements ?" Watch "Prepared  statements ?" New topic
Author

Prepared statements ?

Vanchi Nathan
Ranch Hand

Joined: Feb 24, 2004
Posts: 107
Hello,

Pl. give your tips...

The above code work well by inserting data to the PSTData table, but when i use a PreparedStatement to do the insert. It doesn't work, it gives SQLException.

Why? Is it wrong to use a PreparedStatement for a insert query?

Thanks in advance,
-
vanchin


Best regards,<br /> <br />vanchin
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

No. Can you post your code?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Vanchi Nathan
Ranch Hand

Joined: Feb 24, 2004
Posts: 107
Hello,

here follows the code:


This gives me an SQL exception...

thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

...and what's the SQLException?
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104
You're not using the prepared statement correctly.
The statement should not be:
"INSERT into PSTData values ?"
it should be:
"INSERT into PSTData values (?, ?, ?, ?, ?)"
Vanchi Nathan
Ranch Hand

Joined: Feb 24, 2004
Posts: 107
Hi Lu & Paul, Thanks for the syntax, my prepared statement was wrong...

got it done...
arnel nicolas
Ranch Hand

Joined: Dec 16, 2003
Posts: 149
Just a tip, if your code goes that way, you may try using the addBatch method of the prepared statement. Your code will have a great impact on your network, since you will send sql statement back and forth from your server everytime your loop is executed.

At the end of your loop, you may issue the execute() statement to execute your sql statements once.
Vanchi Nathan
Ranch Hand

Joined: Feb 24, 2004
Posts: 107
hi Arnel,

Sorry, I am not quite sure of batch methods. Can you guide me to some resource materials that shows how to...

thanks
arnel nicolas
Ranch Hand

Joined: Dec 16, 2003
Posts: 149
Hi Vanchi,

Your code as modified using addBatch() method.



Thats quite easy isn't it.


Happy coding...
[ July 14, 2004: Message edited by: arnel nicolas ]
Vanchi Nathan
Ranch Hand

Joined: Feb 24, 2004
Posts: 107
Hello Arnel,

This means that all the N-number of queries of the same type are sent to the database server at the same time to get executed. Hence the connect to the DB server is done only one time.

Is there any limitations to the batch processing?

thanks in advance...
 
wood burning stoves
 
subject: Prepared statements ?
 
Similar Threads
PreparedStatement with dynamic variable?
SQL Column Date Type insertion problem
storing date in database
doubt regarding default values
How to Insert Data into a table in mysql database