dbcp下载  传送门

  Commons Pool下载  传送门

  Commons log下载  传送门

  MySQL_(Java)【事物操作】使用JDBC模拟银行转账向数据库发起修改请求  传送门

  MySQL_(Java)【连接池】简单在JDBCUtils.java中创建连接池  传送门

  DBCP(DataBase Connection Pool):数据库连接池,是Java数据库连接池的一种,通过数据库连接池,可以让程序自动管理数据库连接的释放和断开百度百科

  模拟银行由a向b转账1000元操作,使用事物+DBCP连接池

  

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class JDBC01 { public static void main(String[] args) throws SQLException {
transferAccount("a","b",1000);
} public static void selectAll() throws SQLException {
//注册驱动 使用驱动连接数据库
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
//数据库的连接
con = JDBCUtils.getConnection();
//数据库的增删改查
stmt = con.createStatement();
//返回一个结果集
rs =stmt.executeQuery("select * from garytb"); while(rs.next()) {
//System.out.println(rs.getString(1)+","+rs.getString(2)+","+rs.getString(3));
System.out.println(rs.getString("id")+","+rs.getString("username")+","+rs.getString("password"));
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.close(rs, stmt, con);
}
} //校验用户
public static boolean selectByUernamePassword(String username,String password) throws SQLException {
Connection con=null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver"); String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
con = DriverManager.getConnection(url,"root","123456");
stmt =con.createStatement();
String sql = "select * from garytb where username = '"+username+"' and password = '"+password+"'";
//System.out.println(sql);
rs = stmt.executeQuery(sql); if(rs.next()) {
return true;
}else {
return false;
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
} return false;
} public static boolean selectByUP2(String username,String password) throws SQLException{
Connection con=null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver"); String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
con = DriverManager.getConnection(url,"root","123456"); String sql = "select * from garytb where username = ? and password = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
//添加参数
pstmt.setString(1, username);
pstmt.setString(2, password);
//进行查询
rs = pstmt.executeQuery(); if(rs.next()) {
return true;
}else {
return false;
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
} return false;
} //pageNumber是页数,第几页,pageCount是每页显示多少个数据
public static void selectUserByPage(int pageNumber,int pageCount) throws SQLException {
//注册驱动 使用驱动连接数据库
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver"); //String url ="jdbc:mysql://localhost:3306/garysql";
//指定编码查询数据库
String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
String user = "root";
String password = "123456";
//建立和数据库的连接
con = DriverManager.getConnection(url,user,password); stmt = con.prepareStatement("select * from garytb limit ?,?");
stmt.setInt(1, (pageNumber-1)*pageCount );
stmt.setInt(2, pageCount); rs = stmt.executeQuery(); while(rs.next()) {
//System.out.println(rs.getString(1)+","+rs.getString(2)+","+rs.getString(3));
System.out.println(rs.getString("id")+","+rs.getString("username")+","+rs.getString("password"));
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
} //crud: create read update delete
//插入语句
public static void insert(String username,String password) throws SQLException {
//注册驱动 使用驱动连接数据库
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = JDBCUtils.getConnection();
String sql = "insert into garytb(username,password) values(?,?)";
stmt = con.prepareStatement(sql);
stmt.setString(1, username);
stmt.setString(2, password);
int result =stmt.executeUpdate();// 返回值代表收到影响的行数
System.out.println("插入成功"+username);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.close(rs, stmt, con);
}
}
//删除语句
public static void delete(int id) throws SQLException {
//注册驱动 使用驱动连接数据库
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = JDBCUtils.getConnection(); String sql = "delete from garytb where id = ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, id);
int result =stmt.executeUpdate();// 返回值代表收到影响的行数
if(result>0) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(rs, stmt, con);
}
}
//修改语句
public static void update(int id,String newPassword) throws SQLException {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = JDBCUtils.getConnection(); String sql = "update garytb set password = ? where id = ?";
stmt = con.prepareStatement(sql);
stmt.setString(1, newPassword);
stmt.setInt(2, id);
int result =stmt.executeUpdate();// 返回值代表收到影响的行数
if(result>0) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(rs, stmt, con);
}
} //事物操作
//由username1向username2转账金额
public static void transferAccount(String username1,String username2,int money) {
Connection con = null;
PreparedStatement stmt1 = null;
PreparedStatement stmt2 = null;
ResultSet rs = null;
try {
con = DBCPDataSource.getConnection(); //开启事物 是否自动提交
con.setAutoCommit(false); String sql = "update garytb set balance = balance - ? where username = ?";
stmt1 = con.prepareStatement(sql);
stmt1.setInt(1, money);
stmt1.setString(2, username1);
stmt1.executeUpdate();// 返回值代表收到影响的行数 //显示异常throw new Exception("出现错误");
//隐示异常 空指针异常
//String s = null;
//s.charAt(2); sql = "update garytb set balance = balance + ? where username = ?";
stmt2 = con.prepareStatement(sql);
stmt2.setInt(1, money);
stmt2.setString(2, username2);
stmt2.executeUpdate();// 返回值代表收到影响的行数
System.out.println("操作成功!!"); //提交事务
//当事物中所有事物都完成了才会提交
con.commit(); } catch (Exception e) {
e.printStackTrace();
} finally {
DBCPDataSource.close(stmt2, stmt1, con);
}
} }

