weixin_43412497 2020-05-16 21:37 采纳率: 0%
浏览 322

连接mysql数据库提示字符集异常,设置字符集之后还是报错

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.ayit.dao.UserDao;
import cn.edu.ayit.pojo.User;

public class UserDaoImpl implements UserDao {
public User checkUserLoginDao(String uname, String pwd) {

    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    //声明返回变量
    User u = null;
    try {
        //获取连接
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaee?useUnicode=true&characterEncoding=UTF-8", "root", "root");
        //创建sql语句
        String sql = "select * from t_userinfo where uname=? and pwd=?";
        //创建sql命令对象
        ps = conn.prepareStatement(sql);
        //给占位符赋值
        ps.setString(1, uname);
        ps.setString(2, pwd);
        //执行sql
        rs = ps.executeQuery();
        //遍历结果
        while(rs.next()){
            u = new User();
            u.setUid(rs.getInt("uid"));
            u.setUname(rs.getString("uname"));
            u.setPwd(rs.getString("pwd"));
            u.setAge(rs.getInt("age"));
            u.setSex(rs.getString("sex"));
            u.setBirth(rs.getString("birth"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        //释放资源
        if(null != rs){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(null != ps){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(null != conn){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    return u;
}

}
使用的连接驱动版本是mysql-connector-java-5.0.8-bin.jar,数据库mysql版本是8.0以前的项目可以使用这个驱动正常连接且使用,但是这次连接一直报java.sql.SQLException: Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.异常.就算设置了字符集也不行,然而使用了针对mysql8.0的连接驱动同样异常,求大神解围.图片说明

  • 写回答

2条回答 默认 最新

  • 关注

    MySQL5和8有个较大区别是驱动不一样:
    5: com.mysql.jdbc.Driver
    8: com.mysql.cj.jdbc.Driver

    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错