Try DB connection with Java

How to operate DB in Java

Get the driver for MySQL for Java and add it to your project

Get the SQL driver

Access the following URL and set the select box under Select Operating System: to Platform Independent Download the zip and unzip it. (Download destination is OK anywhere) SQL Driver スクリーンショット 2020-04-30 14.35.36.png

After unzipping, you will have a jar file.

Add to project

Add the unzipped SQL driver to your project. ʻDrag and drop to WebContent-> WEB-INF-> lib of the project created with Eclipse` It becomes as follows. スクリーンショット 2020-04-30 14.34.47.png

Set build path configuration

Open `` Project right-click-> Build path-> Build path configuration, select Add external JAR from the Library tab, and add the jar to WebContent-> WEB-INF-> lib` earlier. Select a file and let it be added. The screen when added is as follows. スクリーンショット 2020-04-30 14.45.16.png

DB connection check

Make a DB connection with the following implementation

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBManager {
    public static Connection getConnection(){
        Connection con = null; //Initialization
            con = DriverManager.getConnection("jdbc:mysql://localhost:port number/Database name","username","password");
            System.out.println("DB connection successful!!!");
            return con;
        }catch(ClassNotFoundException e){
            throw new IllegalMonitorStateException(); //Exception handling when there is no class
        } catch (SQLException e) {
            throw new IllegalMonitorStateException(); //Exception handling when an error occurs in SQL

  con = DriverManager.getConnection("jdbc:mysql://localhost:port number/Database name","username","password");
  System.out.println("DB connection successful!!!"); //It is OK even if it is not designed to output if it succeeds
  return con; //Returns the connection result


Class class

Class Class ... w

A method called getClass () is implemented in the java.lang.Object class, which is a superclass of all classes, and class information of the corresponding subclass is generated and held as an instance of the Class class when the subclass is instantiated. ..

Is it an image like a class for reading classes?

forName("com.mysql.jdbc.Driver"); The class name in parentheses is the SQL driver specified here.

forName () loads the DB driver class and runs the static initializer. All DB driver classes implement java.sql.Driver, and each DB driver class carries out the process of registering the DB driver class itself in the class that manages the driver called DriverManager class with the static initializer. I will.

It seems that forName will go to find the SQL driver and register it without permission Reference: Class class that seems to be known but not known (basic)

This completes the DB connection.

Recommended Posts

Try DB connection with Java
JAVA DB connection method
[Java] Connection with local DB (IntelliJ + SpringBoot)
[Template] MySQL connection with Java
Try gRPC with Java, Maven
Connect to DB with Java
[PDO → DB connection with docker, PHP]
Try using Redis with Java (jar)
Try bidirectional communication with gRPC Java
Try Java 8 Stream
db connection notes
Let's try WebSocket with Java and javascript!
DB connection method-JDBC
Try managing Java libraries with AWS CodeArtifact
Roughly try Java 9
Try using the Wii remote with Java
Try to link Ruby and Java with Dapr
Try to implement TCP / IP + NIO with JAVA
Try debugging a Java program with VS Code
Try DI with Micronaut
Install java with Homebrew
Try create with Trailblazer
Change seats with java
Install Java with Ansible
Try WebSocket with jooby
Comfortable download with JAVA
[Java] DB migration (Flyway)
Switch java with direnv
Try WildFly with Docker
Download Java with Ansible
[Docker] Connection with MySQL
Let's scrape with Java! !!
Build Java with Wercker
Try Java return value
Endian conversion with JAVA
HTTPS connection with Java to the self-signed certificate server
Try connecting to AzureCosmosDB Emulator for Docker with Java
Try building Java into a native module with GraalVM
Easy BDD with (Java) Spectrum?
Use Lambda Layers with Java
Java multi-project creation with Gradle
Getting Started with Java Collection
Try using RocksDB in Java
Java Config with Spring MVC
Basic Authentication with Java 11 HttpClient
Let's experiment with Java inlining
Run batch with docker-compose with Java batch
Rewrite Java try-catch with Optional
Install Java 7 with Homebrew (cask)
[Java] JSON communication with jackson
Try using GloVe with Deeplearning4j
Java to play with Function
Try using view_component with rails
Try scraping using java [Notes]
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
Enable Java EE with NetBeans 9
[Java] JavaConfig with Static InnerClass
[Beginner] Try to make a simple RPG game with Java ①
DB connection settings Spring-Boot