GBase 8s数据库连接 - Druid连接池
通过Druid连接到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目录下,druid-1.1.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_druid"
auth="Container"
type="javax.sql.DataSource"
factory="com.alibaba.druid.pool.DruidDataSourceFactory"
driverClassName="com.gbasedbt.jdbc.Driver"
url="jdbc:gbasedbt-sqli://192.168.1.71:9088/druiddb:GBASEDBTSERVER=gbase01;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8;IFX_LOCK_MODE_WAIT=30"
username="gbasedbt"
password="GBase123"
validationQuery="select 1 from dual"
validationQueryTimeout="60"
testWhileIdle="true"
initialSize="5"
maxActive="10"
/>
</Context>
关于更多的druid的资源配置,参考:https://github.com/alibaba/druid/wiki/DruidDataSource配置属性列表
4, 在WebContent/WEB-INF目录下,修改web.xml配置文件,增加以下内容
<resource-ref>
<description>DRUID DB Connection</description>
<res-ref-name>jdbc/gbase01_druid</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
5, 在Java Resources/src目录下,创建Druid.java类,用于数据库连接。
具体内容如下:
package com.gbasedbt.db;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.alibaba.druid.pool.DruidDataSource;
public class Druid {
public static Connection getConn() throws NamingException, SQLException {
Connection connection = null;
InitialContext context = new InitialContext();
DruidDataSource dataSource = (DruidDataSource)context.lookup("java:/comp/env/jdbc/gbase01_druid");
connection = dataSource.getConnection();
return connection;
}
}
6, 在WebContent目录下,创建编写testDruid.jsp动态网页文件
内容如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="com.gbasedbt.db.Druid"%>
<%@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>druid 连接到GBase 8s数据库</title>
</head>
<body>
<p>druid 连接到GBase 8s数据库</p>
<%
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try{
connection = Druid.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>
完成配置,测试数据库连接池的连接。
已有 2 条评论