插入时间戳时在Java服务器中获取Postgres错误

时间:2016-03-29 06:39:20

标签: java postgresql jdbc error-handling timestamp

我在Spring启动平台上运行,并且我连接到Postgres数据库。当我尝试执行插入时,出现以下错误:

Database error: org.postgresql.util.PSQLException: ERROR: syntax error at or near "19" Position: 110

我正按以下方式执行代码插入:

@RequestMapping(value="/event", method=RequestMethod.POST)
public @ResponseBody
String uploadEvent(@RequestParam("title") String title,
                   @RequestParam("description") String description,
                   @RequestParam("start") Date start,
                   @RequestParam("end") Date end){
    // Schema
    /*
       StartTime timestamp NOT NULL,
       EndTime timestamp NOT NULL,
       Description varchar(1500),
       name  varchar(80) NOT NULL,
       DisplayPicLoc varchar(80),
       attendenceCount int
     */


    Timestamp startTime = null;
    Timestamp endTime = null;

    try{
        startTime = new Timestamp(start.getTime());
        endTime = new Timestamp(end.getTime());
    }
    catch(Exception e){
        System.err.print("Date conversion error: " + e);
        return "failure";
    }

    System.out.println("Event received with Title: " + title +
            " Description: " + description +
            " Start: " + startTime +
            " End: " + endTime);

    Connection connection = DatabaseConnection.connection;
    if(connection==null){
        new DatabaseConnection();
    }
    try{
        connection.setAutoCommit(false);
        Statement preparedStatement = connection.createStatement();
        String query = "INSERT INTO event " +
                       "(StartTime, EndTime, Description, name, DisplayPicLoc, attendenceCount) " +
                       "VALUES (" + startTime +
                                ", " + endTime +
                                ", " + description +
                                ", " + title +
                                ", null" +
                                ", 0);";

        preparedStatement.executeUpdate(query);
        return "success";
    }
    catch(Exception e){
        System.err.println("Database error: " + e);
        return "failure";
    }

我正在尝试将事件插入到具有以下值的数据库中:

Event received with Title: a Description: a Start: 2016-03-02 19:00:00.0 End: 2016-03-03 19:00:00.0

我收到此错误的原因是什么?

0 个答案:

没有答案