Forums Register Login

PreparedStatement

+Pie Number of slices to send: Send
Hi!
I have some errors in the follwing code.
Can you help me, please?

+Pie Number of slices to send: Send
What errors? Can you be more specific?
+Pie Number of slices to send: Send
Welcome to the JavaRanch, Dragu!

"It doesn't work" doesn't tell us anything. But in this case I can make a pretty good guess.

Your method is named "InsertRecords", but the SQL you are using doesn't insert anything. It's a SELECT statement, and SELECT retrieves data - it never inserts or updates data.

Just for information, it's considered bad practice to start the name of a method or class member with an uppercase letter (InsertRecords). The convention in Java is that class names start with an upper-case letter and instance names start with a lower-case letter. Java actually doesn't care, but failing to observe this convention can confuse some Java utilities.
+Pie Number of slices to send: Send
 

Tim Cooke wrote:What errors? Can you be more specific?




Ok.

First of all I did a code in Java that creates a database and then creates tables and inserts the information into the database.
This code use the method Statement.
The code has 3 method: public void CreateDataBase(), public void CreateTables(), public void InsertRecords():



That code works.


Second of all I wanted to do this code using methods PrepareStatement.
And the code is that I posted earlier.
I tryed to creates databases using PrepareStatement.
I tryed to creates tables using PrepareStatement.
I tryed to insert records using PrepareStatement.
+Pie Number of slices to send: Send
This is the final code and it works





Now I stil have a question: How to insert many query ?
+Pie Number of slices to send: Send
Hi dragu stelian,

First of all, a warm welcome to CodeRanch!

dragu stelian wrote:This is the final code and it works


Although it works there are still some issues with this code. Some of the issues are really minor issues (e.g. not following naming conventions for method names, having an obsolete call to Class.forName() if you're using a JDBC 4.0 driver), but other issues are really important and might even result in resource leaks. In your code you close the resources you have used, but these resources will not be closed when an error (e.g. SQLException) occurs prior to executing the close() method on the different resources. For example let's have a look at the InsertRecords() method: at line86 and line87 you are closing respectively the prepared statement and the connection. But if the invocation of the setString() method on line79 throws an SQLException, the "cleanup" code on lines line86 and line87 will not be executed!

Prior to Java 7 you have to use a try/finally block as this code snippet illustrates. As you can see closing the resources requires a bunch of additional code and is very cumbersome
Luckily closing resources is much easier when you are using Java 7 (or later)! With the try-with-resources statement, resources like the prepared statement and the connection will be closed automaticallyThis code snippet is equivalent with the previous one. See how nice and concise the code becomes when using this awesome Java 7 feature

dragu stelian wrote:Now I stil have a question: How to insert many query ?


Have a look at the addBatch() and executeBatch() methods of the PreparedStatement interface.

Hope it helps!
Kind regards,
Roel
1
+Pie Number of slices to send: Send
Coss-posted at Java Forums, where I gave much the same answer as Roel.
+Pie Number of slices to send: Send
Hi dragu stelian,

Please BeForthrightWhenCrossPostingToOtherSites! Many people (including me) don't like to waste their time and effort when your question was already answered on another site.

Kind regards,
Roel
A timing clock, fuse wire, high explosives and a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 665 times.
Similar Threads
Statement doesn´t add data from textField in table.
JDBC exception question
Java executeUpdate won't update!
jdbc questions
compiled or not compiled
Thread Boost feature
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:21:51.