javasqlsql-serverjdbc

I am getting the following error when I am inserting a all the lines of a very large log file into ms sql server 2008


1. The program continues to run for other lines but this error is just for some particular lines . If there are 134789 lines to be inserted , then only around 133666 lines are inserted . The format of all the lines are same as it is in the log file .

 *com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'In'.
            at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
            at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
            at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:792)
            at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:689)
            at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
            at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
            at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
            at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
            at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeUpdate(SQLServerStatement.java:642)
            at Access.insertIntoDatabase(Access.java:186)
            at appWindowLog$1thread1.run(appWindowLog.java:233)
            at java.lang.Thread.run(Unknown Source)*

This is my insert query :

String query =
    "INSERT INTO dbo.logs"
    + " (Host, Mac, Name, Time,"
    + " TimeZone, Request, status,"
    + " size, Referer, SessionId)"
    + " VALUES ('"
    + this.Host + "','" + this.Mac + "','" + this.Name + "','" + t + "','"
    + this.TimeZone + "','" + this.Request + "','" + this.status + "','"
    + this.size + "','" + this.Referer + "','" + this.SessionId + "');";

Solution

  • Consider using PreparedStatement. Your log line probably contains unexpected ' symbol that breaks your insert statement. If you use PreparedStatement it will escape parameters in a correct way.