Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PreparedStatement

 
dragu stelian
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I have some errors in the follwing code.
Can you help me, please?

 
Tim Cooke
Sheriff
Pie
Posts: 3209
142
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What errors? Can you be more specific?
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dragu stelian
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dragu stelian
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the final code and it works





Now I stil have a question: How to insert many query ?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave Tolls
Ranch Hand
Posts: 2110
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coss-posted at Java Forums, where I gave much the same answer as Roel.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic