JDBC 연결
public class DBUtil {
private static Connection conn;
public static Connection open() {
String url="jdbc:oracle:thin:@localhost:1521:xe";
String id="hr";
String pw="*******";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, id, pw);
return conn;
} catch (Exception e) {
System.out.println("DBUtil.open()");
e.printStackTrace();
}
return null;
}
public static Connection open(String server, String id, String pw) {
String url="jdbc:oracle:thin:@"+server+":1521:xe";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, id, pw);
return conn;
} catch (Exception e) {
System.out.println("DBUtil.open()");
e.printStackTrace();
}
return null;
}
}
Statement 클래스
- SQL 구문을 실행해주는 역할(대리자)
- 모든 SQL을 실행할 수 있다.
Statement 클래스 종류
- Statement
stat=conn.createStatement();
int result=stat.executeUpdate(sql);
if (result==1) {
System.out.println("등록 성공");
}else {
System.out.println("등록 실패");
}
stat.close();
conn.close();
private static void m9() {
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try {
conn = DBUtil.open();
stat = conn.createStatement();
String sql = "select count(*) as cnt from tblInsa where buseo='영업부'";
rs = stat.executeQuery(sql);
if (rs.next()) {
System.out.println("영업부 직원수 : "+rs.getString("cnt"));
}
rs.close();
sql = "select * from tblInsa where buseo='영업부'";
rs = stat.executeQuery(sql);
int n=0;
rs.close();
stat.close();
conn.close();
} catch (Exception e) {
System.out.println("Ex05_select.m9()");
e.printStackTrace();
}
}
- PreparedStatement
- Statement 개량 클래스 -> 매개 변수 처리에 특화
- 안정성 높음 + 가독성 높음
- 코드량 많음
public static void main(String[] args) {
String name="하하하";
String age="20";
String gender="m";
String tel="010-1111-2222";
String address="서울시 동대문구 이문'동";
address = address.replace("'", "''");
Connection conn = null;
Statement stat = null;
PreparedStatement pstat = null;
ResultSet rs = null;
try {
conn = DBUtil.open();
String sql = String.format("insert into tblAddress (seq, name, age, gender, tel, address, regdate) values (seqAddress.nextVal, '%s', %s, '%s', '%s', '%s', default)", name, age, gender, tel, address);
stat = conn.createStatement();
System.out.println(stat.executeUpdate(sql));
sql = "insert into tblAddress (seq, name, age, gender, tel, address, regdate) values (seqAddress.nextVal, ?, ?, ?, ?, ?, default)";
pstat=conn.prepareStatement(sql);
pstat.setString(1, name);
pstat.setString(2, age);
pstat.setString(3, gender);
pstat.setString(4, tel);
pstat.setString(5, address);
System.out.println(pstat.executeUpdate());
stat.close();
conn.close();
} catch (Exception e) {
System.out.println("Ex06_PreparedStatement.main()");
e.printStackTrace();
}
}
- CallableStatement
- Statement 개량 클래스 -> 프로시저 호출
- 프로시저 전용
- PreparedStatement와 유사
private static void m5() {
Connection conn = null;
CallableStatement stat = null;
ResultSet rs = null;
try {
conn = DBUtil.open();
String sql = "{ call procM5(?, ?) }";
stat=conn.prepareCall(sql);
stat.setString(1, "개발부");
stat.registerOutParameter(2, OracleTypes.CURSOR);
stat.executeQuery();
rs=(ResultSet)stat.getObject(2);
while(rs.next()) {
System.out.println(rs.getString("name"));
System.out.println(rs.getString("jikwi"));
System.out.println(rs.getString("city"));
System.out.println();
}
rs.close();
stat.close();
conn.close();
} catch (Exception e) {
System.out.println("Ex07_CallableStatement.m5()");
e.printStackTrace();
}
}