1

我正在尝试学习 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)

好吧,我一直在 Google 和 YouTube 上搜索,但没有解决此类问题……我已经按照本教程一步一步地安装了程序……(http://www.youtube.com/watch ?v=E30_-pQGQXs )

我真的需要帮助....

4

2 回答 2

4

utf8mb4字符集特定于 MySQL 。这里解释了。例外是因为 JDBC 驱动程序无法识别该字符集。一种解决方案是将您的数据库编码转换为utf8. 另一种是使用不同的 JDBC 驱动程序。(MySQL 连接器/J 5.1.13及更高版本支持utf8mb4。)

于 2012-03-26T21:10:25.430 回答
0

检查您的数据库并确保整个数据库 + 表 + 字段具有相同的字符集。这是一个真实的表格示例,您可以看到为字段和表格设置了字符集。当然,数据库也是用字符集创建的。

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;
于 2013-09-04T22:24:44.267 回答