getConnection : connection 객체 생성 con = DriverManager.getConnection(url,id,pw); System.out.println("연결 성공..."); } catch (Exception e) { System.out.println("연결 실패..."); e.printStackTrace(); } return con; } // getConnection() end // 회원목록 public void SelectArticle() { Connection con = null; PreparedStatement pstmt = null; ResultSet res = null; tr"> getConnection : connection 객체 생성 con = DriverManager.getConnection(url,id,pw); System.out.println("연결 성공..."); } catch (Exception e) { System.out.println("연결 실패..."); e.printStackTrace(); } return con; } // getConnection() end // 회원목록 public void SelectArticle() { Connection con = null; PreparedStatement pstmt = null; ResultSet res = null; tr"> getConnection : connection 객체 생성 con = DriverManager.getConnection(url,id,pw); System.out.println("연결 성공..."); } catch (Exception e) { System.out.println("연결 실패..."); e.printStackTrace(); } return con; } // getConnection() end // 회원목록 public void SelectArticle() { Connection con = null; PreparedStatement pstmt = null; ResultSet res = null; tr">
package ch03_select;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
class Select{
// 연결 정보
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String id = "DBTEST";
private String pw = "a1234";
public Select() {
// JDBC 드라이버 할당.
try {
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("로딩 성공...");
} catch (Exception e) {
System.out.println("로딩 실패...");
e.printStackTrace();
}
} // Select() end
public Connection getConnection() {
Connection con = null;
try {
// DB연결 객체 생성 -> getConnection : connection 객체 생성
con = DriverManager.getConnection(url,id,pw);
System.out.println("연결 성공...");
} catch (Exception e) {
System.out.println("연결 실패...");
e.printStackTrace();
}
return con;
} // getConnection() end
// 회원목록
public void SelectArticle() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet res = null;
try {
String sql ="select * from member";
con = getConnection();
pstmt = con.prepareStatement(sql);
res = pstmt.executeQuery();
if(res == null) {
System.out.println("조회된 데이터가 없습니다.");
} else {
while(res.next()) { // res 객체가 값이 있나 없나 확인해서 있으면 빼내서 쓰면 됌. 레코드 행 위치를 참조. next() 한개의 행이 있나 없나. hasNext()와 같음.
// String name = res.getString("name");
// int age = res.getInt("age");
// double height = res.getDouble("height");
// String logtime = res.getString("logtime");
// System.out.print(name + '\\t' + age + '\\t' + height + '\\t' + logtime + '\\n');
System.out.print(res.getString("name") + '\\t' + res.getInt("age") + '\\t' + res.getDouble("height") + '\\t' + res.getString("logtime") + '\\n');
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(con!=null) con.close();
if(pstmt!=null) pstmt.close();
if(res!=null) res.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} // SelectArticle() end
}
public class SelectTest {
public static void main(String[] args) {
Select sl = new Select();
sl.SelectArticle();
}
}