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 pstmt = connection.prepareStatement("INSERT INTO my_table (date_column) VALUES (?)"); pstmt.setDate(1, new java.sql.Date(System.currentTimeMillis())); // 使用当前时间创建Date对象 ...
在上面的代码中,我们首先获取了一个数据库连接,然后我们使用 `prepareStatement` 方法创建了一个 `PreparedStatement` 对象。接着,我们使用 `setString` 方法将字符串参数设置到 SQL 语句中。然后,我们使用 `...
PreparedStatement pstmt = conn.prepareStatement( "INSERT INTO your_table (id, created_at) VALUES (?, ?)"); // 设置参数 pstmt.setInt(1, 1); pstmt.setTimestamp(2, sqlTimestamp); // 或者使用 ts /...
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM users WHERE age = ?"); pstmt.setInt(1, age); ResultSet rs = pstmt.executeQuery(); ``` 在这种情况下,即使我们多次执行该语句并更改`age`的值...
Connection 代表和数据库的连接,其常用方法包括 getMetaData() 返回数据库的 MetaData 数据,createStatement() 创建并返回 Statement 对象,PrepareStatement(String sql) 创建并返回 PrepareStatement 对象。...
- **创建PreparedStatement**: 通过`PreparedStatement pst = (PreparedStatement) con.prepareStatement(sql.toString());`创建PreparedStatement对象,这个对象允许我们执行预编译的SQL语句,并且可以多次执行相同...
PreparedStatement pstmt = con.prepareStatement("INSERT INTO Table1 (a, b) VALUES (?, ?)"); pstmt.setInt(1, 1); pstmt.setString(2, "value"); pstmt.executeUpdate(); ``` ### CallableStatement `...
PreparedStatement ps = connection.prepareStatement("INSERT INTO table_name (date_column) VALUES (?)"); ps.setDate(1, sqlDate); ps.executeUpdate(); ``` 3. **时区处理**:如果数据库字段需要精确到...
PreparedStatement pstmt = connection.prepareStatement(sql); for (Data data : dataList) { pstmt.setString(1, data.getColumn1()); pstmt.setString(2, data.getColumn2()); pstmt.addBatch(); } int[] ...
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语句...
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"); ...
3. 创建PreparedStatement:使用Connection.prepareStatement()方法创建PreparedStatement对象。 4. 设置SQL语句:使用PreparedStatement对象设置批量插入数据的SQL语句。 5. 遍历Map:遍历Map对象,获取每个...
PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, value1); pstmt.setString(2, value2); pstmt.executeUpdate(); // 批量插入 List[]> data = ...; // 包含多条数据 for (String[] row ...
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.sql.Date值通过PrepareStatement的setDate方法存入数据库时,java程序会对传入的java.sql.Date规范化,非规范化的部分将会被劫取。 因此,在保存java.util.Date的精确值时,我们需要利用java....
PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1, 20); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { System.out.println(rs.getString("PARTKEY") + ": " + rs.getString...
PreparedStatement ps = conn.prepareStatement("INSERT INTO tb_file (filename, filecontent) VALUES (?, ?)"); ps.setString(1, "aaa.exe"); InputStream in = new FileInputStream("d:/aaa.exe"); ps....
PREPARE statement_name FROM preparable_SQL_statement; ``` 其中`statement_name`是你为预处理语句自定义的名称,`preparable_SQL_statement`是你要准备的SQL语句。名称是大小写不敏感的。 2. **EXECUTE语句**...
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...