JDBC01.java

import java.sql.Connection;
import java.sql.SQLException; import org.apache.commons.dbcp2.BasicDataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class DBCPDataSource { private static final String connectionURL = "jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
private static final String username = "root";
private static final String password = "123456"; private static BasicDataSource ds; //静态代码块:当整个程序执行的时候,优先加载静态代码块
static {
//初始化dbcp数据源
ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl(connectionURL);
ds.setUsername(username);
ds.setPassword(password); //初始化连接池5个
ds.setInitialSize(5);
//连接池最多个数20个
ds.setMaxTotal(20);
//最小的空闲连接
ds.setMinIdle(3);
} public static Connection getConnection() {
try {
//通过dbcp得到的连接,不需要归还,直接close就可以
return ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
} public static void close(ResultSet rs,Statement stmt,Connection con) {
closeResultSet(rs);
closeStatement(stmt);
closeConnection(con);
}
public static void close(Statement stmt1,Statement stmt2,Connection con) {
closeStatement(stmt1);
closeStatement(stmt2);
closeConnection(con);
} private static void closeResultSet(ResultSet rs ) {
try {
if(rs!=null)rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void closeStatement(Statement stmt) {
try {
if(stmt!=null)
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void closeConnection(Connection con) {
try {
if(con!=null)con.close();//这里会把链接归还给dbcp连接池,并不是真正的断开链接
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

DBCPDataSource.java

  a向b转账事物方法

//事物操作
//由username1向username2转账金额
public static void transferAccount(String username1,String username2,int money) {
Connection con = null;
PreparedStatement stmt1 = null;
PreparedStatement stmt2 = null;
ResultSet rs = null;
try {
con = DBCPDataSource.getConnection(); //开启事物 是否自动提交
con.setAutoCommit(false); String sql = "update garytb set balance = balance - ? where username = ?";
stmt1 = con.prepareStatement(sql);
stmt1.setInt(1, money);
stmt1.setString(2, username1);
stmt1.executeUpdate();// 返回值代表收到影响的行数 //显示异常throw new Exception("出现错误");
//隐示异常 空指针异常
//String s = null;
//s.charAt(2); sql = "update garytb set balance = balance + ? where username = ?";
stmt2 = con.prepareStatement(sql);
stmt2.setInt(1, money);
stmt2.setString(2, username2);
stmt2.executeUpdate();// 返回值代表收到影响的行数
System.out.println("操作成功!!"); //提交事务
//当事物中所有事物都完成了才会提交
con.commit(); } catch (Exception e) {
e.printStackTrace();
} finally {
DBCPDataSource.close(stmt2, stmt1, con);
}
}

MySQL_(Java)【连接池】使用DBCP简单模拟银行转账事物的更多相关文章

  1. 几个主流java连接池

    池(Pool)技术在一定程度上可以明显优化服务器应用程序的性能,提高程序执行效率和降低系统资源开销.这里所说的池是一种广义上的池,比如数据库连接池.线程池.内存池.对象池等.其中,对象池可以看成保存对 ...

  2. 转载: 几个主流的Java连接池整理

    https://www.cnblogs.com/linjian/p/4831088.html 池(Pool)技术在一定程度上可以明显优化服务器应用程序的性能,提高程序执行效率和降低系统资源开销.这里所 ...

  3. 几个主流的Java连接池整理

    池(Pool)技术在一定程度上可以明显优化服务器应用程序的性能,提高程序执行效率和降低系统资源开销.这里所说的池是一种广义上的池,比如数据库连接池.线程池.内存池.对象池等.其中,对象池可以看成保存对 ...

  4. 关于 Mybatis的原生连接池 和 DBCP 连接池

    一 遇到的问题:  项目用的play框架,数据库DB2, 持久化框架是Mybatis, 连接池用的是Mybatis原生的,遇到的问题是:有时候抛出如下异常: play.api.UnexpectedEx ...

  5. [JavaEE] 了解Java连接池

    转载自51CTO http://developer.51cto.com/art/201006/207768.htm 51CTO曾经为我们简单的介绍过Java连接池.要了解Java连接池我们先要了解数据 ...

  6. JDBC连接池(三)DBCP连接池

    JDBC连接池(三)DBCP连接池 在前面的随笔中提到 了  1.JDBC自定义连接池  2. C3P0连接池 今天将介绍DBCP连接池 第一步要导入jar包   (注意:mysql和mysql 驱动 ...

  7. Java 连接池的工作原理(转)

    原文:Java 连接池的工作原理 什么是连接? 连接,是我们的编程语言与数据库交互的一种方式.我们经常会听到这么一句话“数据库连接很昂贵“. 有人接受这种说法,却不知道它的真正含义.因此,下面我将解释 ...

  8. KA,连接池居然这么简单? 原创: 58沈剑 架构师之路 3月20日

    KA,连接池居然这么简单? 原创: 58沈剑 架构师之路 3月20日

  9. Redis Java连接池调研

    Redis Java连接池调研 线上服务,由于压力大报错RedisTimeOut,但是需要定位到底问题出现在哪里? 查看Redis慢日志,slowlog get 发现耗时最大的也是11000us也就是 ...

随机推荐

  1. 二叉查找树 平衡二叉查找树 红黑树 b树 b+树 链表 跳表 链表

    https://www.cnblogs.com/mojxtang/p/10122587.html二叉树的新增遍历查找

  2. [转载]Flex的文件规则

    原文在:https://blog.csdn.net/hczhiyue/article/details/20483209 文章中给的一个定义很明白,对于初学者来说很有帮助: 什么是 FLEX?它是一个自 ...

  3. java后台读取配置文件

    前几天开发时遇到一个问题,在后台读取配置文件的时候无法读取属性值,于是上网查了查,现在在这分享给大家: 先附上代码吧: package com.shafei.util; import java.io. ...

  4. 【ExtJs】在Ext.grid.Panel中,两列的值相乘作为第三列的值的实现

    如: 商品总价=商品单价*商品数量 方法: 商品总价列,使用其renderer属性,为期定义一个方法,该方法将当前record中的另外两列中2个数据相乘后渲染到该商品总价列.

  5. HTML的学习(注释)

    <!--charset 编码字符集--> <!--UTF-8 万国码 gb2312 中国标准第2312条 中文,韩文....大部分的亚裔语言(繁体字不支持) GBK 在上面的基础之上 ...

  6. ProgressDialog 进度条的初步认识

    public class MainActivity extends Activity implements View.OnClickListener{ private ProgressBar prog ...

  7. 将 spring boot 安装为 systemd 服务

    [root@ecs-11-132 system]# cat /etc/systemd/system/push-gateway-3.0.0.service [Unit] Description=app- ...

  8. js去掉url后某参数【函数封装】

    function delParam(paramKey) { var url = window.location.href; //页面url var urlParam = window.location ...

  9. Java语言基础(7)

    1 for循环 案例:Demo1 1+1/2+1/3+1/4+1/5+1/6+...+1/100 = ? 1/1+1/2+1/3+1/4+1/5+1/6+...+1/100 = ? 分子都是1,分母是 ...

  10. hbase单机搭建

    一.下载 https://hbase.apache.org/downloads.html 2.1.3版本 解压,拷贝到文件夹 /hbase/hbase-2.1.3 设置HBASE_HOME环境变量,把 ...