仅打印查询字符串的长度

时间:2014-03-13 04:00:10

标签: java mysql database jdbc

有人能告诉我以下连接有什么问题,我只是想将记录插入到数据库中,出于某种原因,每当我尝试运行程序时,它就会继续打印以下两件事:

the length of the Query String for Error is :506
Value of Catch inside Catch with SQLException ex1:true

我的代码如下:

Statement InsertRemoteResultsErrorLogStmt = null;
ResultSet SelectErrorLogRS = null;
Connection connRemoteforCatch = null;

try {

    String QueryStringInsertIntoErrorLogs = "";

    connRemoteforCatch = DriverManager.getConnection("jdbc:mysql://xx.xx.x.xxx:3306/test", MainUser, MainPass);

    QueryStringInsertIntoErrorLogs = " INSERT INTO simplequeue.errorlogs "
            + " ( "
            + " ErrorNumber_int, "
            + " Created_dt, "
            + " Subject_vch, "
            + " Message_vch, "
            + " TroubleshootingTips_vch, "
            + " CatchType_vch, "
            + " CatchMessage_vch, "
            + " CatchDetail_vch, "
            + " Host_vch, "
            + " Referer_vch, "
            + " UserAgent_vch, "
            + " Path_vch, "
            + " Querystring_vch "
            + " ) "
            + " VALUES "
            + " ( "
            //+ " NULL, "  // ErrorLogIdInt
            + " 2002 , " // ErrorNumber_int
            + " 2014-03-12 14:55:32   , "
            + " SimpleX Error Notification, "
            + " Error Simple X Queue, "
            + " No Troubleshooting Tips Specified, "
            + " testgetsqlstate  , "
            + " testgetmessage ,"
            + " NULL, "
            + " ebmdevii.messagebroadcast.com,"
            + " NULL, "
            + " CFSCHEDULE, "
            + " NULL, "
            + " DBSOURCE=BishopDev, "
            + " ) ";

    System.out.println("the length of the Query String for Error is :" + QueryStringInsertIntoErrorLogs.length());

    InsertRemoteResultsErrorLogStmt = connRemoteforCatch.createStatement();
    InsertRemoteResultsErrorLogStmt.executeUpdate(QueryStringInsertIntoErrorLogs);

} catch (SQLException ex1) {
    System.out.println("Stacktrace below:");
    ex1.printStackTrace();
    System.out.println("Value of Catch inside Catch with SQLException ex1:" + connRemoteforCatch.isValid(2));

}

这是我的Stacktrace:

Stacktrace below:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '14:55:32   ,  SimpleX Error Notification,  Error Simple X Queue,  No Troubleshoo' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
    at mypackagename.Myclassreader.call(Myclassreader.java:492)
    at mypackagename.Myclassreader.call(Myclassreader.java:25)

虽然它在第一行中说SQL语法错误,但我在MySQL Workbench中使用了相同的SQL查询并成功插入了记录。并且在我的Java程序中似乎没有任何语法错误。请指教

更新了Stacktrace:

Stacktrace below:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Error Notification,  Error Simple X Queue,  No Troubleshooting Tips Specified,  ' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

最近的Stacktrace:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
at ebmextractor.Myclassreader.call(Myclassreader.java:493)
at ebmextractor.Myclassreader.call(Myclassreader.java:25)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1259)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

3 个答案:

答案 0 :(得分:2)

我不了解MySQL,但可能你的日期需要至少在引号中 - 可能还附加了一个演员。

           + " '2014-03-12 14:55:32'   , "

编辑:看看this page我猜它可能看起来像:

+ " TIMESTAMP '2014-03-12 14:55:32' , "

但我不知道您要插入

的列的类型

编辑2:

感谢Emanual,我想补充说你还需要引用插入中的其他字符串:

QueryStringInsertIntoErrorLogs = " INSERT INTO simplequeue.errorlogs "
        + " ( "
        + " ErrorNumber_int, "
        + " Created_dt, "
        + " Subject_vch, "
        + " Message_vch, "
        + " TroubleshootingTips_vch, "
        + " CatchType_vch, "
        + " CatchMessage_vch, "
        + " CatchDetail_vch, "
        + " Host_vch, "
        + " Referer_vch, "
        + " UserAgent_vch, "
        + " Path_vch, "
        + " Querystring_vch "
        + " ) "
        + " VALUES "
        + " ( "
        //+ " NULL, "  // ErrorLogIdInt
        + " 2002 , " // ErrorNumber_int
        + " '2014-03-12 14:55:32', "
        + " 'SimpleX Error Notification', "
        + " 'Error Simple X Queue', "
        + " 'No Troubleshooting Tips Specified', "
        + " 'testgetsqlstate'  , "
        + " 'testgetmessage' ,"
        + " NULL, "
        + " 'ebmdevii.messagebroadcast.com',"
        + " NULL, "
        + " 'CFSCHEDULE', "
        + " NULL, "
        + " 'DBSOURCE=BishopDev', "
        + " ) ";

答案 1 :(得分:1)

异常是由于SQL查询的错误形成

尝试将你的字符串放在单个qoute中,

例如。

+"'ebmdevii.messagebroadcast.com'"+

答案 2 :(得分:1)

将您尝试插入的值插入单引号中,但NULL和数值除外。

PROTIP:使用System.out.println查看最终查询字符串,然后尝试在MySQL中运行它。减少JDBC错误消息的混乱。