[자바 JDBC] 89. JDBC 연결방법

김건우's avatar
Mar 05, 2025
[자바 JDBC] 89. JDBC 연결방법

1. 더미 데이터 세팅

-- root로 접속한다 -- DB를 생성한다. create database store; use store; -- store 디비를 선택한다. CREATE TABLE store_tb( id int primary key auto_increment, name varchar(20), price int, qty int ); insert into store_tb(name, price, qty) values('사과', 1000, 50); insert into store_tb(name, price, qty) values('딸기', 2000, 50); commit;
 

2. 자바 프로젝트 생성

notion image
notion image
오른쪽에 코끼리 그림이 나오면 업데이트 해줘야함
오른쪽에 코끼리 그림이 나오면 업데이트 해줘야함
설치 됐는지 확인 하려면 라이브러리 확인
설치 됐는지 확인 하려면 라이브러리 확인

3. DB 연결 테스트

import java.sql.DriverManager; public class DBConnection { public static Connection getConnection() { String url = "jdbc:mysql://localhost:3306/store"; String username = "root"; String password = "bitc5600!"; try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection(url, username, password); return conn; } catch (Exception e) { throw new RuntimeException(e); } } }
Share article

gunwoo