Creating report in java with JDBC and trying to convert between sql date and java util date

夙愿已清 提交于 2019-12-13 02:34:29

问题


I'm making some reports in java which then get added to a jTable after execution of an sql query. However, I have a date format in my table and I can't work out how to convert between the java sql date and java util date. I've tried a few options but I always get this error:

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "DATE" at line 1, column 28.

This is what my statement code looks like:

public void ordersReport() {
    //     if (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) == 1) {
    String sql = "SELECT * FROM ORDERS ORDER DATE ASC, TIME ASC";
    try {
        DefaultTableModel model = (DefaultTableModel) ordTbl.getModel();
        boolean t = model.isCellEditable(1, 1);
        System.out.println(t);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            int ordId = rs.getInt("ORDERID");
            String name = rs.getString("NAME");
            String surname = rs.getString("SURNAME");
            String items = rs.getString("ITEMS");
            float cost = rs.getFloat("COST");
            boolean priority = rs.getBoolean("PRIORITY");
            String status = rs.getString("STATUS");
            java.sql.Date date = rs.getDate("NAME");
            Time time = rs.getTime("TIME");
            int empId = rs.getInt("EMPLOYEEID");
           model.addRow(new Object[]{ordId, name, surname, items, cost, priority, status, date, time, empId});
        }
    } catch (SQLException ex) {
        Logger.getLogger(OwnerDashboard.class.getName()).log(Level.SEVERE, null, ex);
    }

回答1:


You can use this method to convert util date to sql date:

DateUtilities.convertUtilDateToSql(java.util.Date)



回答2:


Okay so I found the error and it was in the string statement that is used for the sql query and I was missing the word BY between ORDER and DATE



来源:https://stackoverflow.com/questions/29430466/creating-report-in-java-with-jdbc-and-trying-to-convert-between-sql-date-and-jav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!