GBase 8s数据库连接 - C3p0连接池
通过c3p0 连接到GBase 8s数据库
必需组件:
tomcat 7
数据库连接工具:
GBase 8s JDBC dbtjdbc_2.0.1a2_1.jar
开发环境:
Eclipse
配置前提
1,GBase 8s 数据库服务器已经正常启动
1, 在eclipse环境中 新建 动态Web项目,使用Tomcat 7
2, 将gbase 8s的jdbc jar包dbtjdbc_2.0.1a2_1.jar复制到tomcat的lib目录下,c3p0-0.9.5.2.jar和mchange-commons-java-0.2.11.jar也复制到tomcat的lib目录下(也可以放在WebContent/WEB-INF/lib目录下)
3, 在WebContent/META-INF目录下,创建编写context.xml配置文件
具体内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/gbase01_c3p0"
auth="Container"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
factory="org.apache.naming.factory.BeanFactory"
driverClass="com.gbasedbt.jdbc.Driver"
jdbcUrl="jdbc:gbasedbt-sqli://192.168.1.71:9088/c3p0db:GBASEDBTSERVER=gbase01;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8;IFX_LOCK_MODE_WAIT=30"
user="gbasedbt"
password="GBase123"
minPoolSize="5"
maxPoolSize="10"
maxIdleTime="1800"
initialPoolSize="5"
testConnectionOnCheckin="true"
idleConnectionTestPeriod="60"
/>
</Context>
关于更多的c3p0的资源配置,参考:https://www.mchange.com/projects/c3p0/#configuration_properties
4, 在WebContent/WEB-INF目录下,修改web.xml配置文件,增加以下内容
<resource-ref>
<description>C3P0 DB Connection</description>
<res-ref-name>jdbc/gbase01_c3p0</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
5, 在Java Resources/src目录下,创建C3p0.java类,用于数据库连接。
具体内容如下:
package com.gbasedbt.db;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0 {
public static Connection getConn() throws NamingException, SQLException {
Connection connection = null;
InitialContext context = new InitialContext();
ComboPooledDataSource dataSource = (ComboPooledDataSource)context.lookup("java:/comp/env/jdbc/gbase01_c3p0");
connection = dataSource.getConnection();
return connection;
}
}
6, 在WebContent目录下,创建编写testC3p0.jsp动态网页文件
内容如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="com.gbasedbt.db.C3p0"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>C3P0连接到GBase 8s数据库</title>
</head>
<body>
<p>c3p0 连接到GBase 8s数据库</p>
<%
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try{
connection = C3p0.getConn();
statement = connection.createStatement();
resultSet = statement.executeQuery("select first 10 tabname from systables");
while(resultSet.next()){
out.println("表名: " + resultSet.getString(1) + "<br>");
}
} catch (SQLException e){
e.printStackTrace();
} finally {
if(resultSet != null){
try{
resultSet.close();
} catch (Exception e){}
}
if(statement != null){
try{
statement.close();
} catch (Exception e){}
}
if(connection != null){
try{
connection.close();
} catch (Exception e){}
}
}
%>
</body>
</html>
完成配置,测试数据库连接池的连接。