[JAVA] JDBC basics ① (connecting / disconnecting to DB) Summary memorandum

Introduction

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

What is JDBC

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.

jdbcmodel.png

Connection / disconnection to DB

Procedure for connecting to DB

Follow the procedure below to connect to the DB.

  1. Store JDBC driver in lib folder
  2. Add JDBC driver to build path
  3. Register the JDBC driver class in the JVM
  4. Execution of DB connection processing

__ · 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.

3. Register the JDBC driver class in the JVM

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.

4. Execution of DB connection processing

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. __

Implementation of connection processing

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

JDBC basics ① (connecting / disconnecting to DB) Summary memorandum
JDBC URL for connecting to DB in cluster configuration
rails db: 〇〇 Summary
Settings for connecting to MySQL with Spring Boot + Spring JDBC
BasicDataSourceFactory is ClassNotFoundException when connecting to DB on Tomcat 8