idea连接MySQL测试报错 与 Unknown initial character set index '255' received from server.

点击右边栏的database
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这里有一点需要注意,就是连接不上,报了个错,错误显示 Connection to stu@localhost failed. [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.
在这里插入图片描述
错误的原因是MySQL的驱动不一致,MySQL用的是 5.+ 的,而在idea中下载的是 8.+ 的,这里只需按照下面选择5.1即可
在这里插入图片描述
再次测试连接,ok
在这里插入图片描述
代码方式:

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class Jdbc {

    Connection connection = null;
    Statement statement = null;

    public void test(){
        try{
            String driver = "com.mysql.jdbc.Driver";
            String URL = "jdbc:mysql://localhost:3306/stu?useSSL=false";
            String USER = "用户名";
            String PASSWORD = "密码";
            Class.forName(driver);
            connection = DriverManager.getConnection(URL,USER,PASSWORD);
            System.out.println("连接成功");
            statement = (Statement) connection.createStatement();
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            String sSQL = "SELECT *  FROM  student";//查询数据库表信息
            ResultSet resultSet = statement.executeQuery(sSQL);
            while (resultSet.next()){
                System.out.println(resultSet.getString(2));
            }
        }catch(Exception e){
            System.out.println(e.getMessage());

        }

    }

    public static void main(String[] args) {
        Jdbc jdbc = new Jdbc();
        jdbc.test();
    }
}

运行这个代码的时候有时候会报错,报错信息为:

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:1073)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
	at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1785)
	at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3496)
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2385)
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
	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(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:411)
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at Jdbc.test(Jdbc.java:18)
	at Jdbc.main(Jdbc.java:39)
null

从而拿到的结果是null值,主要错误是这个:
Initial client character set can be forced via the 'characterEncoding' property.
没有设置这个characterEncoding属性,只要在下面这句话的后面加个**&characterEncoding=utf8**就可以了

String URL = "jdbc:mysql://localhost:3306/stu?useSSL=false&characterEncoding=utf8";

测试:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值