安装什么的问题就不说了,就记录一下连接数据库失败的一些解决方法。
一..报错。com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client
public static void main(String[] args) throws ClassNotFoundException {
String driver ="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/myblog;
String username="localhost";
String password="123456";
Class.forName(driver);
getConn(url, username, password);
}
public static Connection getConn(String url, String username,String password){
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
System.out.println("数据库连接成功!!");
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("数据库连接失败!!");
}
return conn;
}
这种报错可能就是用户和密码的问题,如果不知道的可以创建一个新的用户进行连接。创建方法如下:
1.打开命令行cmd
2.开启数据库:net start Mysql
3.登陆数据库
4.更改数据库的加密方式输入:ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
5.更改密码: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123';
6.刷新权限:FLUSH PRIVILEGES;
7.在客户端就可连接成功。
二.可能eclipse会继续报错
Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
这个就是url的路径出问题了,编码格式不对需要在后面添加?useUnicode=true&characterEncoding=utf8";
String url="jdbc:mysql://localhost:3306/myblog?useUnicode=true&characterEncoding=utf8";
这样之后就没问题了。