我正在尝试学习Java编程以及如何将其与数据库MySQL链接...
以下是代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class main {
public static void main(String[] args)
throws Exception
{
System.out.println("Loading Driver ..");
System.out.println("Loading Driver ...");
System.out.println("Loading Driver ....");
System.out.println("Loading Driver .....");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/javabook", "root", "1234"); //Connect
System.out.println("Database Connected");
PreparedStatement statement = con.prepareStatement(" SELECT * From acc_types");
ResultSet result = statement.executeQuery();
while (result.next())
{
System.out.println(result.getString(1)+ ""+result.getString(2));
}
}
}
说:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown character set: 'utf8mb4'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
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:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618)
at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1880)
at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3499)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2384)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at main.main(main.java:22)
好吧,我一直在谷歌和YouTube上搜索,没有回答这个问题......我已经按照本教程逐步安装了这些程序...... (http://www.youtube.com/watch?v=E30_-pQGQXs)
我真的需要帮助......
答案 0 :(得分:4)
utf8mb4
字符集特定于MySQL。它解释了here。例外是因为JDBC驱动程序无法识别该字符集。一种解决方案是将数据库编码转换为utf8
。另一种是使用不同的JDBC驱动程序。 (MySQL Connector/J 5.1.13及以上支持utf8mb4
。)
答案 1 :(得分:0)
检查数据库并确保整个数据库+表+字段具有相同的字符集。 这是一个真实的表示例,您可以看到为字段AND为表设置了字符集。当然,数据库也是用charset创建的。
CREATE TABLE `politicas` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Nombre` varchar(250) CHARACTER SET utf8mb4 NOT NULL,
`Texto` text CHARACTER SET utf8mb4 NOT NULL,
`Lng` varchar(2) CHARACTER SET utf8mb4 NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;