Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. public class ReadDateZeroWithJDBC {
  2. public static void main(String args[]) throws ClassNotFoundException, SQLException {
  3. Connection conn = setupJDBCAndConnect();
  4.  
  5. //expected epoch unix time
  6. java.util.Date dateExpected = new java.util.Date(0);
  7.  
  8. //actual created_at_utc from the user record in DB
  9. PreparedStatement stmtRead = createSelectStatement(conn, 1);
  10. ResultSet rs = stmtRead.executeQuery();
  11. rs.next();
  12. java.util.Date dateRetrieved = new Date(rs.getTimestamp(3).getTime());
  13.  
  14. System.out.println("Dates comparison result:"
  15. + "\nExpected: " + dateExpected
  16. + "\nExpected as UTC0: " + dateExpected.toGMTString()
  17. + "\nExpected as long: " + dateExpected.getTime()
  18. + "\nActual: " + dateRetrieved
  19. + "\nActual as UTC0: " + dateRetrieved.toGMTString()
  20. + "\nActual as long: " + dateRetrieved.getTime());
  21.  
  22. conn.close();
  23. }
  24.  
  25. private static PreparedStatement createSelectStatement(Connection conn, int userId) throws SQLException {
  26. String sql = "SELECT * FROM users WHERE id = ?";
  27. PreparedStatement stmt = conn.prepareStatement(sql);
  28. stmt.setInt(1, userId);
  29. return stmt;
  30. }
  31.  
  32. private static Connection setupJDBCAndConnect() throws ClassNotFoundException, SQLException {
  33. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  34. String url = "jdbc:sqlserver://localhost;databaseName=test";
  35. String userName = "tester";
  36. String password = "test";
  37. return DriverManager.getConnection(url, userName, password);
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement