码迷,mamicode.com
首页 > 数据库 > 详细

我自己的JdbcTemplate

时间:2018-01-17 20:00:36      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:turn   commit   hashmap   array   void   sass   put   template   fas   

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.sql.DataSource;

import com.alibaba.fastjson.JSONObject;

public class JdbcTemplate {

	private DataSource ds;

	public DataSource getDataSource() {
		return ds;
	}

	public void setDataSource(DataSource ds) {
		this.ds = ds;
	}
	JdbcTemplate(){}
	public JdbcTemplate(DataSource ds) {
		setDataSource(ds);
	}

	public List<Map<String, Object>> queryForList(String sql, Object... args) {
		Connection con = null;
		try {
			con = ds.getConnection();
			con.setAutoCommit(false);
			PreparedStatement ps = con.prepareStatement(sql);
			for (int i = 0; i < args.length; i++) {
				ps.setObject(i + 1, args[i]);
			}
			ResultSet rs = ps.executeQuery();
			return convertResult(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return null;

	}

	public Map<String, Object> queryForMap(String sql, Object... param) {
		List<Map<String, Object>> lst = queryForList(sql, param);
		return lst != null && lst.size() > 0 ? lst.get(0) : new HashMap<String, Object>();
	}

	public <T> T queryForObject(String sql, Class<T> clazz, Object... param) {
		T obj = null;
		if (clazz == null) {
			return obj;
		} else {
			Map<String, Object> map = queryForMap(sql, param);
			if (map != null && map.size() > 0) {
				try {
					if (clazz.getName().startsWith("java.util") || clazz.getName().startsWith("java.lang")) {
						for (Entry<String, Object> en : map.entrySet()) {
							if (en.getValue() == null) {
								continue;
							}

							Class<?> c = en.getValue().getClass();
							if (c == clazz || c.isAssignableFrom(clazz) || clazz.isAssignableFrom(c)) {
								String s = en.getValue().toString();
								obj = JSONObject.parseObject(s, clazz);
							}
						}
					} else {
						obj = JSONObject.parseObject(JSONObject.toJSONString(map), clazz);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return obj;
	}

	public int queryForUpdate(String sql, Object... args) {
		int count = 0;
		Connection con = null;
		try {
			con = ds.getConnection();
			con.setAutoCommit(false);
			PreparedStatement ps = con.prepareStatement(sql);
			if (args != null) {
				for (int i = 0; i < args.length; i++) {
					ps.setObject(i + 1, args[i]);
				}
			}
			count = ps.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return count;
	}

	public int queryForBatchUpdate(String sql, String[]... argsArray) {
		int count = 0;
		Connection con = null;
		try {
			con = ds.getConnection();
			con.setAutoCommit(false);
			PreparedStatement ps = ds.getConnection().prepareStatement(sql);
			if (argsArray != null) {
				for (int i = 0; i < argsArray.length; i++) {
					String[] args = argsArray[i];
					for (int j = 0; j < args.length; j++) {
						ps.setObject(i + 1, args[i]);
						ps.addBatch();
					}
				}
			}

			int[] arr = ps.executeBatch();
			for (int i : arr) {
				count += i;
			}
			con.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return count;
	}

	public List<Map<String, Object>> convertResult(ResultSet rs) {
		List<Map<String, Object>> lst = new LinkedList<>();
		if (rs == null)
			return lst;

		try {
			// 得到结果集(rs)的结构信息,比如字段数、字段名等
			ResultSetMetaData md = rs.getMetaData();

			// 返回此 ResultSet 对象中的列数
			int columnCount = md.getColumnCount();

			Map<String, Object> rowData = new LinkedHashMap<>();
			while (rs.next()) {
				rowData = new LinkedHashMap<>(columnCount);
				for (int i = 1; i <= columnCount; i++) {
					rowData.put(md.getColumnName(i), rs.getObject(i));
				}
				lst.add(rowData);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lst;
	}
}

  

我自己的JdbcTemplate

标签:turn   commit   hashmap   array   void   sass   put   template   fas   

原文地址:https://www.cnblogs.com/swtjavaspace/p/8304171.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!