JDBC中如何处理日期?

WBOY
发布: 2023-09-07 21:09:04
转载
1193人浏览过

jdbc中如何处理日期?

You can insert date values in SQL using the date datatype, The java.sql.Date class maps to the SQL DATE type.

The PreparedStatement interface provides a method named setDate(). Using this you can insert date into a table. This method accepts two parameters −

  • An integer representing the parameter index of the place holder (?) to which we need to set date value.

  • a Date object representing the date value to be passed. The constructor of java.sql.Date class accepts a variable of long type representing the number of milliseconds from the epoch (standard base time I.e. January 1, 1970, 00:00:00 GMT).

    动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版
    动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版

    动态WEB网站中的PHP和MySQL详细反映实际程序的需求,仔细地探讨外部数据的验证(例如信用卡卡号的格式)、用户登录以及如何使用模板建立网页的标准外观。动态WEB网站中的PHP和MySQL的内容不仅仅是这些。书中还提到如何串联JavaScript与PHP让用户操作时更快、更方便。还有正确处理用户输入错误的方法,让网站看起来更专业。另外还引入大量来自PEAR外挂函数库的强大功能,对常用的、强大的包

    动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版 508
    查看详情 动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版

Example

Assume we have created a table named Emp in MySQL database with the following description −

+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| Name     | varchar(255) | YES  |     | NULL    |       |
| DOB      | date         | YES  |     | NULL    |       |
| Location | varchar(255) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
登录后复制

Following JDBC program inserts records in to this table −

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Date;
public class InsertingDate {
   public static void main(String args[])throws Exception {
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Inserting values to a table
      String query = "INSERT INTO Emp(Name, DOB, Location) VALUES (?, ?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "Amit"); pstmt.setDate(2, new Date(622790105000L));
      pstmt.setString(3, "Hyderabad");
      pstmt.execute();
      pstmt.setString(1, "Sumith"); pstmt.setDate(2, new Date(620611200000L));
      pstmt.setString(3, "Vishakhapatnam");
      pstmt.execute();
      pstmt.setString(1, "Sudha");
      pstmt.setDate(2, new Date(336614400000L));
      pstmt.setString(3, "Vijayawada");
      pstmt.execute();
      System.out.println("Records inserted......");
   }
}
登录后复制

Output

Connection established......
Records inserted......
登录后复制

如果你在MySQL数据库中验证表格,你可以观察到表格的内容如下:

mysql> select * from Emp;
+--------+------------+----------------+
| Name   | DOB        | Location       |
+--------+------------+----------------+
| Amit   | 1989-09-26 | Hyderabad      |
| Sumith | 2019-03-19 | Vishakhapatnam |
| Sudha  | 2019-03-19 | Vijayawada     |
+--------+------------+----------------+
3 rows in set (0.00 sec)
登录后复制

以上就是JDBC中如何处理日期?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号