Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ReadDateZeroWithJDBC {
- public static void main(String args[]) throws ClassNotFoundException, SQLException {
- Connection conn = setupJDBCAndConnect();
- //expected epoch unix time
- java.util.Date dateExpected = new java.util.Date(0);
- //actual created_at_utc from the user record in DB
- PreparedStatement stmtRead = createSelectStatement(conn, 1);
- ResultSet rs = stmtRead.executeQuery();
- rs.next();
- java.util.Date dateRetrieved = new Date(rs.getTimestamp(3).getTime());
- System.out.println("Dates comparison result:"
- + "\nExpected: " + dateExpected
- + "\nExpected as UTC0: " + dateExpected.toGMTString()
- + "\nExpected as long: " + dateExpected.getTime()
- + "\nActual: " + dateRetrieved
- + "\nActual as UTC0: " + dateRetrieved.toGMTString()
- + "\nActual as long: " + dateRetrieved.getTime());
- conn.close();
- }
- private static PreparedStatement createSelectStatement(Connection conn, int userId) throws SQLException {
- String sql = "SELECT * FROM users WHERE id = ?";
- PreparedStatement stmt = conn.prepareStatement(sql);
- stmt.setInt(1, userId);
- return stmt;
- }
- private static Connection setupJDBCAndConnect() throws ClassNotFoundException, SQLException {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- String url = "jdbc:sqlserver://localhost;databaseName=test";
- String userName = "tester";
- String password = "test";
- return DriverManager.getConnection(url, userName, password);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement