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. 자바 프로젝트 생성




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