JDBC -자바를 이용한 데이터베이스 접속과 SQL 문장의 실행, 그리고 실행 결과로 얻어진 데이터의 핸들링을 제공하는 방법과 절차에 관한 규약 -자바 프로그램내에서 SQL문을 실행하기 위한 자바 API -SQL과 프로그래밍 언어의 통합 접근 중 한 형태
Class.forName메소드란? 클래스를 로딩하는 메소드이다. //왜 new OralcDriver()을 하지 않고 forName() 메소드를 사용할 까? //new 를 이용할 경우 벤더별로 다를것이므로!
MyDriver.java 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 32 33 34 35 36 package com.bigdata2017.jdbc;... public class MyDriver implements Driver { static { System.out.println( "static code area" ); try { DriverManager.registerDriver(new MyDriver ()); } catch (SQLException e) { e.printStackTrace(); } } @Override public boolean acceptsURL (String arg0) throws SQLException { return false ; } @Override public Connection connect (String url, Properties info) throws SQLException { System.out.println( url ); System.out.println( info ); return new MyConnection (); } ... }
ConnectionTest.java 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 import java.sql.DriverManager;import java.sql.SQLException;public class ConnectionTest { public static void main (String[] args) { try { Class.forName( "com.bigdata2017.jdbc.MyDriver" ); String url = "jdbc:oracle:thin:@localhost:1521:xe" ; DriverManager.getConnection(url, "hr" , "hr" ); } catch (ClassNotFoundException e) { System.out.println( "드라이버 로딩 실패:" + e ); } catch (SQLException e) { System.out.println( "연결실패" + e ); } } }
MyConnection.java 1 2 3 4 5 6 7 package com.bigdata2017.jdbc;public class MyConnection implements Connection { ... }
실습 프로젝트 우클릭 - 자바 빌드 패스 - 라이브러리탭- add library - user library 선택 -new - jdbc(아무거나 가능) - add external jar - ojdbc 파일 찾기 - apply - finish
SelectTest SelectTest.java 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class SelectTest { public static void main (String[] args) { Connection conn = null ; Statement stmt = null ; ResultSet rs = null ; try { Class.forName( "oracle.jdbc.driver.OracleDriver" ); String url = "jdbc:oracle:thin:@localhost:1521:xe" ; conn = DriverManager.getConnection(url, "hr" , "hr" ); stmt = conn.createStatement(); String sql = "select EMPLOYEE_ID," + " FIRST_NAME," + " LAST_NAME," + " SALARY " + " from employees" ; rs = stmt.executeQuery(sql); System.out.println("connection 성공" ); while ( rs.next() ) { Long employee_id = rs.getLong( 1 ); String first_Name = rs.getString( 2 ); String last_Name = rs.getString( 3 ); int salary = rs.getInt(4 ); System.out.println( employee_id+ first_Name + last_Name + salary); } } catch (ClassNotFoundException e) { System.out.println( "드라이버 로딩 실패:" + e ); } catch (SQLException e) { System.out.println( "error" + e ); } finally { try { if ( rs != null ) { rs.close(); } if ( stmt != null ) { stmt.close(); } if ( conn != null ) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
UpdateTest Statement를 사용함. 아래 Insert에서는 Prepared Statement를 사용 한다. 비교해보자
UpdateTest.java 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 public class UpdateTest { public static void main (String[] args) { Connection conn = null ; Statement stmt = null ; try { Class.forName( "oracle.jdbc.driver.OracleDriver" ); String url = "jdbc:oracle:thin:@localhost:1521:xe" ; conn = DriverManager.getConnection(url, "dev" , "dev" ); stmt = conn.createStatement(); String name = "도우넛" ; Long no = 12L ; String sql = "update author " + " set name = '" + name +"'" + " where no = " + no; int count = stmt.executeUpdate(sql); System.out.println( count == 1 ? "성공" : "실패" ); } catch (ClassNotFoundException e) { System.out.println( "드라이버 로딩 실패:" + e ); } catch (SQLException e) { System.out.println( "error" + e ); } finally { try { if ( stmt != null ) { stmt.close(); } if ( conn != null ) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
InsertTest 보통 PreparedStatement를 많이 쓴다. select의 경우 바인드만 안하면 되니까!
InsertTest.java 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class InsertTest { public static void main (String[] args) { Connection conn = null ; PreparedStatement pstmt = null ; try { Class.forName( "oracle.jdbc.driver.OracleDriver" ); String url = "jdbc:oracle:thin:@localhost:1521:xe" ; conn = DriverManager.getConnection(url, "dev" , "dev" ); String sql = "insert" + "into author" + "values(seq_author.nextval,?,?)" ; pstmt = conn.prepareStatement(sql); pstmt.setString( 1 , "공자" ); pstmt.setString( 2 , "어쩌구저쩌구" ); int count = pstmt.executeUpdate(); System.out.println( count == 1 ? "성공" : "실패" ); } catch (ClassNotFoundException e) { System.out.println( "드라이버 로딩 실패:" + e ); } catch (SQLException e) { System.out.println( "error" + e ); } finally { try { if ( pstmt != null ) { pstmt.close(); } if ( conn != null ) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }