反射技术将查询结果封装为对象_编写一个基本的连接池来实现连接的复用

 

反射技术将查询结果封装为对象_编写一个基本的连接池来实现连接的复用

 

反射技术将查询结果封装为对象-------------------------------------------------------------------

通过的ORMTest类的getUser方法,

传入sql语句,及需要封装对象的class.

可复用

 ORMTest.java

package com.dwt1220;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

public class ORMTest {
	public static void main(String[] args) throws Exception {
		User user=getUser("select id as Id, name as Name from user;",User.class);
		System.out.println(user);
	}
 
	static <T>T getUser(String sql,Class<T> clazz) throws Exception{
		Connection conn = null;
		PreparedStatement ps = null;//使用PreparedStatement防止SQL注入
		ResultSet rs = null;
		T obj=null;
		try {
			conn = JdbcUtils.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			ResultSetMetaData rsmd=rs.getMetaData();
			
			int count=rsmd.getColumnCount();
			String[] colNames=new String[count];
			
			for(int i=1;i<=count;i++){
				colNames[i-1]=rsmd.getColumnLabel(i);
			}
			
		
			Method[] ms=clazz.getMethods();
			if(rs.next()){
				obj=clazz.newInstance();
				for(int i=0;i<colNames.length;i++){
					String colName=colNames[i];
					String methodName="set"+colName;
					System.out.println(methodName);
					for(Method m:ms){
						if(methodName.equals(m.getName())){
							m.invoke(obj, rs.getObject(colName));
							System.out.println(rs.getObject(colName));
						}
					}
				}
			}
			
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
		return (T) obj;
	}
}


编写一个基本的连接池来实现连接的复用------------------------------------------------------------------------

pool类:MyDataSource.java

工具类:JdbcUtils.java

测试类:Base.java

 

 

MyDataSource.java

package com.dwt1220;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;

public class MyDataSource {
	private static String url = "jdbc:mysql://localhost:3306/jdbc";
	private static String user = "root";
	private static String password = "123456";
	
	private static int initCount=5;
	private static int maxCount=10;
	private  int currentCount=0;
	
	private LinkedList<Connection> connectionsPool = new LinkedList<Connection>();

	public Connection createConnection() throws SQLException {
		return DriverManager.getConnection(url, user, password);
	}

	public MyDataSource() {
		try {
			for (int i = 0; i < initCount; i++) {
				this.connectionsPool.addLast(this.createConnection());
				this.currentCount++;
			}
		} catch (SQLException e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	
	public Connection getConnection() throws SQLException{
		synchronized (connectionsPool) {
			if(this.connectionsPool.size()>0)
				return this.connectionsPool.remove();
			
			if(this.currentCount<maxCount){
				this.currentCount++;
				return this.createConnection();
			}
			throw new SQLException("已经没有连接");
		}
	
	}

	public void free(Connection conn){
		this.connectionsPool.addLast(conn);
	}
}



 

JdbcUtils.java

package com.dwt1220;

import java.sql.DriverManager;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.sql.Connection;

public final class JdbcUtils {
	public static MyDataSource myDataSource = null;

	private JdbcUtils() {
	}

	static {// 静态代码快,只执行一次。
			// 注册驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
			myDataSource = new MyDataSource();
		} catch (ClassNotFoundException e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	public static Connection getConnection() throws SQLException {
		return myDataSource.getConnection();
	}

	public static void free(ResultSet rs, Statement st, Connection conn) {
		try {
			if (rs != null)
				rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (st != null)
					st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (conn != null)
					myDataSource.free(conn);
			}
		}
	}
}


Base.java

package com.dwt1220;

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 Base {

	public static void main(String[] args) throws Exception {
		for(int i=0;i<10;i++){
			Connection conn=JdbcUtils.getConnection();
			System.out.println(conn);
			JdbcUtils.free(null, null, conn);
		}
	}

}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dwt1220

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值