JDBC链接数据库版本三,使用C3P0,使用jar文件两个
JdbcUtil类:
- package com.xiaohui.jdbc.util;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- public final class JdbcUtil {
- private static ComboPooledDataSource dataSource;
- static {
- dataSource = new ComboPooledDataSource();
- }
- // 取得链接
- public static Connection getMySqlConnection() throws SQLException {
- return dataSource.getConnection();
- }
- //
- public static DataSource getDataSource(){
- return dataSource;
- }
- // 关闭链接
- public static void close(Connection conn) throws SQLException {
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- throw e;
- }
- }
- }
- public static void close(PreparedStatement pstate) throws SQLException {
- if(pstate!=null){
- pstate.close();
- }
- }
- public static void close(ResultSet rs) throws SQLException {
- if(rs!=null){
- rs.close();
- }
- }
- }
c3p0-config.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <c3p0-config>
- <default-config>
- <property name="driverClass">com.mysql.jdbc.Driver</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 com.xiaohui.cusSys.dao;
- import java.sql.SQLException;
- import java.util.List;
- import org.apache.commons.dbutils.QueryRunner;
- import org.apache.commons.dbutils.handlers.BeanHandler;
- import org.apache.commons.dbutils.handlers.BeanListHandler;
- import org.apache.commons.dbutils.handlers.ScalarHandler;
- import com.xiaohui.cusSys.domain.Customer;
- import com.xiaohui.cusSys.util.JdbcUtil;
- public class Dao {
- private QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
- // 根据id返回 Customer 对象
- public Customer getCustomerById(int id) throws SQLException {
- String sql = "select * from customer where id = ?";
- Customer cus = (Customer) qr.query(sql,
- new BeanHandler(Customer.class), id);
- return cus;
- }
- // 分页返回
- public List<Customer> getFyList(int start, int size) throws SQLException {
- List<Customer> list = null;
- String sql = "select * from customer limit ?,?";
- list = qr.query(sql, new BeanListHandler(Customer.class), new Object[] {
- start, size });
- return list;
- }
- // 返回记录的总数目
- public int getAllRecordsCount() throws SQLException {
- String sql = "select count(*) from customer";
- Long temp = qr.query(sql, new ScalarHandler());
- return temp.intValue();
- }
- // 根据ID删除指定的记录
- public void deleteRecordById(int id) throws SQLException {
- String sql = "delete from customer where id = ?";
- qr.update(sql, id);
- }
- // 根据id更新记录信息
- public void updateRecordById(Customer newCus) throws SQLException {
- String sql = "update customer set name= ?,address= ?,tel= ?,mail= ?,birthday= ? where id= ?";
- qr.update(
- sql,
- new Object[] { newCus.getName(), newCus.getAddress(),
- newCus.getTel(), newCus.getMail(),
- newCus.getBirthday(), newCus.getId() });
- }
- // 添加记录
- public void addRecord(Customer newCus) throws SQLException {
- String sql = "insert into customer(name,address,tel,mail,birthday) values(?,?,?,?,?)";
- qr.update(sql, new Object[] { newCus.getName(), newCus.getAddress(),
- newCus.getTel(), newCus.getMail(),
- // //将java.util.Date 转换为 java.sql.Date
- // new java.sql.Date( newCus.getBirthday().getTime())
- newCus.getBirthday() });
- }
- }
jar文件:c3p0-0.9.1.2.jar (关键) mysql-connector-java-5.1.22-bin.jar(关键) 在dao中 使用到commons-dbutils-1.5.jar
- 顶
JDBC链接数据库版本三,使用C3P0,使用jar文件两个的更多相关文章
- jdbc链接数据库的三种方式
/** * jdbc连接数据库 * @author APPle * */ public class Demo1 { //连接数据库的URL private String url = "jdb ...
- 4、原生jdbc链接数据库常用资源名
原生jdbc链接数据库要素:#MySql:String url="jdbc:mysql://localhost:3306/数据库名";String name="root& ...
- jdbc链接数据库,获取表名,字段名和数据
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import ...
- 分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件
原文:分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; public class Zip ...
- JDBC操作数据库的三种方式比较
JDBC(java Database Connectivity)java数据库连接,是一种用于执行上sql语句的javaAPI,可以为多种关系型数据库提供统一访问接口.我们项目中经常用到的MySQL. ...
- JDBC链接数据库步骤
java中定义链接数据库的标准:JDBC 1.导包:不同数据库有不同的jdbc驱动包,而且jdbc驱动包和数据库版本必须对应 2.测试 3.写代码 try { 1.//加载JDBC驱动 Clas ...
- jdbc链接数据库
JDBC简介 JDBC全称为:Java Data Base Connectivity (java数据库连接),可以为多种数据库提供填统一的访问.JDBC是sun开发的一套数据库访问编程接口,是一种SQ ...
- Java JDBC链接数据库
1.注册驱动Class.forname("com.mysql.jdbc.Driver");//这是连接mysql数据库的驱动2.获取数据库连接java.sql.Connectio ...
- 1019 JDBC链接数据库进行修删改查
package com.liu.test01; import java.sql.Statement; import java.sql.Connection; import java.sql.Drive ...
随机推荐
- an error occurred during the file system check错误的解决
[root@GIT ~]# fsck -A /dev/mapper/VolGroup-lv_root 下面的选择,一路Y就行了,最后reboot,问题解决!
- MySQL常用的自带函数
MySQL自带函数十分丰富,合理使用可以减少很多编码工作. >>数学函数 数学函数主要用于处理数字,包括整型.浮点数等.数学函数包括绝对值函数.正弦函数.余弦函数.获取随机数的函数等.AB ...
- .net学习之多线程、线程死锁、线程通信 生产者消费者模式、委托的简单使用、GDI(图形设计接口)常用的方法
1.多线程简单使用(1)进程是不执行代码的,执行代码的是线程,一个进程默认有一个线程(2)线程默认情况下都是前台线程,要所有的前台线程退出以后程序才会退出,进程里默认的线程我们叫做主线程或者叫做UI线 ...
- Java Hour3
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为2 Hour,请各位不吝赐教. Hour3 包和i ...
- ajax请求原理及jquery $.ajax封装全解析
.ajax原理: Ajax的原理简单来说通过XmlHttpRequest对象来向服务器发异步请求,从服务器获得数据,然后用javascript来操作DOM而更新页面.这其中最关键的一步就是从服务器获得 ...
- ServletContext与ServletConfig的详解及区别
转自http://hi.baidu.com/huaxuelili/item/1704a03dbb5cd7f22784f4c6 一.ServletContext详解ServletContext是serv ...
- Activity启动方式
Activity启动方式有四种,分别是: standard singleTop singleTask singleInstance 可以根据实际的需求为Activity设置对应的启动模式,从而可以避免 ...
- BeagleBone Black项目实训手册(大学霸内部资料)
BeagleBone Black项目实训手册(大学霸内部资料) 介绍:本教程是<BeagleBone Black快速入门教程>的后续教程.本教程以项目操作为主,讲解LED项目.声音项目.传 ...
- [LintCode] Permuation Index
Given a permutation which contains no repeated number, find its index in all the permutations of the ...
- oracle处理考勤时间,拆分考勤时间段的sql语句
最近一直在用mysql数据库做云项目,有段时间没有接触oracle了,昨天有朋友叫我帮忙用oracle处理一个考勤记录的需求,我在考虑如何尽量精简实现上面花了一些时间.于是把这个实现做个总结. 需求如 ...