1

Im trying to learn Java programming and how to link it with Database MySQL ...

Here's the Code :

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





}
}

says :

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)

well i've been searching on Google and YouTube and no answer for such a problem ... and i've installed the programs well according to this tutorial step by step ... ( http://www.youtube.com/watch?v=E30_-pQGQXs )

I really need help ....

Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
HitchCoder
  • 29
  • 1
  • 6

2 Answers2

4

The utf8mb4 character set is specific to MySQL. It's explained here. The exception is because the JDBC driver doesn't recognize that charset. One solution is to convert your database encoding to utf8. Another is to use a different JDBC driver. (MySQL Connector/J 5.1.13 and up supports utf8mb4.)

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

Check your database and make sure the whole database + tables + fields have the same charset. This is a real table example, you can see the charset is set for the fields AND for the table. And of course the database was created with a charset too.

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;
Alex Angelico
  • 3,710
  • 8
  • 31
  • 49