在一个sql数据库中保存和提取日期数据我们将要使用的下一个类是java.sql.date,它是java.util.date的子类但它使用了java数据库连接(jdbc)方法
。让我们来看一个简单的只有一个表单--last_access的oracle数据库,它是用下面的sql创建的:
create table last_access (
last_hit date
);
这个表单只有一个记录,用下面的插入语句创建:
insert into last_access values (sysdate);
表e演示了如何修改和提取last_hit数据库域。
表 e
import java.sql.*;
import java.text.dateformat;
import java.util.date;
public class dateexample10 {
public static void main(string[] args) {
// get a full date formatter.
dateformat dateformatter = dateformat.getdatetimeinstance(
dateformat.full,
dateformat.full);
// get the system date and time.
java.util.date utildate = new date();
// convert it to java.sql.date
java.sql.date date = new java.sql.date(utildate.gettime());
// display the date before storing.
system.out.println(dateformatter.format(date));
// save the date to the database.
setlasthit(date);
// get the date from the database.
date datefromdb = getlasthit();
// display the date from the database.
system.out.println(dateformatter.format(datefromdb));
}
public static void setlasthit(java.sql.date date) {
try {
// load the class.
class.forname("oracle.jdbc.driver.oracledriver");
// get a connection.
?connection connection = drivermanager.getconnection(
// database url
"jdbc:oracle:thin:@localhost:1521:buzz2",
"web_site", // username
"web_site"); // password
try {
/ get a prepared statement fromthe connection
// specifying the update sql.
preparedstatement ps = connection.preparestatement(
"update last_access set last_hit=");
try {
/ set the date letting jdbc to the work of
// formatting the sql appropriately.
ps.setdate(1, date);
// execute the update statement.
int irowsupdated = ps.executeupdate();
system.out.println("rows updated: " + irowsupdated);
} finally {
ps.close();
}
} finally {
connection.close();
}
} catch (exception ex) {
system.out.println("error: " + ex.getmessage());
}
}
public static java.sql.date getlasthit() {
java.sql.date returndate = null;
try {
// load the driver class.
class.forname("oracle.jdbc.driver.oracledriver");
// get the connection.
connection connection = drivermanager.getconnection(
"jdbc:oracle:thin:@localhost:1521:buzz2",
"web_site", "web_site");
try {
/ get the prepared statement specifying the
// select sql.
preparedstatement ps = connection.preparestatement(
"select last_hit from last_access");
try {
// execute the sql and get the resultset object.
resultset rs = ps.executequery();
try {
// retreive the record.
if (rs.next()) {
// return the last hit date.
returndate = rs.getdate("last_hit");
system.out.println(
"successfully retrieved last hit.");
} else {
?system.out.println("did not get last hit.");
}
}
finally {
rs.close();
}
} finally {
ps.close();
?}
} finally {
connection.close();
}
} catch (exception ex) {
system.out.println("error: " + ex.getmessage());
}
return returndate;
}
}
这个例子的输出如下:
friday, october 5, 2001 10:42:34 pm edt
rows updated: 1
successfully retrieved last hit.
friday, october 5, 2001 12:00:00 am edt
虽然这个例子没有为保存和提取日期数据提供性能上优良的方法,但它确实示范了如何为一条更新和删除语句将java日期数据转换成sql日期数据。从一个java.util.date对象设置oracle date数据域的过程是由以下的语句处理的:
ps.setdate(1, date);
它是我们预定义语句接口java.sql.preparedstatement.setdate 的一个方法。
这行代码出现在我们的setlasthit方法里。它将java以微秒为单位的长整型日期值转换成oracle的sql日期格式。当我们能够在getlasthit方法里用java.sql.preparedstatement.getdate从数据库取得日期数据的时候这种转换就能够完成。
你还应该注意到只有日期被设置了。小时,分钟,秒,和微秒都没有包括在从java日期数据到sql日期数据的转换过程中。
结论
一旦你掌握了这些概念,你就应该能够基于系统时间或者一个输入的时间创建日期对象了。另外,你还应该能够使用标准和定制的格式化过程格式化日期数据,将文本的日期数据解析成日期对象,并以多种语言和多种时区显示一个日期数据。最后,你将能够在一个sql数据库里保存和提取日期值。
闽公网安备 35060202000074号