Coding Planet

[JDBC] Oracle DB 연동 JDBC : INSERT 예제 본문

Server

[JDBC] Oracle DB 연동 JDBC : INSERT 예제

jhj.sharon 2023. 4. 5. 02:05
반응형
public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try {
			//1) 드라이버를 JVM에 로드
			Class.forName("oracle.jdbc.drive.OracleDriver");
			
			//2) 드라이버 연결
			String url = "";
			String user = "사용자";
			String password = "패스워드";
			conn = DriverManager.getConnection(url, user, password);
			
			//3) sql을 담을 pstmt 객체 생성
			String sql = "INSERT INTO NoticeBoard (idx_no, writer, content, pw) VALUES (?, ?, ?, ?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, 1);
			pstmt.setString(2, "sharon");
			pstmt.setString(3,  "blablalbaa");
			pstmt.setString(4, "1234");
			
			int result = pstmt.executeUpdate();
			System.out.println(result + "갱신");
			
			
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
			System.out.println("드라이버를 로드할 수 없습니다.");
		}catch(SQLException e) {
			e.printStackTrace();
			System.out.println("jdbc 연결에 실패했습니다.");
		}finally {
			
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
				
			}catch(SQLException e) {
				e.printStackTrace();
			}
		}

	}

}
반응형
Comments