DataSelect.java
01 import java.sql.*; 02 03 public class DataSelect{ 04 Connection con = null; 05 Statement stmt = null; 06 07 public static void main (String args []) { 08 DataSelect ds = new DataSelect(); 09 ds.connect(); 10 ds.select(); 11 ds.close(); 12 } 13 14 public void connect(){ 15 try{ 16 Class.forName("oracle.jdbc.driver.OracleDriver"); 17 con = DriverManager.getConnection 18 ("jdbc:oracle:thin:@127.0.0.1:1521:ora9", 19 "jdbc00","jdbc00"); 20 System.out.println("데이터베이스 연결되었습니다."); 21 }catch(ClassNotFoundException e){ 22 System.out.println("JDBC Driver load fail!!!"); 23 }catch(SQLException e){ 24 System.out.println("Connection fail!!!"); 25 e.printStackTrace(); 26 } 27 } 28 29 public void close(){ 30 try{ 31 con.close(); 32 System.out.println("데이터베이스 연결 해제되었습니다."); 33 }catch(SQLException e){ 34 System.out.println("Connection fail!!!"); 35 e.printStackTrace(); 36 }finally{ 37 try{ 38 if(con != null) con.close(); 39 }catch(SQLException e){} 40 } 41 } 42 43 public void select(){ 44 try{ 45 stmt = con.createStatement (); 46 String query = "select * from emp"; 47 ResultSet rs = stmt.executeQuery(query); 48 while(rs.next()){ 49 int id = rs.getInt("id"); 50 String result = "ID : " + id + "\t"; 51 String name = "이름 : " + rs.getString(2) + "\t"; 52 result += name; 53 double bonus = rs.getDouble(3); 54 result += "BONUS : " + bonus + "\t"; 55 Date inDate = rs.getDate(4); 56 result += "입사일 : " + inDate + "\t"; 57 String dept = "부서 : " + rs.getString(5) + "\t"; 58 result += dept; 59 System.out.println(result); 60 } 61 System.out.println("emp 테이블의 데이터가 조회되었습니다."); 62 stmt.close(); 63 }catch(SQLException e){ 64 e.printStackTrace(); 65 }finally { 66 try{ 67 if(stmt != null) stmt.close(); 68 }catch(SQLException e){} 69 } 70 } 71 }
|
'개발 > 자바/jsp' 카테고리의 다른 글
object replacenull (0) | 2018.09.07 |
---|---|
자바 리플렉션 (0) | 2016.08.19 |
eclipse 실행오류 could not create java virtual machine (0) | 2013.10.18 |
트랜젝션 처리. (0) | 2012.04.20 |