디비 연동 후, 날짜와 시간처리 하기 예제
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);
}
}
}