Java application이 client로서 어떻게 데이터베이스에 접근하는지 정의해놓은 표준 인터페이스이다.
Java Standard Edition에서 지원하는 스펙으로 Java의 기본 Class와 함수들로 제공되고 있다.
Database에 접속하고 데이터를 업데이트하거나 쿼리할 수 있는 메소드를 제공한다.
기본적으로 RDBMS를 상정하고 만들어졌다.
자바 개발자는 JDBC에 정의된 클래스와 함수만 사용하고 사용하는 DB에 따라 라이브러리만 바꿔 끼우면 코드의 수정 없이 같은 동작이 가능하도록 할 수 있다.
따라서 사용하는 DB에 따라 코드를 수정해 줄 필요가 없다.
기존의 DBMS를 사용하는 대부분의 시스템들이 자바로 작성되었기 때문에 JDBC가 나왔다. 하지만 점차 다른 언어들도 DBMS를 사용고자 하는 요구가 늘었고 프로그래밍 언어와 운영체제에 상관없이 DBMS에 독립적으로 사용할 수 있는 전송계층인 ODBC driver가 만들어졌다.
먼저 간단한 스키마와 테이블을 만들고 데이터를 넣는다.
CREATE SCHEMA `jdbc`; USE `jdbc`;
CREATE TABLE `product` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `updated_at` datetime NOT NULL,
`contents` varchar(2048) COLLATE utf8mb4_unicode_ci NOT NULL, `price` int NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `product` (`name`, `updated_at`, `contents`, `price`)
VALUES ('shoes1', '2022-08-01 01:00:00', 'This is shoes1', '10000');
INSERT INTO `product` (`name`, `updated_at`, `contents`, `price`)
VALUES ('shoes2', '2022-08-01 02:00:00', 'This is shoes2', '20000');
INSERT INTO `product` (`name`, `updated_at`, `contents`, `price`)
VALUES ('shoes3', '2022-08-01 03:00:00', 'This is shoes3', '30000');
INSERT INTO `product` (`name`, `updated_at`, `contents`, `price`)
VALUES ('shoes4', '2022-08-01 04:00:00', 'This is shoes4', '40000');
INSERT INTO `product` (`name`, `updated_at`, `contents`, `price`)
VALUES ('shoes5', '2022-08-01 05:00:00', 'This is shoes5', '50000');
INSERT INTO `product` (`name`, `updated_at`, `contents`, `price`)
VALUES ('shoes6', '2022-08-01 06:00:00', 'This is shoes6', '60000');
INSERT INTO `product` (`name`, `updated_at`, `contents`, `price`)
VALUES ('backpack', '2022-08-02 04:00:00', 'This is backpack', '1500');
INSERT INTO `product` (`name`, `updated_at`, `contents`, `price`)
VALUES ('shirt', '2022-08-03 05:00:00', 'This is shirt', '20000');
INSERT INTO `product` (`name`, `updated_at`, `contents`, `price`)
VALUES ('glasses', '2022-08-04 06:00:00', 'This is glasses', '10000');
MySQL 커넥터 사용을 위해 build.gradle의 dependencies에 mysql connector library를 추가한다.
dependencies {
implementation 'mysql:mysql-connector-java:8.0.30'
}
자바 코드로 product
테이블의 모든 레코드의 모든 속성을 불러온다.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class main {
public static void main(String[] args) {
try {
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jdbc", "root", "1234");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from product");
while (rs.next()) {
System.out.println(
rs.getInt(1) + " " + rs.getString(2) + " " + rs.getDate(3)
+ " " + rs.getString(4) + " " + rs.getInt(5));
}
con.close();
}catch (Exception e) {System.out.println(e);}
}
}
product
테이블과 동일한 결과를 얻을 수 있다.
1 shoes1 2022-08-01 This is shoes1 40000
2 shoes2 2022-08-01 This is shoes2 50000
3 shoes3 2022-08-01 This is shoes3 60000
4 shoes4 2022-08-01 This is shoes4 40000
5 shoes5 2022-08-01 This is shoes5 50000
6 shoes6 2022-08-01 This is shoes6 60000
7 backpack 2022-08-02 This is backpack 1500
8 shirt 2022-08-03 This is shirt 20000
9 glasses 2022-08-04 This is glasses 10000