公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

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).

    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中如何处理日期?的详细内容,更多请关注www.92cms.cn其它相关文章!

分享到:
标签:JDBC 如何处理 日期
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定