⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconn.java

📁 Spring2.0宝典
💻 JAVA
字号:
package lee;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
 * @author  yeeku.H.lee kongyeeku@163.com
 * @version  1.0
 * <br>Copyright (C), 2005-2008, yeeku.H.Lee
 * <br>This program is protected by copyright laws.
 * <br>Program Name:
 * <br>Date: 
 */
public class DBConn
{
	private static DBConn dc;
	private Connection conn = null;
	private Statement stmt = null;
	private DBConn()
	{
	}
	public static DBConn instance()
	{
		if (dc == null)
		{
			dc = new DBConn();
		}
		return dc;
	}

	public Statement openStmt()
	{
		if (stmt == null)
		{
			conn = getConn();
			try
			{
				stmt = conn.createStatement();
			}
			catch (Exception e)
			{
				System.err.println("创建Statement异常: " + e.getMessage());
			}
		}
		return stmt;
	}
	public void closeStmt()
	{
		if (stmt != null)
		{
			try
			{
				stmt.close();
			}
			catch (Exception e)
			{
				System.err.println("Statement关闭异常");
			}
		}
		if (conn != null)
		{
			try
			{
				conn.close();
			}
			catch (Exception e)
			{
				System.err.println("数据库关闭异常");
			}
		}
	}
	
	public Connection getConn()
	{
		if (conn == null)
		{
			try
			{
				ComboPooledDataSource ds = new ComboPooledDataSource();
				ds.setDriverClass("com.mysql.jdbc.Driver");
				ds.setJdbcUrl("jdbc:mysql://localhost:3306/j2ee");
				ds.setUser("root");
				ds.setPassword("32147");
                ds.setMaxPoolSize(40);
                ds.setMinPoolSize(2); 
                ds.setMaxStatements(180); 
				conn = ds.getConnection();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		return conn;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -