Java Maria DB Library 설치
기본 패키지 생성

그림과 같이 패키지와 JavaClass 생성
Test
어노테이션으로 @Test를 붙이면 메서들 별로 실행이 가능하다
junit
java로 테스트할 수 있는 도구
import org.junit.jupiter.api.Test;
임포트를 하는데 위의 파일은 처음 Gradle로 프로젝트를 만들면 자동으로 설치되는 라이브러리
MariaDB url 프로토콜
String url = "jdbc:mariadb://아이피주소:포트번호/데이터베이스이름;package db;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnectionTest {
    // 리턴타입을 적을 수 없다.
    // 매개변수를 적을 수 없다.
    // @Test 붙이면 메서드 별로 실행 가능
    @Test
    public void getInstance_test(){
        String username = "root";
        String password = "1234";
        String url = "jdbc:mariadb://localhost:3306/cosdb";
        // 프로토콜이 적용된 소켓
        try {
            Connection conn = DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}Main
package db;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnection {
    public static Connection getInstance() {
        String username = "root";
        String password = "1234";
        String url = "jdbc:mariadb://localhost:3306/cosdb";
        // 프로토콜이 적용된 소켓
        try {
            Connection conn = DriverManager.getConnection(url, username, password);
            System.out.println("db connect success");
            return conn;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}메인에서 적용되는지 확인
사용해보기
데이터 베이스와 테이블 생성

BankApp
Insert
import db.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class BankApp {
    public static void main(String[] args) {
        Connection conn = DBConnection.getInstance();
        try {
            PreparedStatement pstmt = conn.prepareStatement("insert into account_tb(password,balance,created_at) values(?,?,now())"); // 버퍼
            pstmt.setString(1,"1234");// parameterIndex : 물음표의 순서(1부터 시작함)
            pstmt.setInt(2,1000);
            int num = pstmt.executeUpdate(); // flush
            System.out.println(num); // 영향받은 행의 수 리턴 오류는 -1
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}Update
import db.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class BankApp {
    public static void main(String[] args) {
        Connection conn = DBConnection.getInstance();
        try {
            String insert ="insert into account_tb(password,balance,created_at) values(?,?,now())";
            String update ="update account_tb set balance = balance + ? where number = ?";
            String delete ="";
            PreparedStatement pstmt = conn.prepareStatement(update); // 버퍼
//            pstmt.setString(1,"123456789101112131415");// parameterIndex : 물음표의 순서(1부터 시작함)
            pstmt.setInt(1,2000);
            pstmt.setInt(2,1);
            int num = pstmt.executeUpdate(); // flush
            System.out.println(num); // 영향받은 행의 수 리턴 오류는 -1
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}Delete
import db.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class BankApp {
    public static void main(String[] args) {
        Connection conn = DBConnection.getInstance();
        try {
            String insert ="insert into account_tb(password,balance,created_at) values(?,?,now())";
            String update ="update account_tb set balance = balance + ? where number = ?";
            String delete ="delete from account_tb where number = ?";
            PreparedStatement pstmt = conn.prepareStatement(delete); // 버퍼
//            pstmt.setString(1,"1234");// parameterIndex : 물음표의 순서(1부터 시작함)
            pstmt.setInt(1,1);
            int num = pstmt.executeUpdate(); // flush
            System.out.println(num); // 영향받은 행의 수 리턴 오류는 -1
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}Share article


