Error querying database.Cause: org.springframework.jdbc.CannotGetJdbcConnectionException报错

    科技2022-07-12  123

    写了Controller类返回json数据文件时候报错

    This application has no explicit mapping for /error, so you are seeing this as a fallback.

    回到idear发现报错: Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user ‘root’@‘localhost’ (using password: YES) The error may exist in edu/xalead/dao/ItemDao.java (best guess) The error may involve edu.xalead.dao.ItemDao.selectAll The error occurred while executing a query Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user ‘root’@‘localhost’ (using password: YES)] with root cause java.sql.SQLException: Access denied for user ‘root’@‘localhost’ (using password: YES)

    猜测应该是数据库连接的问题,去application.yaml文件下查看数据库连接。

    spring: application: name: item-service datasource: url: jdbc:mysql://localhost:3306/

    路径不完整所以连接有问题

    spring: application: name: item-service datasource: url: jdbc:mysql://localhost:3306/jdd

    补全你的数据库路径,结果还是发现同样的错误!

    Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user ‘root’@‘localhost’ (using password: YES)] with root cause java.sql.SQLException: Access denied for user ‘root’@‘localhost’ (using password: YES)

    再看一遍,原来是数据库的用户名和密码的错误! 这波啊,这波是一石二鸟…

    datasource: url: jdbc:mysql://localhost:3306/jdd username: root password:

    把自己的数据库密码和用户名校正就好了!

    最后在刷新一下网页,返回的数据与数据库中的数据匹配即成功。

    Processed: 0.009, SQL: 8