常用的数据源配置
1、DBCP
DBCP:Apache推出的Database Connection Pool
使用步骤:
> 添加jar包 commons-dbcp-1.4.jar commons-pool-1.5.6.jar
> 添加属性资源文件 dbcpconfig.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day13
username=root
password=abc
#
initialSize=10
#
maxActive=50
#
maxIdle=20
minIdle=5
maxWait=60000
connectionProperties=useUnicode=true;characterEncoding=utf8
defaultAutoCommit=true
defaultReadOnly=
defaultTransactionIsolation=REPEATABLE_READ
> 编写数据源工具类
public class DBCPUtil {private static DataSource ds = null;
static{
Properties prop = new Properties();
try {
prop.load(DBCPUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"));//根据DBCPUtil的classes的路径,加载配置文件
ds = BasicDataSourceFactory.createDataSource(prop);//得到一个数据源
} catch (Exception e) {
throw new ExceptionInInitializerError("初始化错误,请检查配置文件");
}
}
public static Connection getConnection(){
try {
return ds.getConnection();
} catch (SQLException e) {
throw new RuntimeException("服务器忙。。。");
}
}
public static void release(Connection conn,Statement stmt,ResultSet rs){
//关闭资源
if(rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();//关闭
} catch (Exception e) {
e.printStackTrace();
}
conn = null;
}
}
}
测试类
public class TestJDBC {
@Test
public void test1(){
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBCPUtil.getConnection();
ps = conn.prepareStatement("...");
// ...
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBCPUtil.release(conn, ps, null);
}
}
}
.2、C3P0
使用步骤:
1、添加jar包
2、编写配置文件
c3p0-config.xml,放在classpath中,或classes目录中
<?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/day13</property>
<property name="user">root</property>
<property name="password">abc</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>
3、编写工具类:
public class C3P0Util {
//得到一个数据源
private static DataSource dataSource = new ComboPooledDataSource();
//从数据源中得到一个连接对象
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("服务器错误");
}
}
public static void release(Connection conn,Statement stmt,ResultSet rs){
//关闭资源
if(rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();//关闭
} catch (Exception e) {
e.printStackTrace();
}
conn = null;
}
}
}
测试类:
public class TestCRUD {
@Test
public void testInsert(){
Connection conn = null;
PreparedStatement ps = null;
try {
conn = C3P0Util.getConnection();
ps = conn.prepareStatement("insert into account(name,money) values('ggg',2000)");
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
C3P0Util.release(conn, ps, null);
}
System.out.println(conn.getClass().getName());
}
}
用JavaWeb服务器管理数据源:Tomcat
开发JavaWeb应用,必须使用一个JavaWeb服务器,JavaWeb服务器都内置数据源。
Tomcat:(DBCP)
数据源只需要配置服务器即可。
配置数据源的步骤:
1、拷贝数据库连接的jar到tomcatlib目录下
2、配置数据源XML文件
a)如果把配置信息写在tomcat下的conf目录的context.xml中,那么所有应用都能使用此数据源。
b)如果是在当前应用的META-INF中创建context.xml,编写数据源,那么只有当前应用可以使用。
contxt.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/pn" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="abc" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/pn"/>
</Context>
3、使用连接池
<%
Context initContext = new InitialContext();
DataSource ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/databasename");
Connection conn = ds.getConnection();
out.print(conn);
%>
JNDI:java nameing directory interface
JNDI容器就是一个Map
key(String) | value(Object) |
path+name | 对象 |
path+"jdbc/databasename" | DataSource对象 |