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 ...
随机推荐
- [Java基础] System.arraycopy使用
转载自:http://blog.csdn.net/java2000_net/article/details/4059465 System提供了一个native 静态方法arraycopy(),我们可以 ...
- Fresco 源码分析(三) Fresco服务端处理(1) ImagePipeline为何物
4.3 服务端的处理 备注: 因为是分析,而不是设计,所以很多知识我们类似于插叙的方式叙述,就是用到了哪个知识点,我们再提及相关的知识点,如果分析到了最后,我想想是不是应该将这个架构按照设计的方式,重 ...
- kvm虚似机监控
[root@ok Desktop]# yum install virt-top [root@ok Desktop]# virt-top virt-top 22:53:44 - x86_64 4/4CP ...
- CI当开启URL重写的时候,报错500 Internal Server Error
Internal Server Error The server encountered an internal error or misconfiguration and was unable to ...
- CodeChef DISTNUM2 Easy Queries 节点数组线段树
Description You are given an array A consisting of N positive integers. You have to answer Q queries ...
- 普通SQL语句可以用Exec执行
例如存储过名为:myprocedure use AdventureWorks create procedure myprocedure @city varchar(20) as begin selec ...
- Tomcat中的webapps中的web应用的文件结构
可仿造Tomcat中的webapps下的已有web应用的例子 具体文件结构如下:
- 【HTML5】Server-Sent服务器发送事件
Server-Sent 事件 - 单向消息传递 Server-Sent 事件指的是网页自动获取来自服务器的更新. 以前也可能做到这一点,前提是网页不得不询问是否有可用的更新.通过服务器发送事件,更新能 ...
- Android loading进度条使用简单总结
在这里,总结一下loading进度条的使用简单总结一下. 一.说起进度条,必须说说条形进度条,经常都会使用到嘛,特别是下载文件进度等等,还有像腾讯QQ安装进度条一样,有个进度总给人良好的用户体验. 先 ...
- CodeForces 304C
E - E Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Statu ...