도라에몽주머니

[Java/Eclipse] Java 61, 62일차 본문

Study/Java

[Java/Eclipse] Java 61, 62일차

에몽쓰 2022. 12. 14. 15:51

Java

 

Class

: 클래스 정보를 찾을 때 사용하는 클래스

 

  • forName() : 클래스 이름 검색. try - catch문으로 예외 처리 필요

 

드라이버 클래스 등록 확인

try {
	// 프로젝트에 클래스를 검색해서 없으면 예외를 발생시킴
	Class.forName("oracle.jdbc.driver.OracleDriver");
	System.out.println("드라이버 등록 성공");
} catch (ClassNotFoundException e) {
	System.out.println("드라이버 등록 실패");
//	e.printStackTrace();
}

 

Oracle 접속 (Connection)

public Connection getConnection() {
	Connection conn = null;
	String url = "jdbc:oracle:thin:@localhost:1521:xe";
	String username = "C##dbexam";
	String password = "m1234";
	
	try {
		// oracle DB에 접속을 시도함
		// 접속이 성공하면, 접속정보를 Connection 객체에 저장시키고, 리턴함
		conn = DriverManager.getConnection(url, username, password);
	} catch (SQLException e) {
		e.printStackTrace();
	}
	
	return conn;
		
}

 

SQL 처리 ; insert

public int insertArticle(String name, int age, double height) {
	// SQL문에는 세미콜론(;)이 포함되어 있으면 안됨
	// SQL문에 삽입될 데이터 위치에는 "?"로 표시함
	String sql = "insert into dbtest values (?, ?, ?, sysdate)";
	int result = 0; // 처리 결과를 저장할 변수
	
	// DB에 접속
	Connection conn = getConnection();
	
	// SQL문 요청, 응답 처리 클래스
	PreparedStatement pstmt = null;
	
	try {
		pstmt = conn.prepareStatement(sql);
		// SQL문의 "?" 자리에 데이터를 삽입시켜서, SQL문을 완성하기
		pstmt.setString(1, name);
		pstmt.setInt(2, age);
		pstmt.setDouble(3, height);
		
		// SQL문 처리 작업
		// 1) SQL문 처리 요청
		// 2) 응답 기다림
		// 3) 전달된 응답 데이터를 리턴함
		// => insert 처리 데이터수를 oracle db가 전달함
		// => 기본값이 auto commit
		result = pstmt.executeUpdate();
			
	} catch (SQLException e) {
		e.printStackTrace();
	} finally {
		// 접속 끊기
		try {
			if(pstmt != null) pstmt.close();
			if(conn != null) conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	return result;	
}

 

SQL 처리 ; select

public void selectArticle() {
	String sql = "select * from dbtest";
		
	// 접속
	Connection conn = getConnection();
	// 요청, 응답 클래스
	PreparedStatement pstmt = null;
	// oracle db로부터 전달된 결과를 데이터를 저장하는 클래스
	ResultSet rs = null;
	
	try {
		pstmt = conn.prepareStatement(sql);
		// SQL문 처리 작업
		// 1) SQL문 처리 요청
		// 2) 응답 기다림
		// 3) 전달된 응답 데이터를 ResultSet 객체에 저장시키고 리턴함
		// => oracle db는 전체데이터를 문자열로 전달함
		rs = pstmt.executeQuery();
		
		// ResultSet 객체에 저장된 데이터 처리
		while(rs.next()) {
			String name = rs.getString("name");
			int age = rs.getInt("age");
			double height = rs.getDouble("height");
			String logtime  = rs.getString("logtime");
			
			String result = String.format("%s\t%d\t%.2f\t%s", name, age, height, logtime);
			System.out.println(result);
		}
	} catch (SQLException e) {
		e.printStackTrace();
	} finally { 
		try {
			if(rs != null) rs.close();
			if(pstmt != null) pstmt.close();
			if(conn != null) conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

 

SQL 처리 ; update

public int updateArticle(String name) {
	String sql = "update dbtest set age=age+1 where name=?";
	int result = 0;
	
	Connection conn = getConnection();
	PreparedStatement pstmt = null;
	
	try {
		pstmt = conn.prepareStatement(sql);
		
		// sql문 완성하기
		pstmt.setString(1, name);
		
		// SQL문 처리 작업
		// 1) SQL문 처리 요청
		// 2) 응답 기다림
		// 3) 전달된 응답 데이터를 리턴함
		// => insert 처리 데이터수를 oracle db가 전달함
		// => 기본값이 auto commit
		result = pstmt.executeUpdate();
		
	} catch (SQLException e) {
		e.printStackTrace();
	} finally {
		try {
			if(pstmt != null) pstmt.close();
			if(conn != null) conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}	
	return result;
}

 

SQL 처리 ; delete

public int deleteArticle(String name) {
	String sql = "delete dbtest where name=?";
	int result = 0;
	
	Connection conn = getConnection();
	PreparedStatement pstmt = null;
	
	try {
		pstmt = conn.prepareStatement(sql);
		
		// sql문 완성하기
		pstmt.setString(1, name);
		// SQL문 처리 작업
		// 1) SQL문 처리 요청
		// 2) 응답 기다림
		// 3) 전달된 응답 데이터를 리턴함
		// => insert 처리 데이터수를 oracle db가 전달함
		// => 기본값이 auto commit
		result = pstmt.executeUpdate();
	} catch (SQLException e) {
		e.printStackTrace();
	} finally {
		try {
			if(pstmt != null) pstmt.close();
			if(conn != null) conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	return result;
}

'Study > Java' 카테고리의 다른 글

[Java/Eclipse] Java 60일차  (0) 2022.12.08
[Java/Eclipse] Java 59일차  (0) 2022.12.07
[Java/Eclipse] Java 58일차  (0) 2022.12.06
[Java/Eclipse] Java 56일차  (0) 2022.12.02
[Java/Eclipse] Java 55일차  (0) 2022.12.01