<?xml version="1.0" encoding="UTF-8"?>
<!-- 需要导入c3p0驱动jar包和mysql驱动jar包 -->
<!-- c3p0-config.xml配置文件放在src包下 -->
<c3p0-config>
<named-config name="mysqlConnection">
<property name="user">root</property>
<property name="password"></property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="driverClass">com.mysql.jdbc.Driver</property> <property name="initialPoolSize">20</property>
<property name="maxPoolSize">25</property>
<property name="minPoolSize">5</property>
</named-config>
</c3p0-config>
package cn.zr.testfilter.pojo;

public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} public User() {
super();
}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
} }
package cn.zr.testfilter.Utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import javax.sql.DataSource; import cn.zr.testfilter.pojo.User; import com.mchange.v2.c3p0.ComboPooledDataSource; public class ConnectionUtils { public static void main(String[] args) {
ConnectionUtils utils = new ConnectionUtils();
List<User> list = utils.getData();
System.out.println(list);
} public List<User> getData(){
List<User> list = new ArrayList<User>();
Connection connection = ConnectionUtils.getConnection();
String sql = "SELECT NAME,AGE FROM USERTEST";
PreparedStatement pStatement = null;
ResultSet rSet = null;
try {
pStatement = (PreparedStatement) connection.prepareStatement(sql);
rSet = pStatement.executeQuery();
while (rSet.next()) {
User user = new User(rSet.getString("name"), rSet.getInt("age"));
list.add(user);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if (rSet != null) {
rSet.close();
}
if (pStatement != null) {
pStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
} public static Connection getConnection() { DataSource ds = new ComboPooledDataSource("mysqlConnection");
try {
Connection connection = ds.getConnection();
return connection;
} catch (SQLException e) {
e.printStackTrace();
} return null; /*String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/mysql";
String user = "root";
String password = "";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;*/ } }

Java c3p0 连接 MySQL的更多相关文章

  1. java jdbc 连接mysql数据库 实现增删改查

    好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...

  2. Java中连接MySql数据库的例子

    Java中连接MySql数据库的例子: package com.joinmysql.demo; import java.sql.DriverManager; import java.sql.Resul ...

  3. java jdbc 连接mysql 数据库

    JDBC连接MySQL 加载及注册JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); Class.forName("com. ...

  4. java怎么连接mysql数据库

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...

  5. java 项目连接MySQL数据库

    1.导入jar包 mysql-connector-java-5.1.35百度云链接如下: 链接:https://pan.baidu.com/s/1DPvIwU_An4MA3mU5bQa6VA 密码:5 ...

  6. java使用c3p0连接mysql,写中文数据乱码的问题

    此文说的乱码,是指所有中文的字符都变成了?. 首先,网上普遍搜索到的解决方案都是告诉你要在数据库连接字符串里面增加编码的定义,完整的连接字符串如下: url="jdbc:mysql://12 ...

  7. Java c3p0连接池

    import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.SQLException; i ...

  8. java jdbc连接mysql

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...

  9. java实现连接mysql数据库单元测试查询数据项目分享

    1.按照javaweb项目的要求逐步建立搭建起机构,具体的类包有:model .db.dao.test;具体的架构详见下图: 2.根据搭建的项目架构新建数据库test和数据库表t_userinfo并且 ...

随机推荐

  1. struts2逻辑视图类型汇总与解释(转)

    在struts2框架中,当action处理完之后,就应该向用户返回结果信息,该任务被分为两部分:结果类型和结果本身. 结果类型提供了返回给用户信息类型的实现细节.结果类型通常在Struts2中就已预定 ...

  2. BaseCommand

    import java.io.Serializable; import android.util.Log; public class BaseCommand implements Serializab ...

  3. Mybatis与Hibernate的详细对比

    前言 这篇博文我们重点分析一下Mybatis与hibernate的区别,当然在前面的博文中我们已经深入的研究了Mybatis和Hibernate的原理. Mybatis [持久化框架]Mybatis简 ...

  4. LeetCode Valid Parenthesis String

    原题链接在这里:https://leetcode.com/problems/valid-parenthesis-string/description/ 题目: Given a string conta ...

  5. c++ 遍历任务栏窗口

    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h& ...

  6. BZOJ3507 [Cqoi2014]通配符匹配

    题意 几乎所有操作系统的命令行界面(CLI)中都支持文件名的通配符匹配以方便用户.最常见的通配符有两个,一个是星号("*"),可以匹配0个及以上的任意字符:另一个是问号(" ...

  7. Servlet和JSP规范与Tomcat版本对应关系

    apache tomcat 官网地址:http://tomcat.apache.org/whichversion.html

  8. 三种实现Ajax的方式

    本文主要是比较三种实现Ajax的方式 1. prototype.js 2. jquery1.3.2.min.js 3. json2.js Java代码 收藏代码 后台处理程序(Servlet),访问路 ...

  9. onItemLongClick事件的监听

    首先需要implements public class MainActivity extends AppCompatActivity implements OnItemLongClickListene ...

  10. mysql事务之一:MySQL数据库事务隔离级别(Transaction Isolation Level)及锁的实现原理

    一.数据库隔离级别 数据库隔离级别有四种,应用<高性能mysql>一书中的说明: 然后说说修改事务隔离级别的方法: 1.全局修改,修改mysql.ini配置文件,在最后加上 1 #可选参数 ...