数据库:动态代理

来源:互联网 发布:淘宝xbox one手柄 编辑:程序博客网 时间:2024/06/08 03:11

第一步:prop.properties配置文件

className = com.mysql.jdbc.Driverurl = jdbc:mysql://localhost:3306/testuser = rootpassword =********

第二步:写工具类,中使用动态代理

package com.liuzhen.proxy;import java.io.InputStream;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.DriverManager;import java.sql.SQLException;import java.util.LinkedList;import java.util.Properties;import javax.sql.DataSource;import com.liuzhen.util.JdbcUtil;public class MyDataSource implements DataSource {private static String className;private static String url;private static String user;private static String password;private static LinkedList<Connection> pool = new LinkedList<Connection>();static {try {InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("prop.properties");Properties prop = new Properties();prop.load(in);className = prop.getProperty("className");user = prop.getProperty("user");password = prop.getProperty("password");url = prop.getProperty("url");Class.forName(className);// 初始化10个连接到池中for (int i = 0; i < 10; i++) {Connection conn = DriverManager.getConnection(url, user,password);pool.add(conn);}} catch (Exception e) {e.printStackTrace();}}@Overridepublic synchronized Connection getConnection() throws SQLException {if (pool.size() > 0) {final Connection conn = pool.remove();//使用动态代理return (Connection)Proxy.newProxyInstance(conn.getClass().getClassLoader(), conn.getClass().getInterfaces(), new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {if("close".equals(method.getName())){return pool.add(conn);}else{return method.invoke(conn, args);}}});} else {throw new RuntimeException("服务器忙!");}}@Overridepublic Connection getConnection(String username, String password)throws SQLException {return null;}@Overridepublic PrintWriter getLogWriter() throws SQLException {return null;}@Overridepublic int getLoginTimeout() throws SQLException {// TODO Auto-generated method stubreturn 0;}@Overridepublic void setLogWriter(PrintWriter out) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void setLoginTimeout(int seconds) throws SQLException {// TODO Auto-generated method stub}@Overridepublic boolean isWrapperFor(Class<?> iface) throws SQLException {// TODO Auto-generated method stubreturn false;}@Overridepublic <T> T unwrap(Class<T> iface) throws SQLException {// TODO Auto-generated method stubreturn null;}}
第三步:使用操作工具类

package com.liuzhen.proxy;import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;import javax.sql.DataSource;public class MyClient {   private static DataSource ds = new MyDataSource();public static void main(String[] args) {Connection conn =null;Statement stmt = null;try{conn = ds.getConnection();stmt = conn.createStatement();}catch(Exception e){e.printStackTrace();}finally{if(stmt!=null){try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if(conn!=null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}}




0 0
原创粉丝点击