JDBC I will try to summarize about "JDBC" which is the range of java SE8 GOLD!
"JDBC" is a mechanism that allows you to access various databases from java, so-called interface.
The "polymorphism" that I learned when I was in silver is also applied to the database.
In other words, I can't think of anything else that can be done with this, such as Oracle, MySQL, PostgreSQL ... (ignorance) By preparing JDBC, you will be able to connect to these databases with one line __.
Here is what you need first. __ · JDBC driver __ that's all. Then, here we will do it in Eclipse, MySQL.
You can find it by searching for "jdbc driver [database name you want to use]". Download the one according to the database you use.
When you open the downloaded one, you will find the JAR file. (For mysql, it is "mysql-connector-java-〇〇.jar".) This is the body of the JDBC driver.
If left in place, java won't know where this is. So, in various messages, he complains, "Where is this?" Settings are also required to run in Eclipse.
Right-click the project → [Properties] → [Java Build Path] → [Library] tab → [Add External JAR]
In this procedure, specify the JAR file you downloaded earlier.
No Suitable Driver found for ... You may get an error like this. This is the state above, "Where is this?"
It's a bit off topic, but the above settings aren't enough when running in a Servlet.
You need to be careful about where you put the JAR files, rather than not enough.
[WEB-INF / lib]
Deploy the JAR file directly under this.
Create a project, package, class in Eclipse and write it.
String jdbc = "jdbc:mysql://localhost/test_db?serverTimezone=JST";
// jdbc: (database name): // localhost / (schema name)? (Timezone information)
First of all, this is it. This will identify the location of jdbc when using the Connection class described later.
try(Connection con = DriverManager.getConnection(jdbc,"root","pass");
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery(sql)){
It is in the middle of the try-with-resouece statement. Since jdbc related throws SQLException, it is enclosed in try.
You are connecting using a Connection object.
The argument of getConnection ()
is
Use this connection to prepare the Statement object for receiving SQL statements.
The SQL statement is finally executed on the third line. ResultSet is literally a "result set". ʻExecuteQuery ()` can receive the result of a SELECT statement. It will come in all at once, so let's take it out later.
And the open connection must be closed.
Normally, it is closed with close ()
in each class, but there is no problem if you release it with a try-with-resouce statement.
while(resultSet.next()){
System.out.println(resultSet.getString(2));
}
Depending on what SQL statement you threw, this is an example of a statement dealing with a result set.
First, next ()
on the first line is a method of the ResultSet class. This will move you to the "next line" of the result set you received.
If it is the first time, it will look at the "first line". From the second time onward, it will proceed line by line. It's convenient.
It works similar to Iterator's hasNext ().
Here, it will process as long as there is a line.
By the way, I decided to throw the SQL SELECT * FROM human
here.
Suppose the human table
has multiple columns, but the ___ second ___ has a column called name
. This column contains string data, so you can receive it as String.
Therefore, here it is ___ getString (2)
___.
Of course, if the data type and method you receive do not match, you will not be able to receive the data.
Methods are prepared according to each data type, so use them properly. You can also sort it on java after receiving it with getObject ()
.
Also, it is an integer of the argument of getXX (), but this is a "result set column index" as a supplement. Since I didn't specify the column this time, I chose getString (2)
because it is the second column.
However, if the SQL is SELECT name FROM human
, this number will be" 1 "because there is only name.
(Do not count from 0. 1 starts. Be careful.)
Recommended Posts