- JDBC (Java Database Connectivity)
- 자바 프로그램에서 데이터베이스 접속할 수 있도록 만든 API
 
 - JDBC 프로그래밍 개요 (사용 클래스)
- JDBC 드라이버 코드
- System.setProperty()
 - Class.forName()
 
 - 데이터베이스 연결
- java.sql.Connection
 
 - Statement 생성
- java.sql.Statement
 - java.sql.PreparedStatement
 
 - SQL문 전송
- java.sql.Statement
- executeQuery()
 - executeUpdate()
 
 
 - java.sql.Statement
 - 결과 받기
- java.sql.ResultSet
 
 - 연결 해제
- java.sql.Connection
- close()
 
 
 - java.sql.Connection
 
 - JDBC 드라이버 코드
 - JDBC 프로그래밍
- JDBC 드라이버 로딩 (두가지 방법 중 택)
- System.setProperty(”jdbc.drivers”, “com.mysql.jdbc.Driver”);
 - Class.forName(”com.mysql.cj.jdbc.Driver”);
 
 - 데이터베이스 연결
- JDBC_URL = “jdbc:mysql://localhost:3306/sqldb”;
 - Connection conn = DriverManager.getConnection(JDBC_URL, “DB아이디”, “DB비밀번호”);
 
 - Statement 생성
- Statement stmt = conn.createStatement();
 - 사용 후에는 stmt.close();
 
 - SQL문 전송
- stmt.executeUpdate(”insert into 테이블명 values(’ “ + request.getParameter(”username”) + “’,’” + request.getParameter(”email”) + “ ‘)”);
- executeQuery()
- SELECT문 수행 시 사용
 
 - executeUpdate()
- UPDATE, DELETE과 같은 문 수행 시 사용
 
 
 - executeQuery()
 
 - stmt.executeUpdate(”insert into 테이블명 values(’ “ + request.getParameter(”username”) + “’,’” + request.getParameter(”email”) + “ ‘)”);
 - 결과 받기
- ResultSet rs = stmt.executeQuery();
 - while문
 - 
while(rs.next()) { name = rs.getString(1); age = rs.getInt(2); } - rs.close();
 
 - 연결 해제
- 사용이 끝난 데이터베이스 연결 정보는 닫아주기
 - try - with -resource 방법으로 처리
 
 
 - JDBC 드라이버 로딩 (두가지 방법 중 택)
 
728x90
    
    
  'Java' 카테고리의 다른 글
| [Java] JSP (Java Server Page) (0) | 2024.04.13 | 
|---|---|
| [Java] Jar 파일 생성 (0) | 2024.04.06 | 
| [Java] Collection (0) | 2024.03.18 | 
| [Java] Thread (0) | 2024.03.18 | 
| [Java] Exception (0) | 2024.03.18 |