21. MySQL과 자바 연결

김해준's avatar
Jan 07, 2026
21. MySQL과 자바 연결

테이블 세팅

create database store; use store; CREATE TABLE store_tb ( id INT PRIMARY KEY, name VARCHAR(255), price INT, qty INT ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
notion image

프로젝트 세팅

notion image
notion image
데이터베이스 연결
package com.mtcoding.store; import java.sql.Connection; 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 { // new 클래스명(); 과 동일 Class.forName("com.mysql.cj.jdbc.Driver"); // conn = 프로토콜이 적용된 소켓 Connection conn = DriverManager.getConnection(url, username, password); System.out.println("성공"); return conn; } catch (Exception e) { e.printStackTrace(); } return null; } }

Insert

package com.mtcoding.store; import java.sql.PreparedStatement; import java.sql.SQLException; public class StoreApp { public static void main(String[] args) { // 1. DBMS와 연결된 소켓 var conn = DBConnection.getConnection(); String sql = "insert into store_tb(id, name, price, qty) values(?,?,?,?)"; try { // 2. 버퍼 달기 PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 1); pstmt.setString(2, "바나나"); pstmt.setInt(3, 100); pstmt.setInt(4, 10); // 3. 쿼리 전송 int result = pstmt.executeUpdate(); System.out.println("결과 : "+result); // 변경된 row count를 리턴한다 } catch (SQLException e) { e.printStackTrace(); } } }
notion image
 

코드 분리

package com.mtcoding.store; import java.sql.PreparedStatement; import java.sql.SQLException; // 요즘은 DAO같이 안부르고 추상적인 Repository로 적음 // store Table DB 전담 public class storeRepository { public int insert(int id, String name, int price, int qty) { // 1. DBMS와 연결된 소켓 var conn = DBConnection.getConnection(); String sql = "insert into store_tb(id, name, price, qty) values(?,?,?,?)"; try { // 2. 버퍼 달기 PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); pstmt.setString(2, name); pstmt.setInt(3, price); pstmt.setInt(4, qty); // 3. 쿼리 전송 int result = pstmt.executeUpdate(); // 결과값 반환 return result; } catch (SQLException e) { e.printStackTrace(); } return -1; } }
package com.mtcoding.store; public class StoreApp { public static void main(String[] args) { storeRepository repo = new storeRepository(); int result = repo.insert(3,"감자",1000,2); System.out.println("결과 : " + result); } }

updateOne

public int updateOne(int id, String name, int price, int qty) { // 1. DBMS와 연결된 소켓 var conn = DBConnection.getConnection(); String sql = "update store_tb set name=?, price=?,qty=? where id=?"; try { // 2. 버퍼 달기 PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, name); pstmt.setInt(2, price); pstmt.setInt(3, qty); pstmt.setInt(4, id); // 3. 쿼리 전송 int result = pstmt.executeUpdate(); // 결과값 반환 return result; } catch (SQLException e) { e.printStackTrace(); } return -1; }

deleteOne

public int deleteOne(int id) { // 1. DBMS와 연결된 소켓 var conn = DBConnection.getConnection(); String sql = "delete from store_tb where id=?"; try { // 2. 버퍼 달기 PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); // 3. 쿼리 전송 int result = pstmt.executeUpdate(); // 결과값 반환 return result; } catch (SQLException e) { e.printStackTrace(); } return -1; }
 
Share article