1.报错信息
java.sql.SQLException: Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1672) at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3425) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2045) at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718) at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at com.yg.test.JDBCTest.main(JDBCTest.java:17)
2.原因
这个错误是在使用JDBC连接mysql数据库时没有设置字符集
起因就是在设置JDBC的url时使用的是"jdbc:mysql://localhost:3306/数据库名称"的方式
3.解决方式
将url信息从"jdbc:mysql://localhost:3306/数据库名称"改为"jdbc:mysql://localhost:3306/数据库名称?useUnicode=true&characterEncoding=字符集名称"
注意:
数据库名称为自己项目要连接的数据库名称;
字符集名称为项目连接的数据库使用的编码字符集名称;