๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Database

[JAVA] JDBC MySQL ์—ฐ๋™ ๋ฐ CRUD ํ…Œ์ด๋ธ” ์ž‘์„ฑ

by soy๋ฏธ๋‹ˆ 2021. 9. 14.

 

1. Java ํ”„๋กœ์ ํŠธ MySQL ์—ฐ๋™

  1. JDBC ๋“œ๋ผ์ด๋ฒ„ ์„ค์น˜ : https://dev.mysql.com/downloads/connector/j/
 

MySQL :: Download Connector/J

MySQL Connector/J 8.0 is highly recommended for use with MySQL Server 8.0, 5.7 and 5.6. Please upgrade to MySQL Connector/J 8.0.

dev.mysql.com

 

2. JDBC ํŒŒ์ผ ์••์ถ• ํ•ด์ œ ํ›„ Java ํ”„๋กœ์ ํŠธ ์šฐํด๋ฆญ - Properties - Java Build Path - Libraries ์— mysql-connector-java-๋ฒ„์ „.jar ์ถ”๊ฐ€

 

3. Java ํ”„๋กœ์ ํŠธ ์‹คํ–‰

 

 

2. CRUD ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ (Insert, Select, Update, Delete)

DB๋ช… : DB01

ํ…Œ์ด๋ธ”๋ช… : TABLE01

VALUE TYPE
fullname varchar
age int
salary int

 

1. ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๊ฐ ์ˆซ์ž์— ๋งž๋Š” ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ

package testdb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
import java.sql.SQLException;
import java.sql.PreparedStatement;

public class Main {
	static String jdbc_driver = "com.mysql.cj.jdbc.Driver";
	static String jdbc_url = "jdbc:mysql://localhost:3306/DB01";

	public static void main(String[] args) throws ClassNotFoundException {
		while(true) {
			System.out.print("์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
			Scanner scanner = new Scanner(System.in);
			int n = scanner.nextInt();
			System.out.println(n);
			if(n==5) {
				System.out.println("์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
				break;
			}
			switch(n) {
			case 1:
				insert();
				break;
			case 2:
				select();
				break;
			case 3:
				update();
				break;
			case 4:
				delete();
				break;
			default:
				break;
			}
		}
	}

}

 

2. INSERT

public static void insert() {
		Connection con = null;
		PreparedStatement pstmt = null;
		
		System.out.println("Insert ์ฟผ๋ฆฌ");
		
		try {
			Class.forName(jdbc_driver);
			con = DriverManager.getConnection(jdbc_url, "root", "1234");
			String sql = "INSERT INTO TABLE01 VALUES (?,?,?)";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "ํ•˜๋งˆ");
			pstmt.setInt(2, 10);
			pstmt.setInt(3, 2000);
			pstmt.executeUpdate();
			int count = pstmt.executeUpdate();

			if(count == 0){
				System.out.println("์ž…๋ ฅ ์‹คํŒจ");
			}
			else{
				System.out.println("์ž…๋ ฅ ์„ฑ๊ณต");
			}
		} 
		catch( ClassNotFoundException e){
			System.out.println("์˜ค๋ฅ˜");
		}

		catch( SQLException e){
			System.out.println("์˜ค๋ฅ˜" + e);
		}

		finally{
			try{
				if(con != null && !con.isClosed()){
					con.close();
				}
				if(pstmt != null && !pstmt.isClosed()){
					pstmt.close();
				}
			}
			catch( SQLException e){
				e.printStackTrace();
			}
		}

	}
  • Connection : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ์ฒด
  • DriverManager : JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ํ†ตํ•ด Connection์„ ๋งŒ๋“œ๋Š” ์—ญํ• 
  • PreparedStatement : SQL ์ฟผ๋ฆฌ๋ฌธ์„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ณด๋‚ด๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
  • Statement : SQL ์ฟผ๋ฆฌ๋ฌธ์„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ณด๋‚ด๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
PreparedStatement Statement
- SQL ์ฟผ๋ฆฌ๋ฌธ์„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ณด๋‚ด๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
- try catch ๋ฌธ์œผ๋กœ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•ด์ค˜์•ผ ํ•จ
- ๋™์ ์ธ ์ฟผ๋ฆฌ๋ฌธ์— ์ ํ•ฉ
- ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋งŽ์€ ๊ฒฝ์šฐ์— ์ ํ•ฉ
- INSERT, UPDATE, DELETE
- ์ •์ ์ธ ์ฟผ๋ฆฌ๋ฌธ์— ์ ํ•ฉ
- SELECT

 

3. SELECT

public static void select() throws ClassNotFoundException {
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		System.out.println("Select ์ฟผ๋ฆฌ");
		

		try {
			Class.forName(jdbc_driver);
			con = DriverManager.getConnection(jdbc_url, "root", "1234");
			String sql = "SELECT * FROM TABLE01";
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);

			while(rs.next()) {
				String name = rs.getString("FULLNAME");
				int age = rs.getInt("AGE");
				int salary = rs.getInt("SALARY");

				System.out.println(name + " " + age + " " + salary);
			}
		} 
		catch (SQLException e) { 
			System.out.println("์˜ค๋ฅ˜ : " + e.getMessage()); 
		}

		catch (ClassNotFoundException e1) { 
			System.out.println("์˜ค๋ฅ˜ : " + e1.getMessage()); 
		}
		finally {
			if(rs != null) {
				try {
					rs.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(stmt != null) {
				try {
					stmt.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(con != null) {
				try {
					con.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

 

4. UPDATE

public static void update() {
		Connection con = null;
		PreparedStatement pstmt = null;

		System.out.println("Update ์ฟผ๋ฆฌ");
		
		try {
			Class.forName(jdbc_driver);
			con = DriverManager.getConnection(jdbc_url, "root", "1234");
			String sql = "UPDATE TABLE01 SET FULLNAME=?, AGE=? WHERE SALARY=?";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "๋ฉ”๋šœ๊ธฐ");
			pstmt.setInt(2, 20);
			pstmt.setInt(3, 4000);
			int count = pstmt.executeUpdate();
			System.out.println(pstmt);
			System.out.println("๋ณ€๊ฒฝ๋œ row : " + count);

		} 
		catch (SQLException e) { 
			System.out.println("์˜ค๋ฅ˜ : " + e.getMessage()); 
		}

		catch (ClassNotFoundException e1) { 
			System.out.println("์˜ค๋ฅ˜ : " + e1.getMessage()); 
		}
		finally {
			if(pstmt != null) {
				try {
					pstmt.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(con != null) {
				try {
					con.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

 

5. DELETE

public static void delete() {
		Connection con = null;
		PreparedStatement pstmt = null;
		
		System.out.println("Delete ์ฟผ๋ฆฌ");
		
		try {
			Class.forName(jdbc_driver);
			con = DriverManager.getConnection(jdbc_url, "root", "1234");
			String sql = "DELETE FROM TABLE01 WHERE FULLNAME=?";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "๋ฉ”๋šœ๊ธฐ");
			int count = pstmt.executeUpdate();
			System.out.println(pstmt);
			System.out.println("๋ณ€๊ฒฝ๋œ row : " + count);

		} 
		catch (SQLException e) { 
			System.out.println("์˜ค๋ฅ˜ : " + e.getMessage()); 
		}

		catch (ClassNotFoundException e1) { 
			System.out.println("์˜ค๋ฅ˜ : " + e1.getMessage()); 
		}
		finally {
			if(pstmt != null) {
				try {
					pstmt.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(con != null) {
				try {
					con.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

'Database' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

PostgreSQL ์‚ฌ์šฉํ•˜๊ธฐ  (0) 2021.09.15