在使用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

具体解决步骤如下:

  1. 打开MySQL目录下的my.ini文件(win10默认安装在C:\ProgramData\MySQL\MySQL Server 5.7\my.ini),在文件的最后添加一行“skip-grant-tables”,保存并关闭文件。
  2. 重启mysql服务,通过服务管理器(服务名称:mysql57)或者cmd(>net stop mysql57  >net start mysql57)都行。
  3. 通过命令行进入MySQL的安装目录BIN下(WIN10默认安装,BIN目录为:C:\Program Files\MySQL\MySQL Server 5.7\bin,如果配置过mysql环境变量可不进入该目录,直接执行命令),输入“mysql -u root -p”(不输入密码),提示输入密码不用管,直接Enter回车即可进入数据库。
  4. 执行“use mysql;”,使用mysql数据库。
  5. 执行命令“update user set authentication_string=PASSWORD("root") where user='root';”(修改root的密码)。
  6. 打开MySQL目录下的my.ini文件,删除最后一行的“skip-grant-tables”,保存并关闭文件。
  7. 重启MySQL服务。
  8. 重新执行代码,执行成功。

问题解决。

Eclipse使用jdbc连接MySql数据库报:java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)的更多相关文章

  1. 连接mysql时提示java.sql.SQLException: Access denied for user 'root'@'DESKTOP-N2B2D9A' (using password: YES)

    用root连接mysql时提示:访问被拒绝 检查一下mysql server是否开启,发现后台在运行着..  然后查了一下mysql的用户表,发现root只能运行使用本地ip(localhost或者1 ...

  2. 数据库异常 :java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

    最近在新项目中突然出现了  java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) ...

  3. 技术笔记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 ...

  4. java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)

    在更新项目之后,做了一定的改动后发现竟然报错了,刚才还好好的. java.sql.SQLException: Access denied for user 'root'@'localhost' (us ...

  5. java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) 解决办法

    一.背景 在Spark中,将DStream写入到MySQL出现错误:java.sql.SQLException: Access denied for user 'root'@'localhost' ( ...

  6. [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 ...

  7. 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. ...

  8. java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

    :: - [localhost-startStop-] INFO - Root WebApplicationContext: initialization started -- :: - [local ...

  9. mysql jdbc连接时的小问题java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

    这次重新修改老程序时出现了上面的错误,排查过后最终找到问题所在:root帐户默认不开放远程访问权限,所以需要修改一下相关权限. 打开MySQL目录下的my.ini文件(win10默认安装在C:\Pro ...

随机推荐

  1. xDSL相关

    ----------------------- --------------

  2. 在centos7.5使用DockerFile构建镜像时报错“Error parsing reference: "microsoft/dotnet:2.2-aspnetcore-runtime AS base" is not a valid repository/tag: invalid reference format”

    运行dockerfile时报出的错误 FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base Error parsing reference: &qu ...

  3. tf.expand_dims和tf.squeeze函数

    from http://blog.csdn.net/qq_31780525/article/details/72280284 tf.expand_dims() Function tf.expand_d ...

  4. php大文件分片上传插件

    PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...

  5. 6.LINUX32位和64位系统的区别和选择

  6. Java面试题系列(六)优化tomcat配置

    序言 资料 如何优化tomcat配置(从内存.并发.缓存3个方面)优化

  7. PHP 大文件上传,支持断点续传,求具体方案、源码或者文件上传插件

    文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...

  8. 微信小程序访问后台出现 对应的服务器证书无效。控制台输入 showRequestInfo() 可以获取更详细信息。

    检查微信开发者平台配置 https 服务端 nginx 配置 ssl 协议是否有效 在开发者工具中可以使用(详情 > 不校验合法域名.web-view(业务域名).TLS 版本以及 HTTPS ...

  9. Oracle 字符串拼接会出现0自动忽略,解决方案

    解决方案 ,),'||num,num) from table_name 参考:https://blog.csdn.net/menghuannvxia/article/details/73089903

  10. Unity PlayerPrefs 存储的位置

    Mac OS 在Mac OS X上PlayerPrefs是存储在~/Library/Preferences文件夹,名为unity.[company name].[product name].plist ...