0

Im trying to basically make a log of errors generated from a Java program and save them in an SQL table with 2 colums( Error and Date )

Try and catch sends the error to the method which should hopefully insert into database but its failing somewhere. Im new enough to Java so im not sure how to debug it properly or get more details on wahts going wrong and where

Heres the method.

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();

          }

        }




    }   
4

2 回答 2

4

试试下面的。

String insertSaleProperty = "INSERT INTO ErrorLogs"
                           + "(Error,Time) values "                  
                           + "(?,?)";
于 2013-10-15T17:32:59.610 回答
3
String insertSaleProperty = "INSERT INTO ErrorLogs"
                + "(Error, "
                + "Time, "                  
                + "(?,?)";

应该是:

String insertSaleProperty = "INSERT INTO ErrorLogs "
                + "(Error, Time) "                  
                + "VALUES (?,?)";
于 2013-10-15T17:32:53.323 回答