on
Java JDBC Mybatis 방식
Java JDBC Mybatis 방식
public class DeptDTO { //테이블의 컬럼을 담는 클래스
int deptno;
String dname;
String loc;
public int getDeptno() {
return deptno;
}
public void setDeptno( int deptno) {
this .deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname( String dname) {
this .dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc( String loc) {
this .loc = loc;
}
public DeptDTO( int deptno, String dname, String loc) {
super ();
this .deptno = deptno;
this .dname = dname;
this .loc = loc;
}
public DeptDTO() {
super ();
}
@Override
public String toString () {
return "DeptDTO [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]" ;
}
}
///////////////////////////////////////////////////////////////////////////////
public class DeptDAO {
String driver = "oracle.jdbc.driver.OracleDriver" ;
String url = "jdbc:oracle:thin:@localhost:1521:xe" ;
String userid = "scott" ;
String passwd = "tiger" ;
// 기본생성자는 자동으로 실행되니까, 기본생성자에서 바로 드라이버로딩시키면 좋음
public DeptDAO() {
try {
Class.forName(driver);
System . out . println ( "로딩성공" );
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} // 기본생성자종료
public void selectAlldept() {
Connection con = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
// 로컬변수로 선언해야 더 안정적으로 사용할 수 있음
try {
con = DriverManager.getConnection(url, userid, passwd);
String sql = "select * from dept" ;
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
int deptno = rs.getInt( 1 );
String dname = rs.getString( 2 );
String loc = rs.getString( 3 );
System . out . println (deptno + " " + dname + " " + loc);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs ! = null )
rs.close();
if (pstmt ! = null )
pstmt.close();
if (con ! = null )
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
} // selectAlldept end
//////////////////////////////////////////////////////////////////////////
public class JDBCTest {
public static void main( String [] args) {
DeptDAO dao = new DeptDAO();
from http://cocoshin.tistory.com/28 by ccl(A) rewrite - 2021-11-27 22:01:50