国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

Java高級日期概念三

2019-11-18 12:10:54
字體:
來源:轉載
供稿:網(wǎng)友

  在一個SQL數(shù)據(jù)庫中保存和提取日期數(shù)據(jù)我們將要使用的下一個類是java.sql.Date,它是java.util.Date的子類但它使用了Java數(shù)據(jù)庫連接(JDBC)方法
  
  。讓我們來看一個簡單的只有一個表單--LAST_accessOracle數(shù)據(jù)庫,它是用下面的SQL創(chuàng)建的:
  
  create table LAST_ACCESS (
  
  LAST_HIT date
  
  );
  
  
  
  這個表單只有一個記錄,用下面的插入語句創(chuàng)建:
  
  insert into LAST_ACCESS values (Sysdate);
  
  
  
  表E演示了如何修改和提取LAST_HIT數(shù)據(jù)庫域。
  
  
  
  表 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.
  
  燙onnection 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 {
  
  燬ystem.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
  
  
  
  雖然這個例子沒有為保存和提取日期數(shù)據(jù)提供性能上優(yōu)良的方法,但它確實示范了如何為一條更新和刪除語句將Java日期數(shù)據(jù)轉換成SQL日期數(shù)據(jù)。從一個java.util.Date對象設置Oracle date數(shù)據(jù)域的過程是由以下的語句處理的:
  
  ps.setDate(1, date);
  
  
  
  它是我們預定義語句接口java.sql.PreparedStatement.setDate 的一個方法。
  
  
  
  這行代碼出現(xiàn)在我們的setLastHit方法里。它將Java以微秒為單位的長整型日期值轉換成ORACLE的SQL日期格式。當我們能夠在getLastHit方法里用java.sql.PreparedStatement.getDate從數(shù)據(jù)庫取得日期數(shù)據(jù)的時候這種轉換就能夠完成。
  
  
  
  你還應該注重到只有日期被設置了。小時,分鐘,秒,和微秒都沒有包括在從Java日期數(shù)據(jù)到SQL日期數(shù)據(jù)的轉換過程中。
  
  
  
  結論
  
  一旦你把握了這些概念,你就應該能夠基于系統(tǒng)時間或者一個輸入的時間創(chuàng)建日期對象了。另外,你還應該能夠使用標準和定制的格式化過程格式化日期數(shù)據(jù),將文本的日期數(shù)據(jù)解析成日期對象,并以多種語言和多種時區(qū)顯示一個日期數(shù)據(jù)。最后,你將能夠在一個SQL數(shù)據(jù)庫里保存和提取日期值。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 嘉荫县| 友谊县| 海门市| 黄大仙区| 宜宾市| 台南县| 吉首市| 新平| 抚宁县| 久治县| 汕尾市| 威远县| 罗甸县| 邢台市| 茂名市| 上思县| 昭苏县| 外汇| 新民市| 白水县| 鹤山市| 固镇县| 禄丰县| 玉山县| 青神县| 万山特区| 乐陵市| 清远市| 平武县| 本溪市| 睢宁县| 五莲县| 西畴县| 阿克| 淮阳县| 苏尼特右旗| 永泰县| 扎囊县| 西乌珠穆沁旗| 澄江县| 济宁市|