DBUtil

  DBUtil初级原始版

package BaseDao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

public class DBUtil {
	public static Connection getConn() throws ClassNotFoundException,
			SQLException {
		// ORACLE的连接方式
		Class.forName("oracle.jdbc.driver.OracleDriver");
		String url = "jdbc:oracle:thin:@localhost:1521:orcl";

		// MYSQL的连接方式
		// Class.forName("com.mysql.jdbc.Driver");
		// String url = "jdbc:mysql://localhost:3306/DatabaseName"; 这个没有验证,错误了要更正。

		String user = "scott";
		String password = "tiger";

		Connection conn = DriverManager.getConnection(url, user, password);
		return conn;
	}

	public static void closeConn(Connection conn, PreparedStatement ps,
			ResultSet rs) throws SQLException {
		if (conn != null)
			conn.close();
		if (rs != null)
			rs.close();
		if (ps != null)
			ps.close();
	}
}

  使用DBCP  注意在main方法中不能使用 在servlet中使用成功

  1.先建立一个db.properties文件在src下边

url=jdbc\:mysql\://localhost\:3306/mr
username=root
password=root
driverName=com.mysql.jdbc.Driver

  2.写一个监听器,在tomcat启动的时候就把连接池就初始化

package Listener;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.dbcp.BasicDataSource;
import java.util.Properties;


import util.DBUtil;

public class InitDataSourceListener implements ServletContextListener{

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		Properties properties = new Properties();
		try {
			properties.load(InitDataSourceListener.class.getClassLoader().getResourceAsStream("db.properties"));
			String url = properties.getProperty("url");
			String username = properties.getProperty("username");
			String password = properties.getProperty("password");
			String driverName = properties.getProperty("driverName");
			BasicDataSource basicDataSource = new BasicDataSource();
			basicDataSource.setUrl(url);
			basicDataSource.setUsername(username);
			basicDataSource.setPassword(password);
			basicDataSource.setDriverClassName(driverName);
			basicDataSource.setInitialSize(30);
			basicDataSource.setMaxActive(10);
			basicDataSource.setMaxIdle(20);  //最大闲置个数
			basicDataSource.setMaxWait(1000);  //最大等待时间
			System.out.println(basicDataSource);
			DBUtil.setDataSource(basicDataSource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}


  3.配置web.xml

<listener>
	<listener-class>Listener.InitDataSourceListener</listener-class>
</listener>


  4.再写DBUtil

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;


public class DBUtil {
	private static DataSource dataSource = null;

	public static DataSource getDataSource() {
		return dataSource;
	}

	public static Connection getConnection(){
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void setDataSource(DataSource dataSource) {
		DBUtil.dataSource = dataSource;
	}


	public static void closeConn(Connection conn, PreparedStatement ps,
			ResultSet rs) throws SQLException {
		if (conn != null)
			conn.close();
		if (rs != null)
			rs.close();
		if (ps != null)
			ps.close();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值