0

I created the following table on a MySQL DataBase:

CREATE TABLE DATES(
    DT DATETIME,
    TS TIMESTAMP,
 );

And inserted the following data into the DATES:

INSERT INTO DATES(DT, TS) VALUES(NOW(), NOW());

So when I do the following query:

SELECT * FROM DATES;

The result is the following:

_______DT________|_______TS_________

2017-11-26 12:16:34_|_2017-11-26 12:16:34

Which is correct, each register holds the current date and time of the moment I inserted the data into DATES.

However, when I try to display those values using the following query in Java:

try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/MYDB","root","pswrd");
        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
        ResultSet srs = stmt.executeQuery("SELECT * FROM DATES");
    }
catch(Exception e){
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error.");
    }

I get the following output:

____DT____|____TS____

2017-11-26_|_2017-11-26

That is, the query shows only the incomplete values, as it's only returning the values of the dates stored but not the times.

Is there any way to display both the date & time, as opposed to just the date?

Also, I'm using a JTable to display those values as I cannot find a way to store them in a variable and display them with:

JOptionPane.showMessageDialog(null, "Date & Time: " + <<I want the values here>>);

Any help would be greatly appreciated. Thanks in advance!

AC Zepp
  • 1
  • 1
  • The code above doesn't display the data, it simply executes the SELECT query. Normally you'd loop while(srs.next()) and you'd retrieve each row with srs.getTimestamp(1) or srs.getTimestamp(2). – Hassan Nov 26 '17 at 22:25
  • @Hassan what type of variable should I store srs.getTimestamp(1) in? Thank you! – AC Zepp Nov 26 '17 at 22:48
  • You can store it in a (java.sql.) [Timestamp](https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getTimestamp(int)) object. From what I've read, it supports both DATETIME and TIMESTAMP, though I haven't tried it personally. – Hassan Nov 26 '17 at 22:55

0 Answers0