public static void main(String[] args) {
getTime();
}
public static void saveTime(){
//定义oracle数据库的驱动的类
String driverClass = "oracle.jdbc.OracleDriver";
//定义连接的oracle的url
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
//用户名
String username = "scott";
//密码
String password = "tiger";
//定义Connection连接
Connection conn = null;
//定义数据库的sql执行对象
PreparedStatement ps = null;
//定义要执行的sql
String addSql = "insert into time_test values(?,?,?)";
try {
//注册数据库的驱动程序
Class.forName(driverClass);
//获得数据库的连接
conn = DriverManager.getConnection(url, username, password);
//创建sql执行对象
ps = conn.prepareStatement(addSql);
java.util.Date date = new java.util.Date();
ps.setDate(1, new Date(date.getTime()));
ps.setTime(2, new Time(date.getTime()));
ps.setTimestamp(3, new Timestamp(date.getTime()));
int count = ps.executeUpdate();
System.out.println("数据相应条数:"+count);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(ps != null){
ps.close();
}
if(conn != null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void getTime(){
//定义oracle数据库的驱动的类
String driverClass = "oracle.jdbc.OracleDriver";
//定义连接的oracle的url
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
//用户名
String username = "scott";
//密码
String password = "tiger";
//定义Connection连接
Connection conn = null;
//定义数据库的sql执行对象
PreparedStatement ps = null;
//定义结果集对象
ResultSet rs = null;
//定义要执行的sql
String sql = "select * from time_test";
try {
//注册数据库的驱动程序
Class.forName(driverClass);
//获得数据库的连接
conn = DriverManager.getConnection(url, username, password);
//创建sql执行对象
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
/*while(rs.next()){
Date date = rs.getDate(1);
Time time = rs.getTime(2);
Timestamp timestamp = rs.getTimestamp(3);
System.out.println("日期:"+date);
System.out.println("时间:"+time);
System.out.println("时间戳:"+timestamp);
}*/
/*while(rs.next()){
java.util.Date date = rs.getDate(1);
java.util.Date time = rs.getTime(2);
java.util.Date timestamp = rs.getTimestamp(3);
System.out.println("日期:"+date);
System.out.println("时间:"+time);
System.out.println("时间戳:"+timestamp);
}*/
while(rs.next()){
java.util.Date date = rs.getDate(1);
java.util.Date time = rs.getTimestamp(2);
java.util.Date timestamp = rs.getTimestamp(3);
System.out.println("日期:"+date);
System.out.println("时间:"+time);
System.out.println("时间戳:"+timestamp);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(ps != null){
ps.close();
}
if(conn != null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
JDBC学习笔记—对时间类型的处理
最新推荐文章于 2024-06-14 20:27:00 发布