Since I got into the contents of JDBC in the external training, I will summarize it in my memorandum. The environment is as follows.
Description | |
---|---|
language | Java8 |
DB | OracleDB 11g |
APP server | Tomcat |
JDBC | ojdbc7 |
IDE | Eclipse Mars |
A standard API that links DB records and Java objects.
What is API? A "program for sharing software functions".
JDBC stores the value of each column in the record in a Java object and in a Java program It will be available.
JDBC itself is an interface. To use each JDBC function, a JDBC driver (JDBC implementation class) corresponding to the type of RDBMS is required separately! !!
The JDBC configuration is as follows.
Follow the procedure below to connect to the DB.
__ · Exception handling (SQLException) __ SQLException is thrown when an error occurs in the method that performs DB connection, SQL execution, etc. in JDBC.
__ ・ Disconnection process with DB __ Be sure to disconnect when the DB connection or SQL operation is completed. Because there is a risk of fatal trouble such as system outage. Executed by __Connection # close () method. __
1 and 2 are omitted. Just put it in.
To call a method of DB connection processing, use the JDBC driver class that implements the method. You need to register with the JVM.
To register, use Class # forName () method
Class#forName() Specify the JDBC driver class name in the argument. Register the class specified in the argument to the JVM. If the class specified in the argument does not exist Throw ClassNotFoundException as an exception class
For Oracle: oracle.jdbc.driver.OracleDriver
Once you register, you don't need to re-register until the JVM is finished. Exception handling needs to be described.
When connecting to DB, use DriverManager # getConnection () method.
DriverManager#getConnection() 3 arguments can be specified First argument: URL of the DB to connect to (host name, port number, DB name) Second argument: Username to connect Third argument: Password to connect
If the connection is successful, a Connection type object is returned as a return value. Connection itself is an h interface, and variables of that type implement the Connection interface. Refers to an object in the class. __ This object stores the connection information with the DB and is used when performing SQL operations on the DB. __
DBManager.java
/**
*Connect with DB
*
*@return DB connection
*@throws ClassNotFundException
*If the driver class cannot be found
*@throws SQLException
*When DB connection fails
*/
public static Connection getConnection() throws ClassNotFoundException,
SQLException {
//Register JDBC driver class in JVM
Class.forName("JDBC driver class name");
//Connect to DB
Connection connection = DriverManager.getConnection("URL", "User name", "password");
return connection;
}
/**
*DB connection disconnection
*
*@param connection
*Connection information with DB
*/
public static void close(Connection connection) {
if (connection != null) {
try {
//DB connection disconnection
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Recommended Posts