jdbc 오라클 연결하는 방법이다.
import java.io.*;
import java.sql.*;
class Employee
{
String sql = "select distinct lendTitle as title, lendDay, receiveDay from film, lend, customer where customer.memberNum=lend.lendMember and customer.customerName=?";
String url = "jdbc:oracle:thin:@210.115.46.49:1521:orcl";
Connection conn;
String userid = "its50";
String passwd = "whdudxo2";
PreparedStatement stmt;
ResultSet rset;
public Employee(){}
public void Connection()
{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url,userid,passwd);
System.out.println("오라클 서버에 연결 성공");
}catch(SQLException e1){
System.out.println(e1+"연결 실패");
}catch(ClassNotFoundException e2){
System.out.println(e2+"드라이버 로딩 실패");
}
}
public void ActionDB() throws IOException
{
String str = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
sql = "SELECT distinct customerName, customerPhone, addr FROM customer WHERE customerName=?";
stmt = conn.prepareStatement(sql);
System.out.println("input name:");
str = br.readLine();
stmt.setString(1, str);
rset = stmt.executeQuery();
while(rset.next())
{
System.out.println("이름: " + str + "\t");
System.out.println("전화번호: " + rset.getString(2) + "\t");
System.out.println("주소: " + rset.getString(3) + "\t");
}
sql = "select distinct lendTitle as title, lendDay, receiveDay from film, lend, customer where customer.memberNum=lend.lendMember and customer.customerName=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, str);
rset = stmt.executeQuery();
while(rset.next())
{
System.out.println("제목: " + rset.getString(1) + "\t");
System.out.println("대여일: " + rset.getString(2) + "\t");
System.out.println("반납일: " + rset.getString(3) + "\n");
}
}catch(SQLException e){
System.out.println("쿼리실패");
}
}
public static void main(String args[]) throws SQLException
{
Employee em = new Employee();
em.Connection();
try{
em.ActionDB();
}catch(IOException e){}
}
}
위 소스는 간단한 예제이다. 위 소스는 간단하게 디비에서 데이터를 받아서 화면에 뿌려 주는 소스이다.
위 소스를 컴파일 하기전에 자신의 컴퓨터에 자바 sdk 와 드라이버가 설치되어 있어야 한다.
드라이버는 자바 홈에서 다운받아서 JAVA_HOME\jre1.5.0_06\lib\ext 폴더에 카피하면 된다.
DB_10주_결과_20001814_조영태.hwp