【问题标题】:Only printing the length of the query string只打印查询字符串的长度
【发布时间】:2014-03-13 04:00:10
【问题描述】:

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

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

【问题讨论】:

  • 添加堆栈跟踪而不是注释掉怎么样,这样我们就可以看到问题所在了?注释打印行并取消注释堆栈跟踪并发布结果。
  • 你能告诉我们堆栈跟踪吗?取消注释ex1.printStackTrace();
  • 我认为您需要阅读Connection#isValid 上的文档以了解它告诉您的内容...
  • @Acapulco 已添加!请检查 !谢谢

标签: java mysql database jdbc


【解决方案1】:

我不了解 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', "
        + " ) ";

【讨论】:

  • 可能是空格引起了问题?我不是 MySql 专家,但可能是 jdbc 驱动程序错误地解析了空格或什么?
  • 将日期更改为 ` + " '2014-03-12 14:55:32' , "` 已将我的堆栈跟踪减少到两行,如我更新的帖子所示。
  • @Jeff - 快到了,但 OP 不仅应该引用日期,还应该引用正在插入的其他字符串。
  • @EmanuelSaringan true - 我只是查看显示错误的具体内容。我会更新我的答案,谢谢
  • @Jeff 我应该像您和文档中提到的那样包括 TIMESTAMP 吗?
【解决方案2】:

将您尝试插入的值用单引号括起来,NULL 和数值除外。

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

【讨论】:

  • 你的意思是我应该做类似System.out.println("Query String:"+QueryStringInsertIntoErrorLogs); 的事情?
  • 不错的提示,由于您的提示,我能够解决最近的错误。感谢您的额外提示。在我的 SQL 查询中的最后一列值之后有一个额外的逗号。
【解决方案3】:

异常是由于SQL查询格式不正确,

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

例如

+"'ebmdevii.messagebroadcast.com'"+

【讨论】:

    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多