谁能看到为什么这个SQL插入不起作用?(字符串和插入字符串的日期例外) - JDBC



我试图基本上记录从Java程序生成的错误,并将它们保存在带有2列的SQL表中(错误和日期)

尝试并捕获将错误发送到应该插入到数据库中但在某处失败的方法。我对 Java 足够新,所以我不确定如何正确调试它或获取有关 wahts 出错以及在哪里的更多详细信息

这是方法。

public void createErrorLog(Exception error) throws SQLException{
        // Error as String
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        error.printStackTrace(pw);
        sw.toString(); // stack trace as a string
        String errorOne = sw.toString();
        System.out.println(errorOne);
        // Date
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        String dateString = dateFormat.format(date);


        Connection conn = null;
        try {       
            conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
            System.out.println("Connected to database.");       
            String insertSaleProperty = "INSERT INTO ErrorLogs"
                    + "(Error, "
                    + "Time, "                  
                    + "(?,?)";
            PreparedStatement preparedStatement = conn.prepareStatement(insertSaleProperty);
            preparedStatement.setString(1, errorOne);
            preparedStatement.setString(2, dateString);
            // execute insert SQL statement
            preparedStatement.executeUpdate();


        } catch (SQLException e) {
            System.err.println("Cannot write to database.");

        } finally {
        if(conn != null){
            conn.close();
          }
        }


    }   

尝试以下操作。

String insertSaleProperty = "INSERT INTO ErrorLogs"
                           + "(Error,Time) values "                  
                           + "(?,?)";
String insertSaleProperty = "INSERT INTO ErrorLogs"
                + "(Error, "
                + "Time, "                  
                + "(?,?)";

应该是:

String insertSaleProperty = "INSERT INTO ErrorLogs "
                + "(Error, Time) "                  
                + "VALUES (?,?)";

最新更新