properties.setProperty("username","rozen")
写错参数明了,改成
properties.setProperty("user","rozen")
参数名写的有问题,是 user 不是 username
public static void main(String[] args) throws SQLException {
Driver driver=new com.mysql.cj.jdbc.Driver();
Properties properties=new Properties();
properties.setProperty("user","rozen");
properties.setProperty("password","wdmbsqy2003");
Connection connection=driver.connect("jdbc:mysql://localhost:3306/rozen?serverTimezone=UTC",properties);
String sql="insert into actor values(null,'我的','男','2003-10-11','110')";
Statement statement= connection.createStatement();
int row=statement.executeUpdate(sql);
}
1、用户名和密码不匹配,或者是在连接池里多打了空格之类的
2、可能是用户的权限不够,给他添加权限
3、数据库配置文件jdbc.properties中多了空格。
以前是否在代码里用过leon这个名字?如果有请清理一下编译后代码重新编译
如果项目目录存在target或者out目录,删除它们,然后重新运行就应该好啦
mysql的配置方便发一下?
数据库连上了吗
从报错信息中看 Access denied for user 'Leon'@'localhost
与 这段数据库连接的 username 没有关联。
可尝试从以下几点排查:
在 connection 前后
添加日志打印,看下是否有正常获取?```java
properties.setProperty("user","rozen");
properties.setProperty("password","wdmbsqy2003");
```username换成user即可