JDBC链接数据库版本三,使用C3P0,使用jar文件两个

时间:2025-03-23 09:31:22

JdbcUtil类:

package ;

import ;
import ;
import ;
import ;
import ;
import .v2.;
public final class JdbcUtil {
	private static ComboPooledDataSource dataSource;
	static {
		dataSource = new ComboPooledDataSource();
	}
	// 取得链接
	public static Connection getMySqlConnection() throws SQLException {
		return ();
	}
	//
	public static DataSource getDataSource(){
		return dataSource;
	}

	// 关闭链接
	public static void close(Connection conn) throws SQLException {
		if (conn != null) {
			try {
				();
			} catch (SQLException e) {
				();
				throw e;
			}
		}
	}

	public static void close(PreparedStatement pstate) throws SQLException {
		if(pstate!=null){
			();
		}
	}
	public static void close(ResultSet rs) throws SQLException {
		if(rs!=null){
			();
		}
	}
	
}


<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
	<default-config>
		<property name="driverClass"></property>
		<property name="user">root</property>
		<property name="password">root</property>
		<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/mysql4</property>
	</default-config>
</c3p0-config>


 

分页的一个dao:

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class Dao {
	private QueryRunner qr = new QueryRunner(());

	// 根据id返回 Customer 对象
	public Customer getCustomerById(int id) throws SQLException {
		String sql = "select * from customer where id = ?";
		Customer cus = (Customer) (sql,
				new BeanHandler(), id);
		return cus;
	}

	// 分页返回
	public List<Customer> getFyList(int start, int size) throws SQLException {
		List<Customer> list = null;
		String sql = "select * from customer limit ?,?";
		list = (sql, new BeanListHandler(), new Object[] {
				start, size });
		return list;
	}

	// 返回记录的总数目
	public int getAllRecordsCount() throws SQLException {
		String sql = "select count(*) from customer";
		Long temp = (sql, new ScalarHandler());
		return ();
	}

	// 根据ID删除指定的记录
	public void deleteRecordById(int id) throws SQLException {
		String sql = "delete from customer where id = ?";
		(sql, id);
	}

	// 根据id更新记录信息
	public void updateRecordById(Customer newCus) throws SQLException {
		String sql = "update customer set name= ?,address= ?,tel= ?,mail= ?,birthday= ? where id= ?";
		(
				sql,
				new Object[] { (), (),
						(), (),
						(), () });
	}

	// 添加记录
	public void addRecord(Customer newCus) throws SQLException {
		String sql = "insert into customer(name,address,tel,mail,birthday) values(?,?,?,?,?)";
		(sql, new Object[] { (), (),
				(), (),
				// //将 转换为 
				// new ( ().getTime())
				() });
	}

}


 

jar文件:c3p0-0.9.1. (关键)  mysql-connector-java-5.1.(关键) 在dao中 使用到commons-dbutils-1.