当前位置: 首页 > 工具软件 > BoneCP > 使用案例 >

bonecp mysql_bonecp数据库连接

杜苏燕
2023-12-01

bonecp相关包bonecp-0.7.0.jar

bonecp-provider-0.7.0.jar

bonecp-spring-0.7.0.jar

google-collections-1.0.jar

slf4j-api-1.6.1.jar

slf4j-log4j12-1.6.1.jar

2. 数据库相关配置bonecp-config.xml

jdbc:mysql://10.120.105.42:3306/db_tsp?characterEncoding=UTF-8&useUnicode=TRUE&autoReconnect=true

zhangsan

tsp123

3

5

1

1

60

3. java 代码获取连接

public class ConnectionPool {

private static final Logger logger = LoggerFactory.getLogger(ConnectionPool.class);

private static BoneCP connectionPool = null;

private static Connection connection = null;

static {

try {

Class.forName("com.mysql.jdbc.Driver");

BoneCPConfig config = null;

config = new BoneCPConfig("bonecp-config.xml");

connectionPool = new BoneCP(config);

} catch(Exception e) {

logger.debug("连接数据库异常", e);

}

}

public static Connection getConnection() throws Exception {

if (connectionPool != null) {

connection = connectionPool.getConnection();

return connection;

}

throws ...

}

}

 类似资料: