`
luyunjian
  • 浏览: 57179 次
社区版块
存档分类
最新评论

PrepareStatement插入时间

阅读更多
java.sql.Date stores only date information, not times. Simply converting a java.util.Date into a java.sql.Date will silently set the time to midnight. So, to store date/times to be manipulated as java.util.Date objects, don’t do this:

// BUG: loses time of day
preparedStatement.setDate(1, new java.sql.Date(date.getTime()));

do this instead:

preparedStatement.setTimestamp(1, new java.sql.Timestamp(date.getTime()));
java.sql.Timestamp is not a date

java.sql.Timestamp extends java.util.Date, but it should not be used as a Date. In JDK 1.3.1, Timestamp.getTime() (inherited from Date) returns the time to the nearest second only, but JDK 1.4.2 and JDK 1.5 it returns the time to the nearest millisecond as expected. So in JDK 1.3, when reading a timestamp from a ResultSet, don’t do this:

// Java 1.3
java.util.Date d = resultSet.getTimestamp(1);
long millis = d.getTime(); // BUG: loses fractional seconds in JDK 1.3

To get the full date including milliseconds, you have to do this:

java.sql.Timestamp timestamp = resultSet.getTimestamp(1);
java.util.Date d = new java.util.Date(timestamp.getTime() +
                                      timestamp.getNanos() / 1000000);

In JDK 1.4.2 and JDK 1.5, you can just do this, depending on what you’re going to do with the Date:

// Java 1.4+
java.util.Date d = resultSet.getTimestamp(1);

But this might be safer since it avoids any other potential Timestamp problems:

// Java 1.4+
java.util.Date d = new java.util.Date(resultSet.getTimestamp(1).getTime());

If your code needs to run on JDK 1.3 and later, you’ll have to do this:

java.sql.Timestamp timestamp = resultSet.getTimestamp(1);
long millis = (timestamp.getTime() / 1000) * 1000 + timestamp.getNanos() / 1000000;
java.util.Date d = new java.util.Date(millis);


http://www.thunderguy.com/semicolon/2003/08/14/java-sql-date-is-not-a-real-date/
分享到:
评论

相关推荐

    关于PreparedStatement插入Date类型值的方法.txt

    PreparedStatement pstmt = connection.prepareStatement("INSERT INTO my_table (date_column) VALUES (?)"); pstmt.setDate(1, new java.sql.Date(System.currentTimeMillis())); // 使用当前时间创建Date对象 ...

    java中时间转换问题及将时间插入数据库

    在上面的代码中,我们首先获取了一个数据库连接,然后我们使用 `prepareStatement` 方法创建了一个 `PreparedStatement` 对象。接着,我们使用 `setString` 方法将字符串参数设置到 SQL 语句中。然后,我们使用 `...

    在oracle中插入当前时间

    PreparedStatement pstmt = conn.prepareStatement( "INSERT INTO your_table (id, created_at) VALUES (?, ?)"); // 设置参数 pstmt.setInt(1, 1); pstmt.setTimestamp(2, sqlTimestamp); // 或者使用 ts /...

    statement和prepared区别

    PreparedStatement pstmt = con.prepareStatement("SELECT * FROM users WHERE age = ?"); pstmt.setInt(1, age); ResultSet rs = pstmt.executeQuery(); ``` 在这种情况下,即使我们多次执行该语句并更改`age`的值...

    详细介绍在tomcat中配置数据源原理

    Connection 代表和数据库的连接,其常用方法包括 getMetaData() 返回数据库的 MetaData 数据,createStatement() 创建并返回 Statement 对象,PrepareStatement(String sql) 创建并返回 PrepareStatement 对象。...

    java代码oracle数据库批量插入

    - **创建PreparedStatement**: 通过`PreparedStatement pst = (PreparedStatement) con.prepareStatement(sql.toString());`创建PreparedStatement对象,这个对象允许我们执行预编译的SQL语句,并且可以多次执行相同...

    java数据库连接Statement

    PreparedStatement pstmt = con.prepareStatement("INSERT INTO Table1 (a, b) VALUES (?, ?)"); pstmt.setInt(1, 1); pstmt.setString(2, "value"); pstmt.executeUpdate(); ``` ### CallableStatement `...

    java实现oracle插入当前时间的方法

    PreparedStatement ps = connection.prepareStatement("INSERT INTO table_name (date_column) VALUES (?)"); ps.setDate(1, sqlDate); ps.executeUpdate(); ``` 3. **时区处理**:如果数据库字段需要精确到...

    java向mysql中插入150万条数据比较索引表的优势

    PreparedStatement pstmt = connection.prepareStatement(sql); for (Data data : dataList) { pstmt.setString(1, data.getColumn1()); pstmt.setString(2, data.getColumn2()); pstmt.addBatch(); } int[] ...

    用java程序怎么实现200ms往数据库中插入10000条数据.doc

    PreparedStatement ps = con.prepareStatement(insertStr); for (int i = 0; i ; i++) { ps.setString(1, names[i]); ps.setString(2, mimas[i]); ps.addBatch(); } ps.executeBatch(); ``` 2. **预编译SQL语句...

    5、批量插入海量数据之Java插入MySql(csdn)————程序.pdf

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO table_name (column1, column2) VALUES (?, ?)"); for (int i = 0; i ; i++) { pstmt.setString(1, "value1"); pstmt.setString(2, "value2"); ...

    JDBC连接MySQL数据库批量插入数据过程详解

    3. 创建PreparedStatement:使用Connection.prepareStatement()方法创建PreparedStatement对象。 4. 设置SQL语句:使用PreparedStatement对象设置批量插入数据的SQL语句。 5. 遍历Map:遍历Map对象,获取每个...

    jdbc线程池演示demo

    PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, value1); pstmt.setString(2, value2); pstmt.executeUpdate(); // 批量插入 List[]> data = ...; // 包含多条数据 for (String[] row ...

    java连接数据库.doc

    PreparedStatement ps = con.prepareStatement("insert into TableName(dAddTime) values(?)"); ps.setDate(1, new java.sql.Date(System.currentTimeMillis())); // 或者 ps.setTime(2, new java.sql.Time(System....

    java.util.Date与java.sql.Date互转及字符串转换为日期时间格式[文].pdf

    如果我们把一个java.sql.Date值通过PrepareStatement的setDate方法存入数据库时,java程序会对传入的java.sql.Date规范化,非规范化的部分将会被劫取。 因此,在保存java.util.Date的精确值时,我们需要利用java....

    数据库实验JDBC连接数据库.docx

    PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1, 20); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { System.out.println(rs.getString("PARTKEY") + ": " + rs.getString...

    mysql,sqlserver,oracle三种数据库的大对象存取

    PreparedStatement ps = conn.prepareStatement("INSERT INTO tb_file (filename, filecontent) VALUES (?, ?)"); ps.setString(1, "aaa.exe"); InputStream in = new FileInputStream("d:/aaa.exe"); ps....

    MySQL prepare语句的SQL语法

    PREPARE statement_name FROM preparable_SQL_statement; ``` 其中`statement_name`是你为预处理语句自定义的名称,`preparable_SQL_statement`是你要准备的SQL语句。名称是大小写不敏感的。 2. **EXECUTE语句**...

    教你如何6秒钟往MySQL插入100万条数据的实现

    pstmt = conn.prepareStatement(sql); for (int i = 0; i ; i++) { pstmt.setString(1, (int) (Math.random() * 1000000) + ""); pstmt.setString(2, (int) (Math.random() * 1000000) + ""); pstmt.addBatch...

Global site tag (gtag.js) - Google Analytics