Javaweb配置常用的数据源配置

Javaweb配置常用的数据源配置

一、DBCP

DBCP是Apache推出的数据库连接池(Database Connection Pool)。

操作步骤
- 添加jar包:

commons-dbcp-1.4.jar

commons-pool-1.5.6.jar

  • 添加属性资源文件

    在JavaResource目录的src目录下创建一个dbcpconfig.properties文件。

    image

    文件内容:

    driverClassName=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/db_user
    username=root
    password=root
    initialSize=10
    maxActive=50
    maxIdle=20
    minIdle=5
    maxWait=60000
    connectionProperties=useUnicode=true;characterEncoding=utf8
    defaultAutoCommit=true
    defaultReadOnly=
    defaultTransactionIsolation=REPEATABLE_READ
  • 编写数据源工具类

    public class DBUtil {
        public static Connection getConnection(){
            Connection conn = null;
    
            try {
                conn = getDataSource().getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    
            return conn;
        }
    
        private static DataSource getDataSource(){
            DataSource dataSource=null;
            Properties p = new Properties();
            try {
                p.load(DBUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"));
                dataSource = BasicDataSourceFactory.createDataSource(p);
            } catch (Exception e) {
                throw new RuntimeException("获取DataSource对象失败");
            } 
            return dataSource;
        }
    }

二、C3P0

操作步骤

  • 添加jar包

    添加 c3p0-0.9.1.2.jar

  • 编写配置文件

    在JavaResource目录的src目录下创建一个c3p0-config.xml文件。

    image

    c3p0-config.xml文件内容:

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
      <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/db_user</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="initialPoolSize">10</property>
        <property name="maxIdleTime">30</property>
        <property name="maxPoolSize">100</property>
        <property name="minPoolSize">10</property>
      </default-config>
    </c3p0-config>
  • 编写数据源工具类

    public class DBUtil {
        private static DataSource dataSource = new ComboPooledDataSource();
        public static Connection getConnection(){
            Connection conn = null;
            try {
                conn = dataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
        }
    }

三、JavaWeb之Tomcat管理数据源

上面2中方式都需要导入jar包,在JavaWeb服务器Tomcat中其实内置了数据源。所以不需要导入jar包。

Tomcat内置数据源其实也是DBCP,是Tomcat的lib目录下的tomcat-dbcp.jar。

配置数据源的步骤

  • 拷贝数据库连接的jar(mysql-connector-java-5.1.7-bin.jar)到tomcat/lib目录下

    image

  • 配置数据源XML文件

    • 如果把配置信息写在tomcat下的conf目录下context.xml中。

      <Context>
           <Resource name="jdbc/login_register" auth="Container" type="javax.sql.DataSource"
                 maxActive="100" maxIdle="30" maxWait="10000"
                 username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
                 url="jdbc:mysql://localhost:3306/db_user"/>
      </Context>
    • 如果是在当前应用的META-INF中创建context.xml,编写数据源,那么只有当前应用可以使用。

      image

      <Context>
           <Resource name="jdbc/login_register" auth="Container" type="javax.sql.DataSource"
                 maxActive="100" maxIdle="30" maxWait="10000"
                 username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
                 url="jdbc:mysql://localhost:3306/db_user"/>
      </Context>
  • 使用连接池,封装工具类

    public class DBUtil {
        public static Connection getConnection(){
            Connection conn = null;
            try {
                Context c = new InitialContext();
                DataSource dataSource = (DataSource) c.lookup("java:/comp/env/jdbc/login_register");//这里的jdbc/login_register和篇配置文件中的name属性一致
                conn = dataSource.getConnection();
                return conn;
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (NamingException e) {
                e.printStackTrace();
            }
    
            return conn;
        }
    }

【注意】在使用Javaweb配置数据源时,可能会碰到这样的一个错误:

java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z

这是你的Tomcat放置的 mysql-connector-java-x.x.x-bin.jar 版本比较低。下载新的版本就好,mysql-connector-java-5.1.7-bin.jar下载

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值