Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter02 : insert 본문

JAVA/chapter26_statement

chapter02 : insert

GAWON 2023. 5. 31. 09:34
package org.joonzis.ex;

import java.sql.Connection;
import java.sql.Statement;

import org.joonzis.db.DBConnection;

public class Ex01_insert {
	public static void main(String[] args) {

		Connection conn = null;
		Statement st = null;

		try {
			conn = DBConnection.getConnection();

			// Statement 생성
			st = conn.createStatement();

			// 쿼리 작성 - > 스콧, 99 , 인천
			// 세미콜론 주의
			String sql = "insert INTO PERSON VALUES('스콧',99,'인천')";

			// 쿼리 날리기 (DB로 sql 전달)
			// executrUpdate : insert , update, delete문 사용
			// (메소드 리턴 타입이 int라서 이렇게 작성)
			// ex) st:버스 sql : 승객
			int result = st.executeUpdate(sql);

			// 삽입성고 : 1 ,실패 : 0
			if (result == 1) {
				System.out.println("레코드의 삽입 성공!");
			} else {
				System.out.println("레코드의 삽입 실패");
			}

			conn.commit();

		} catch (Exception e) {
			e.printStackTrace();

			// 롤백

			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (Exception e2) {

			}
		} finally {
			try {
				if (st != null) {
					st.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}

		}

	}
}

'JAVA > chapter26_statement' 카테고리의 다른 글

chapter04 : select  (0) 2023.05.31
chapter03 : update  (0) 2023.05.31
chapter01 : DBConnection  (0) 2023.05.31