class Test02 {
public Test02() {
Connection conn = null; //DB접속 , 해제 , 트랜젝션 처리 ..
Statement stmt = null; // SQL 구문 실행
ResultSet rs = null; //select로 추출된 결과 집합을 사용하기 위한 객체 (Select는 update,delete와 다르기 때문에 포스팅을 하나 더 했습니다)
try {
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String id = "test";
String password = "1234";
conn=DriverManager.getConnection(url, id, password);
System.out.println("jdbc driver Connection");
String sql = "select * from members";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql); // rs의 시작지점을 참조
while(rs.next()) { //다음 행으로 이동 (while문을 돌려 다음 값을 꺼내온다.)
//현재 행의 컬럼값들 꺼내오기
int num = rs.getInt("num"); //행의 num값 꺼내오기 ..
String name = rs.getString("name"); //행의 name값 꺼내오기 ..
String phone = rs.getString("phone"); //행의 phone값 꺼내오기 ..
String addr = rs.getString("addr"); //행의 addr값 꺼내오기 ..
//java.sql.Date import
Date regdate = rs.getDate("regDate");
System.out.print(num+ " ");
System.out.print(name+ " ");
System.out.print(phone+ " ");
System.out.print(addr+ " ");
System.out.print(regdate+ " ");
System.out.println("");
} //while
}catch(ClassNotFoundException e) {
System.out.println(e.getMessage());
}catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(rs!=null)rs.close(); //닫는건 역순으로 닫는다.
if(stmt!=null)stmt.close();
if(conn!=null)conn.close();
}catch(SQLException e) {
System.out.println(e.getMessage());
}
}
}
}
public class JDBC02 {
public static void main(String[] args) {
new Test02();
}
}
'JSP' 카테고리의 다른 글
[JSP/Servlet] Statement / PreparedStatement (0) | 2018.12.20 |
---|---|
[JSP/Servlet] PreparedStatement (0) | 2018.12.20 |
[JSP/Servlet] Oracle 연결 (0) | 2018.12.18 |
[JSP/Servlet]페이지이동 (2) | 2018.12.13 |
[JSP/Servlet] SCOPE (0) | 2018.12.12 |