import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class Exam08 {
public static void main(String[] ar){
try{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(Exception e){
System.err.println(e);
System.exit(1);
}
Connection conn = null;
PreparedStatement pstmt = null;
Statement stmt = null;
String url = "jdbc:mysql://localhost:3306/java";
String id = "root";
String pass= "1423";
String query = null;
try{
conn = DriverManager.getConnection(url,id,pass);
}catch(Exception e){
System.err.println(e);
System.exit(1);
}
// ------------- Databases 접속 ----------------//
query = "select * from datetable";
try{
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
int num = rs.getInt("num");
java.sql.Date mydate = rs.getDate("mydate");
long a = mydate.getTime();
java.sql.Time mytime = rs.getTime("mytime");
long b = mytime.getTime();
java.sql.Timestamp mytimestamp = rs.getTimestamp("mytimestamp");
long c = mytimestamp.getTime();
System.out.print("num = " + num + "date : " + new Date(a) + "time = " + new Date(b) + "stamp = " + new Date(c) );
// 요즘은 오토박싱이 되어서 결과값이 변환작업을 하지 않아도 출력이 되나. 수정하기 위해서는 이런 변환 작업을 거쳐야 함
}
rs.close();
stmt.close();conn.close();
}catch(SQLException e){
System.err.println(e);
System.exit(1);
}
}
}
'JAVA > JSP & Servlet' 카테고리의 다른 글
오라클 디비 연동하여 프로시져호출 예제 (0) | 2013.07.15 |
---|---|
디비 연동 후 zip code 예제 소스 (0) | 2013.07.15 |
JDBC (0) | 2013.07.13 |
JDBC 프로그래밍 (0) | 2013.03.14 |
에러 페이지 다루기 ! (0) | 2013.03.06 |