0

I have a list of Orders which has start date and end date, when i add the date, the program input the true date but when i try to import the data from SQL server, the date i get is auto minus 2 days compare to the date in the database

My code for importing data

List<Order> olist = new LinkedList<>();
    String sql = "SELECT o.oId,o.caId,o.cId,o.startdate,o.enddate,o.pay,o.ohiringprice,c.cName,c.cPhone,c.cAddress,ct.tType,ct.tModel FROM Orders as o INNER JOIN Customers as c ON o.cId=c.cId INNER JOIN Cars as ca ON o.caId=ca.caId INNER JOIN CarTypes as ct ON ca.tId =ct.tId";
    ResultSet rs = ConnectSQL.getPreparedStatement(sql).executeQuery();
    while (rs.next()) {
        Order o = new Order();
        o.oId = rs.getInt("oId");
        o.caId = rs.getInt("caId");
        o.cId = rs.getInt("cId");
        o.startdate = rs.getString("startdate");
        o.enddate = rs.getString("enddate");
        o.pay = rs.getString("pay");
        o.ohiringprice = rs.getFloat("ohiringprice");
        o.cName = rs.getString("cName");
        o.cPhone = rs.getString("cPhone");
        o.cAddress = rs.getString("cAddress");
        o.type = rs.getString("tType");
        o.model = rs.getString("tModel");
        System.out.println(o.startdate);
        olist.add(o);
    }
    return olist;
}

Here is the result for the start date

2017-06-14
2017-06-14
2017-06-14
2017-06-14
2017-06-14
2017-05-13
2017-05-13
2017-05-13

The table for Order in my SQL

create table Orders(
oId int IDENTITY(1,1) primary key,
caId int,
foreign key (caId) references Cars(caId),
cId int,
foreign key (cId) references Customers(cId),
startdate DATE not null,
enddate   DATE not null,
pay nvarchar(50) not null,
ohiringprice float not null
)

The real startdate records in the database

2017-06-16
2017-06-16
2017-06-16
2017-06-16
2017-06-16
2017-05-15
2017-05-15
2017-05-15

it's also happened with the endate too

for the library i'm using sqljdbc4 and sdk1.8, SQL server version is 2014

Vinh Nguyễn
  • 71
  • 2
  • 10
  • Strange. Why do you get the dates as strings from the result set? If you have a new JDBC driver (JDBC 4.2), you should be able to get them as `LocalDate` through the `getObject` method. If you can, I’d be curious whether it makes any difference. – Ole V.V. Jun 11 '17 at 18:29
  • can you show me some specific code , thanks u very much – Vinh Nguyễn Jun 11 '17 at 18:31
  • It’s in [this question: Getting the date from a ResultSet for use with java.time classes](https://stackoverflow.com/questions/29773390/getting-the-date-from-a-resultset-for-use-with-java-time-classes) (and its answers). – Ole V.V. Jun 11 '17 at 18:34
  • 1
    thank you it auto fix when i added the jdbc verson 4.2 , i didn't need to do anything – Vinh Nguyễn Jun 11 '17 at 18:37
  • Makes me curious what went wrong with the old one; but glad it works! – Ole V.V. Jun 11 '17 at 18:42
  • 1
    i check around and some people also have the same problem with me but they use older version which is jdbc 3.0 and after they up to version 4.0 it work, i wonder why i need to up to version 4.2 to fix this – Vinh Nguyễn Jun 11 '17 at 18:43

0 Answers0