[ JDBC (Java DataBase Connectivity) ]
class JDBC01{
public JDBC01() {
Connection con=null; //DB접속,해제
Statement stmt=null; //sql구문을 실행하는 기능을 갖는 객체
try {
//1.드라이버 로딩
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("드라이버로딩 성공");
//2. DB접속 및 Connection객체 얻어오기
//String url="DB접속방법@서버주소:포트번호:SID";
String url="jdbc:oracle:thin:@localhost:1521:xe";
String user="c##scott";
String password="tiger";
con=DriverManager.getConnection(url,user,password);
System.out.println("DB접속 성공!");
//3. 원하는 작업 구현(추가/수정/삭제/조회등)
stmt=con.createStatement(); //sql명령어를 실해하기 위한 객체 얻어오기
// String sql="insert into members values(4,'이씨','0106666444','수원',sysdate)";
// stmt.executeUpdate(sql);//sql명령어 실행하기
String sql1="delete from members where name='최씨'";
int n=stmt.executeUpdate(sql1);
System.out.println(n + "개의 요청작업완료!");
String sql2="update members set phone='01088889999',addr='서울' where num=4";
stmt.executeUpdate(sql2);
System.out.println("수정 요청 작업완료!");
}catch(ClassNotFoundException ce) {
System.out.println("드라이버로딩실패:" + ce.getMessage());
}catch(SQLException se) {
System.out.println(se.getMessage());
}finally {
//4.DB접속해제
try {
if(stmt!=null)stmt.close();
if(con!=null)con.close();
}catch(SQLException se) {
System.out.println(se.getMessage());
}
}
}
}
public class Test01 {
public static void main(String[] args) {
new JDBC01();
}
}