一尘不染

JDBC的残端SQL异常

mysql

我正在尽一切努力使JDBC正常工作,现在让我感到困扰的唯一事情就是这个异常,对此我一无所知:

com.mysql.jdbc.exceptions.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
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1049)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
    com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
    com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
    com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2620)
    com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1890)
    com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3523)
    com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2386)
    com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2163)
    com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
    com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:374)
    com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    java.sql.DriverManager.getConnection(libgcj.so.10)
    java.sql.DriverManager.getConnection(libgcj.so.10)

我正在用它来连接…

Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "username", "password");

非常感谢!


阅读 213

收藏
2020-05-17

共1个答案

一尘不染

这些是相关的部分:

com.mysql.jdbc.exceptions.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
    ...
    com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1890)
    ...
    java.sql.DriverManager.getConnection(libgcj.so.10)

这些问号表示在配置客户端字符集的查询过程中存在严重的字符编码问题。

首次尝试时,打开my.cnf文件并确保存在以下两个条目:

character_set_server=utf8
collation_server=utf8_general_ci

第二次尝试,用更健壮的OpenJDKOracle(Sun)JDK替换GCJ
。众所周知,GCJ有其独特之处。

2020-05-17