Eclipse使用jdbc连接MySql数据库报:java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
在使用eclipse连接mysql数据库时报异常:
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
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(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
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:571)
at java.sql.DriverManager.getConnection(DriverManager.java:233)
at cn.itcast.mybatis.jdbc.jdbcTest.main(jdbcTest.java:36)
jdk版本:1.7.0_79,mysql为5.7,使用mysql-connector-java-5.1.18.jar。
代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; /**
*
* @author lilia
*
*/
public class jdbcTest {
//mysql数据库地址
private static final String url = "jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8";
//mysql数据库用户名
private static final String username = "root";
//myslq数据库密码
private static final String password = "root"; public static void main(String[] args) {
//数据库连接
Connection connection = null;
//预编译的statement(使用预编译的statement可以提高数据库的性能)
PreparedStatement preparedStatement = null;
//结果集对象
ResultSet resultSet = null; try {
//加载数据驱动
Class.forName("com.mysql.jdbc.Driver");
//通过驱动管理类获取数据库连接
connection = DriverManager.getConnection(url, username, password);
//定义sql语句
String sql = "select * from user where username = ?";
//获取预处理statement,并把sql放入到statement中。
preparedStatement = connection.prepareStatement(sql);
//参数赋值,序号从1开始
preparedStatement.setString(1, "王五");
//向数据库发出sql执行查询,并返回查询结果集
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getString("id") + ":" + resultSet.getString("username"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement != null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
执行后报错如上。
网上搜索很多解决方案行不通,最终找到问题所在:root帐户默认不开放远程访问权限,所以需要修改一下相关权限。,参考:https://blog.csdn.net/jack__love/article/details/79019049
具体解决步骤如下:
- 打开MySQL目录下的my.ini文件(win10默认安装在C:\ProgramData\MySQL\MySQL Server 5.7\my.ini),在文件的最后添加一行“skip-grant-tables”,保存并关闭文件。
- 重启mysql服务,通过服务管理器(服务名称:mysql57)或者cmd(>net stop mysql57 >net start mysql57)都行。
- 通过命令行进入MySQL的安装目录BIN下(WIN10默认安装,BIN目录为:C:\Program Files\MySQL\MySQL Server 5.7\bin,如果配置过mysql环境变量可不进入该目录,直接执行命令),输入“mysql -u root -p”(不输入密码),提示输入密码不用管,直接Enter回车即可进入数据库。
- 执行“use mysql;”,使用mysql数据库。
- 执行命令“update user set authentication_string=PASSWORD("root") where user='root';”(修改root的密码)。
- 打开MySQL目录下的my.ini文件,删除最后一行的“skip-grant-tables”,保存并关闭文件。
- 重启MySQL服务。
- 重新执行代码,执行成功。

问题解决。
Eclipse使用jdbc连接MySql数据库报:java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)的更多相关文章
- 连接mysql时提示java.sql.SQLException: Access denied for user 'root'@'DESKTOP-N2B2D9A' (using password: YES)
用root连接mysql时提示:访问被拒绝 检查一下mysql server是否开启,发现后台在运行着.. 然后查了一下mysql的用户表,发现root只能运行使用本地ip(localhost或者1 ...
- 数据库异常 :java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
最近在新项目中突然出现了 java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) ...
- 技术笔记1:java.sql.SQLException: Access denied for user 'root'@'localhost' (using password)
在myEclipse10中运行java项目的时候,遇到java.sql.SQLException: Access denied for user 'root'@'localhost' (using p ...
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
在更新项目之后,做了一定的改动后发现竟然报错了,刚才还好好的. java.sql.SQLException: Access denied for user 'root'@'localhost' (us ...
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) 解决办法
一.背景 在Spark中,将DStream写入到MySQL出现错误:java.sql.SQLException: Access denied for user 'root'@'localhost' ( ...
- [Spring MVC - 2A] - java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
严重: Servlet.service() for servlet [springMVC] in context with path [/ExceptionManageSystem] threw ex ...
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)解决方案
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at com.mysql. ...
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
:: - [localhost-startStop-] INFO - Root WebApplicationContext: initialization started -- :: - [local ...
- mysql jdbc连接时的小问题java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
这次重新修改老程序时出现了上面的错误,排查过后最终找到问题所在:root帐户默认不开放远程访问权限,所以需要修改一下相关权限. 打开MySQL目录下的my.ini文件(win10默认安装在C:\Pro ...
随机推荐
- 安装twisted遇到的坑
在使用twisted框架的时候,我们需要知道他是干什么的? twisted支持很多种协议,包括传输层的TCP, UDP, TLS和引用层的HTTP和FTP等. twisted框架其主要发行版本是以p ...
- gitlab docker中postgresql远程访问配置
1.配置postgresql远程访问 配置postgresql远程访问,需要修改两个文件,在gitlab-ce的docker中位置为 /var/opt/gitlab/postgresql/data 首 ...
- xgboost调参过程
from http://blog.csdn.net/han_xiaoyang/article/details/52665396
- sh_06_函数的返回值
sh_06_函数的返回值 def sum_2_num(num1, num2): """对两个数字的求和""" result = num1 + ...
- zabbix配置通过远程命令来发送邮件
1.安装好zabbix后,在/var/log/zabbix可以查看日志. 2.主机通过zabbix-get检查 yum install zabbix-get -y zabbix-get -s 客户主 ...
- yum install ntp 报错:Error: Package: ntp-4.2.6p5-25.el7.centos.2.x86_64 (base)
redhat7 在安装ntp时报如下错误 Error: Package: ntp-4.2.6p5-25.el7.centos.2.x86_64 (base) Requires: ntpdate = 4 ...
- 从Java中的length和length()开始
1.在没有IDE自动补齐的情况下,怎样得到数组的长度?怎样得到字符串的长度? int[] arr = new int[3]; System.out.println(arr.length);//leng ...
- Mybaits二级缓存的使用与配置
什么是延迟加载 resultMap中的association和collection标签具有延迟加载的功能. 延迟加载的意思是说,在关联查询时,利用延迟加载,先加载主信息.使用关联信息时再去加载关联信 ...
- 如何获得select被选中option的value和text和其他属性值
比如这个: <select id="select"> <option value="A" url="http://www.baidu ...
- Android Studio 安装 Flutter
1 下载sdk https://flutter.dev/docs/get-started/install/windows 2 解压到自定义文件夹,并配置bin路径到环境变量path中 path添加 ...