时间:2021-07-01 10:21:17 帮助过:20人阅读
create table account( id int primary key auto_increment, name varchar(20), money double ); insert into account values(null,‘aaa‘,1000); insert into account values(null,‘bbb‘,1000); insert into account values(null,‘ccc‘,1000);
driverClass=com.mysql.jdbc.Driver url=jdbc:mysql:///mydb1 username=root password=root #driverClass=oracle.jdbc.driver.OracleDriver #url=jdbc:oracle:thin:@localhost:1521:XE #username=system #password=system
package cn.itcast.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ResourceBundle; //使用配置文件 public class JdbcUtils { private static final String DRIVERCLASS; private static final String URL; private static final String USERNAME; private static final String PASSWORD; static { DRIVERCLASS = ResourceBundle.getBundle("jdbc").getString("driverClass"); URL = ResourceBundle.getBundle("jdbc").getString("url"); USERNAME = ResourceBundle.getBundle("jdbc").getString("username"); PASSWORD = ResourceBundle.getBundle("jdbc").getString("password"); } static { try { // 将加载驱动操作,放置在静态代码块中.这样就保证了只加载一次. Class.forName(DRIVERCLASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { // 2.获取连接 Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD); return con; } //关闭操作 public static void closeConnection(Connection con) throws SQLException{ if(con!=null){ con.close(); } } public static void closeStatement(Statement st) throws SQLException{ if(st!=null){ st.close(); } } public static void closeResultSet(ResultSet rs) throws SQLException{ if(rs!=null){ rs.close(); } } }新建简单测试类TransactionTest1.java
package cn.itcast.transaction; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import cn.itcast.utils.JdbcUtils; //jdbc中事务操作 public class TransactionTest1 { public static void main(String[] args) throws SQLException { // 修改id=2这个人的money=500; String sql = "update account set money=500 where id=2"; Connection con = JdbcUtils.getConnection(); con.setAutoCommit(false); //开启事务,相当于 start transaction; Statement st = con.createStatement(); st.executeUpdate(sql); //事务回滚 //con.rollback(); con.commit(); //事务提交 st.close(); con.close(); } }
package cn.itcast.transaction; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import cn.itcast.utils.JdbcUtils; //jdbc中事务操作 public class TransactionTest2 { public static void main(String[] args) { // 修改id=2这个人的money=500; String sql = "update account set money=500 where id=1"; Connection con = null; Statement st = null; try { con = JdbcUtils.getConnection(); con.setAutoCommit(false); // 开启事务,相当于 start transaction; st = con.createStatement(); st.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); // 事务回滚 try { con.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } finally { try { con.commit(); // 事务提交 st.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
package cn.itcast.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import cn.itcast.exception.AccountException; //没有使用ThreadLocal来获取Connection。 public class _AccountDaoImpl { // 从accountOut账户转出money public void accountOut(Connection con, String accountOut, double money) throws SQLException, AccountException { String sql = "update account set money=money-? where name=?"; PreparedStatement pst = con.prepareStatement(sql); pst.setDouble(1, money); pst.setString(2, accountOut); int row = pst.executeUpdate(); if (row == 0) { throw new AccountException("转出失败"); } pst.close(); } // 向accountIn账户转入money public void accountIn(Connection con, String accountIn, double money) throws SQLException, AccountException { String sql = "update account set money=money+? where name=?"; PreparedStatement pst = con.prepareStatement(sql); pst.setDouble(1, money); pst.setString(2, accountIn); int row = pst.executeUpdate(); if (row == 0) { throw new AccountException("转入失败"); } pst.close(); } }真实的实现类实现自别人设计的DAO接口,一般不会考虑到Connection AccountDao接口如下:
package cn.itcast.dao; import java.sql.Connection; public interface AccountDao { public void accountOut(String accountOut, double money) throws Exception; public void accountIn(String accountIn, double money) throws Exception; }真实DAO实现类实现了AccountDao接口
package cn.itcast.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import cn.itcast.exception.AccountException; import cn.itcast.utils.JdbcUtils; public class AccountDaoImpl implements AccountDao { // 从accountOut账户转出money public void accountOut(String accountOut, double money) throws SQLException, AccountException { String sql = "update account set money=money-? where name=?"; Connection con=JdbcUtils.getConnection(); PreparedStatement pst = con.prepareStatement(sql); pst.setDouble(1, money); pst.setString(2, accountOut); int row = pst.executeUpdate(); if (row == 0) { throw new AccountException("转出失败"); } pst.close(); } // 向accountIn账户转入money public void accountIn( String accountIn, double money) throws SQLException, AccountException { String sql = "update account set money=money+? where name=?"; Connection con=JdbcUtils.getConnection(); PreparedStatement pst = con.prepareStatement(sql); pst.setDouble(1, money); pst.setString(2, accountIn); int row = pst.executeUpdate(); if (row == 0) { throw new AccountException("转入失败"); } pst.close(); } }AccountException.java (自定义异常)
package cn.itcast.exception; public class AccountException extends Exception { public AccountException() { super(); // TODO Auto-generated constructor stub } public AccountException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public AccountException(String message) { super(message); // TODO Auto-generated constructor stub } public AccountException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } }JdbcUtils.java (封装Connection连接对象)
package cn.itcast.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ResourceBundle; public class JdbcUtils { private static final String DRIVERCLASS; private static final String URL; private static final String USERNAME; private static final String PASSWORD; private static final ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); static { DRIVERCLASS = ResourceBundle.getBundle("jdbc").getString("driverClass"); URL = ResourceBundle.getBundle("jdbc").getString("url"); USERNAME = ResourceBundle.getBundle("jdbc").getString("username"); PASSWORD = ResourceBundle.getBundle("jdbc").getString("password"); } static { try { // 将加载驱动操作,放置在静态代码块中.这样就保证了只加载一次. Class.forName(DRIVERCLASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { Connection con = tl.get();// 从ThreadLocal中获取Connection。第一次获取得到的是null. if (con == null) { // 2.获取连接 con = DriverManager.getConnection(URL, USERNAME, PASSWORD); tl.set(con); // 将con装入到ThreadLocal中。 } // tl.remove(); //解除 return con; } // 关闭操作 public static void closeConnection(Connection con) throws SQLException { if (con != null) { con.close(); } } public static void closeStatement(Statement st) throws SQLException { if (st != null) { st.close(); } } public static void closeResultSet(ResultSet rs) throws SQLException { if (rs != null) { rs.close(); } } }AccountServlet.java
package cn.itcast.web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.exception.AccountException; import cn.itcast.service.AccountService; public class AccountServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); // 1.得到请求参数 String accountIn = request.getParameter("accountin"); String accountOut = request.getParameter("accountout"); double money = Double.parseDouble(request.getParameter("money")); // 2.调用service,完成汇款操作 AccountService service = new AccountService(); try { service.account(accountIn, accountOut, money); response.getWriter().write("转账成功"); return; } catch (AccountException e) { e.printStackTrace(); response.getWriter().write(e.getMessage()); return; } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘index.jsp‘ starting page</title> </head> <body> <form action="${pageContext.request.contextPath}/account" method="post"> 转入账户:<input type="text" name="accountin"><br> 转出账户:<input type="text" name="accountout"><br> 金额:<input type="text" name="money"><br> <input type="submit" value="提交"> </form> </body> </html>看运行结果:
package cn.itcast.zq; //使用继承增强 public class Demo1 { public static void main(String[] args) { Person1 p=new Student1(); p.eat(); } } class Person1{ public void eat(){ System.out.println("吃两个馒头"); } } class Student1 extends Person1{ @Override public void eat(){ super.eat(); System.out.println("加两个鸡腿"); } }
package cn.itcast.zq; //使用装饰进行增强 public class Demo2 { public static void main(String[] args) { Car car=new Bmw(); //给车增强 CarDerector cd=new CarDerector(car); cd.run(); } } interface Car { void run(); } class Bmw implements Car { public void run() { System.out.println("bmw run...."); } } class Benz implements Car { public void run() { System.out.println("benz run...."); } } // 使用装饰来完成 class CarDerector implements Car { private Car car; public CarDerector(Car car) { this.car = car; } public void run() { System.out.println("添加导航"); car.run(); } }
package cn.itcast.zq; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import cn.itcast.utils.JdbcUtils; public class Demo3 { public static void main(String[] args) throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); //注册驱动 final Connection con = DriverManager.getConnection("jdbc:mysql:///mydb1","root","root"); Connection proxy = (Connection) Proxy.newProxyInstance(con.getClass() .getClassLoader(), con.getClass().getInterfaces(), new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(con, args); } }); System.out.println(proxy); } }运行发现出现了如下错误:
package cn.itcast.zq; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import cn.itcast.utils.JdbcUtils; public class Demo3 { public static void main(String[] args) throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); //注册驱动 final Connection con = DriverManager.getConnection("jdbc:mysql:///mydb1","root","root"); Connection proxy = (Connection) Proxy.newProxyInstance(con.getClass() .getClassLoader(), new Class[]{Connection.class}, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(con, args); } }); System.out.println(proxy); } }
package cn.itcast.datasource; import java.io.PrintWriter; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.util.LinkedList; import java.util.logging.Logger; import javax.sql.DataSource; import cn.itcast.utils.JdbcUtils; public class MyDataSource implements DataSource { private LinkedList<Connection> ll; // 用于装Connection对象的容器。 public MyDataSource() throws SQLException { ll = new LinkedList<Connection>(); // 当创建MyDateSource对象时,会向ll中装入5个Connection对象。 for (int i = 0; i < 5; i++) { Connection con = JdbcUtils.getConnection(); ll.add(con); } } // 获取连接方法 public Connection getConnection() throws SQLException { if (ll.isEmpty()) { for (int i = 0; i < 3; i++) { Connection con = JdbcUtils.getConnection(); ll.add(con); } } final Connection con = ll.removeFirst(); Connection proxyCon = (Connection) Proxy.newProxyInstance(con .getClass().getClassLoader(), new Class[]{ Connection.class}, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if ("close".equals(method.getName())) { // 这代表是close方法,它要做的事情是将con对象重新装入到集合中. ll.add(con); System.out.println("重新将连接对象装入到集合中"); return null; } else { return method.invoke(con, args);// 其它方法执行原来操作 } } }); return proxyCon; } // 将Connection对象重新装入. // public void readd(Connection con) { // // ll.addLast(con); // // } // public int getSize() { // return ll.size(); // } public Connection getConnection(String username, String password) throws SQLException { return null; } public PrintWriter getLogWriter() throws SQLException { return null; } public void setLogWriter(PrintWriter out) throws SQLException { } public void setLoginTimeout(int seconds) throws SQLException { } public int getLoginTimeout() throws SQLException { return 0; } public <T> T unwrap(Class<T> iface) throws SQLException { return null; } public boolean isWrapperFor(Class<?> iface) throws SQLException { return false; } public Logger getParentLogger() throws SQLFeatureNotSupportedException { // TODO Auto-generated method stub return null; } }新建测试类,测试刚才新建的数据库连接池
package cn.itcast.datasource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; //测试自己定义的连接池 public class MyDataSourceTest { public static void main(String[] args) throws SQLException { DataSource mds = new MyDataSource(); // 创建一个连接池 // 获取一个连接对象 Connection con = mds.getConnection(); // 操作 ResultSet rs = con.createStatement().executeQuery( "select * from account"); while (rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name")); } rs.close(); // 将连接对象重新装入到连接池 // mds.readd(con); con.close(); // 原来作用是将Connection对象销毁,我们在使用连接池,获取Connection对象后,在调用close方法,就不在是销毁,而是将其重新放回到连接池。 // System.out.println(mds.getSize()); } }
package cn.itcast.datasource; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; import org.junit.Test; public class DbcpTest { // 1.手动配置 @Test public void test1() throws SQLException { BasicDataSource bds = new BasicDataSource(); // 需要设置连接数据库最基本四个条件 bds.setDriverClassName("com.mysql.jdbc.Driver"); bds.setUrl("jdbc:mysql:///mydb1"); bds.setUsername("root"); bds.setPassword("root"); // 得到一个Connection Connection con = bds.getConnection(); ResultSet rs = con.createStatement().executeQuery( "select * from account"); while (rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name")); } rs.close(); con.close(); // 将Connection对象重新装入到连接池. } // 2.自动配置 @Test public void test2() throws Exception { Properties props = new Properties(); // props.setProperty("driverClassName", "com.mysql.jdbc.Driver"); // props.setProperty("url", "jdbc:mysql:///day18"); // props.setProperty("username", "root"); // props.setProperty("password", "abc"); InputStream fis = this.getClass().getResourceAsStream("/dbcp.properties"); props.load(fis); DataSource ds = BasicDataSourceFactory.createDataSource(props); // 得到一个Connection Connection con = ds.getConnection(); ResultSet rs = con.createStatement().executeQuery( "select * from account"); while (rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name")); } rs.close(); con.close(); // 将Connection对象重新装入到连接池. } }
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///mydb1</property> <property name="user">root</property> <property name="password">root</property> </default-config> </c3p0-config>测试类如下:
package cn.itcast.datasource; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; import org.junit.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; //c3p0连接池 public class C3p0Test { // @Test public void test1() throws PropertyVetoException, SQLException { ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass("com.mysql.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql:///mydb1"); cpds.setUser("root"); cpds.setPassword("root"); // 得到一个Connection Connection con = cpds.getConnection(); ResultSet rs = con.createStatement().executeQuery( "select * from account"); while (rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name")); } rs.close(); con.close(); // 将Connection对象重新装入到连接池. } @Test public void test2() throws PropertyVetoException, SQLException { ComboPooledDataSource cpds = new ComboPooledDataSource(); // 得到一个Connection Connection con = cpds.getConnection(); ResultSet rs = con.createStatement().executeQuery( "select * from account"); while (rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name")); } rs.close(); con.close(); // 将Connection对象重新装入到连接池. // String path = this.getClass().getResource("/").getPath(); // System.out.println(path); } }
<c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement">3</property> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts">30</property> <!--两次连接中间隔时间,单位毫秒。Default: 1000 --> <property name="acquireRetryDelay">1000</property> <!--连接关闭时默认将所有未提交的操作回滚。Default: false --> <property name="autoCommitOnClose">false</property> <!--c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么 属性preferredTestQuery将被忽略。你不能在这张Test表上进