加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用ConnectionPoolDataSource的连接

发布时间:2020-12-14 05:17:47 所属栏目:Java 来源:网络整理
导读:连接我需要的数据库.我打算使用ConnectionPoolDataSource类.但是如何使用此实例设置有关数据库名称(我希望它连接到哪个)的详细信息.请帮助解决这个问题. 解决方法 尝试阅读 this文档和 example 编辑 刚从上面链接修改了示例 准备步骤: – 下载MySQL Server
连接我需要的数据库.我打算使用ConnectionPoolDataSource类.但是如何使用此实例设置有关数据库名称(我希望它连接到哪个)的详细信息.请帮助解决这个问题.

解决方法

尝试阅读 this文档和 example

编辑

刚从上面链接修改了示例

准备步骤:
– 下载MySQL Server
– 下载mySQL java driver
– 下载Apache Commons Pool
– 下载Commons DBCP
– 打开像MySQL Workbench这样的MySQL客户端,并使用下一个脚本创建数据库

delimiter $$

CREATE DATABASE `test_stackoverflow` /*!40100 DEFAULT CHARACTER SET utf8 */$$

delimiter $$

CREATE TABLE `test_table` (
  `idtest_table` int(11) NOT NULL,`test_field` varchar(45) DEFAULT NULL,PRIMARY KEY (`idtest_table`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$

INSERT INTO `test_stackoverflow`.`test_table` (`idtest_table`,`test_field`) VALUES (1,'test1');
INSERT INTO `test_stackoverflow`.`test_table` (`idtest_table`,`test_field`) VALUES (2,'test2');

>创建java项目,添加到类路径,myscl连接器,池和dbcp(你只需下载所有这些jar)

添加下一个课程

import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
import org.apache.commons.dbcp.datasources.SharedPoolDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author Sergii.Zagriichuk
 */
public class Pool {
    private static DataSource ds;

    static {
        DriverAdapterCPDS cpds = new DriverAdapterCPDS();
        try {
            cpds.setDriver("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        cpds.setUrl("jdbc:mysql://localhost:3306/test_stackoverflow");
        cpds.setUser("root");
        cpds.setPassword("root");

        SharedPoolDataSource tds = new SharedPoolDataSource();
        tds.setConnectionPoolDataSource(cpds);
        tds.setMaxActive(10);
        tds.setMaxWait(50);

        ds = tds;
    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}

用户名和密码应更改为您的数据库用户/密码

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MainClass {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            connection = Pool.getConnection();
            // Do work with connection
            statement = connection.createStatement();
            String selectEmployeesSQL = "select * from test_table";
            resultSet = statement.executeQuery(selectEmployeesSQL);

            while (resultSet.next()) {
                printTestTable(resultSet);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                } // nothing we can do
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                } // nothing we can do
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                } // nothing we can do
            }
        }
    }

    private static void printTestTable(ResultSet resultSet) throws SQLException {
        System.out.print(resultSet.getInt("idtest_table")+",");
        System.out.print(resultSet.getString("test_field"));
    }

}

只需运行main方法,您将看到打印测试值到控制台!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读