"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.
"privilege" comes from the Latin words for "private" and "law" (legal) and dates to feudal times. To "claim privilege" meant that you were above the laws that applied to the common people.
posted 4 years ago
Tim Cooke wrote:What errors? Can you be more specific?
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.
posted 4 years ago
This is the final code and it works
Now I stil have a question: How to insert many query ?
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.