通过xml配置文件,连接数据库代码,这样是可以正常的
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/user_db?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="ztbdlb"></property>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
</bean>
后来用了配置类来创建数据库连接,代码如下
@Bean
public DruidDataSource getDruidDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/user_db?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("ztbdlb");
return dataSource;
}
然后就出错了
create connection SQLException, url: jdbc:mysql://localhost:3306/user_db?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC, errorCode 0, state 08001
java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ';allowPublicKeyRetrieval=true&serverTimezone=UTC'.
后来把url修改一下就好了
修改前:jdbc:mysql://localhost:3306/user_db?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
修改后:jdbc:mysql://localhost:3306/user_db
求解释一下吗
