mixturer 2015-06-17 17:30 采纳率: 0%
浏览 828

Spring set注入jdbc url为null

使用apacha的basicdatasource连接池,写了一个spring小程序,测试获得mySQL数据库连接,set注入数据库连接的基本参数后,提示错误

Exception in thread "main" java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2023)
    at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1897)
    at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
    at com.alex.util.MySQLDataSource.getConnection(MySQLDataSource.java:43)
    at com.alex.util.test.TestCase.main(TestCase.java:16)
Caused by: java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(Unknown Source)
    at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2007)
    ... 4 more 

后多次修改调试,发现问题出在

    public MySQLDataSource() {
        bds = new BasicDataSource();
        bds.setDriverClassName(driver);
        bds.setUrl(url);
        bds.setUsername(username);
        bds.setPassword(password);
    }

修改成

 public MySQLDataSource() {
        bds = new BasicDataSource();
    }

    @Override
    public Connection getConnection() throws SQLException{
        bds.setDriverClassName(driver);
        bds.setUrl(url);
        bds.setUsername(username);
        bds.setPassword(password);
        return bds.getConnection();
    }

提示可以获得链接,请问这是为什么啊,是注入顺序问题吗,我百度过之后,还是无解,本人小白,求指教。附所有源代码。

applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="http://www.springframework.org/schema/util"

    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        ">

        <bean id="mydatasource" class="com.alex.util.MySQLDataSource">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/jsd1407?useUnicode=true&amp;characterEncoding=utf8"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
        </bean>
</beans>

interface:

 package com.alex.util;

import java.sql.Connection;
import java.sql.SQLException;

public interface DataSource {

    public abstract Connection getConnection() throws SQLException;

    public abstract void closeConnection() throws SQLException;

}

MySQLDataSource:

 package com.alex.util;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp2.BasicDataSource;

public class MySQLDataSource implements DataSource {

    private String driver;
    private String url;
    private String username;
    private String password;
    private BasicDataSource bds;

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public MySQLDataSource() {
        bds = new BasicDataSource();
    }

    @Override
    public Connection getConnection() throws SQLException{
        bds.setDriverClassName(driver);
        bds.setUrl(url);
        bds.setUsername(username);
        bds.setPassword(password);
        return bds.getConnection();
    }

    @Override
    public void closeConnection() throws SQLException{
        bds.close();
    }
}

图片说明

图片说明

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-04 00:09
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    您的代码和配置看起来都是正确的,但您可能遇到的问题在于如何正确地设置BasicDataSource对象。根据您的描述,您尝试将以下属性设置到bds变量中:

    1. driverClassName:设置为com.mysql.jdbc.Driver
    2. url:设置为jdbc:mysql://localhost:3306/jsd1407?useUnicode=true&amp;characterEncoding=utf8
    3. username:设置为root
    4. password:设置为root

    然而,在构造函数中,您只设置了driverClassNameurl,没有设置usernamepassword。因此,当您尝试创建bds对象时,它会抛出异常,因为缺少必要的凭据信息(即用户名和密码)。

    要解决此问题,您可以按照以下方式重写MySQLDataSource类的构造方法:

    public class MySQLDataSource extends BasicDataSource {
    
        public MySQLDataSource() {
            super(); // 使用父类的构造方法初始化其他属性
        }
    
        @Override
        protected void initConnectionProperties() {
            super.initConnectionProperties();
            if (username != null && !"".equals(username)) {
                properties.put("user", username);
            }
            if (password != null && !"".equals(password)) {
                properties.put("password", password);
            }
        }
    }
    

    这样,每当调用initConnectionProperties()方法时,都会检查usernamepassword是否为空,并且不为空时将其添加到properties字典中。这应该可以避免在getConnection()方法中抛出SQLException。请注意,如果您希望在获取连接时自动设置这些凭据,您还可以在getUsername()getPassword()方法上添加适当的逻辑来完成这一工作。

    最后,确保您的MySQLDataSource类继承自BasicDataSource,并在构造方法中初始化BasicDataSource的所有公共属性。

    评论

报告相同问题?

悬赏问题

  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振