Creating report in java with JDBC and trying to co

2019-08-13 18:56发布

问题:

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