Spring笔记02 Idea运行示例项目的坑
coconutnut

表建好之后,Idea里面直接打开示例工程跑不起来,mysql的依赖有问题

1
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

对比发现Maven路径不对

这是自己创建的可以运行的Maven项目配置
img

这是导入工程的配置
img

修改路径

1
2
3
/usr/local/Cellar/maven/3.6.2/libexec
/Users/coconutnut/.m2/settings.xml
/Users/coconutnut/.m2/repository

解决Driver问题


下一个bug
Idea中点运行

1
MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server

https://www.cnblogs.com/inconceivable/p/9186822.html

在MySQL Workbench中输入

1
2
3
USE mysql;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'iamgroot';
FLUSH PRIVILEGES;

Idea中提示变成了

1
2
Exception in thread "main" 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:1055)

https://blog.csdn.net/qq1515312832/article/details/85614733

在连接数据库的url后加上编码方式

1
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/groot?useUnicode=true&characterEncoding=utf8","root","iamgroot");

成功


总结:

  1. 修改Preference->Build, Execution, Deployment->Build Tools->Maven中路径
  2. 在连接数据库的url后加上编码方式