一个简单的JDBC的实现

/**
 * @Description: 连接数据库的工具类,被定义成不可继承且是私有访问
 * @author chenlushun
 * @date 2016年4月1日 上午11:34:41
 */
public final class DBUtils {
	private static String url;
	private static String user;
	private static String psw;
	private static Connection connection;
	private PreparedStatement pstmt;// 有效的使SQL和参数分开,防止SQL注入,使程序更加健壮
	private ResultSet rs;


	public static void main(String[] args) {
		connection = DBUtils.getConnection();
		System.out.println(connection);
	}


	static {
		Properties prop = new Properties();
		try {
			prop.load(DBUtils.class.getResourceAsStream("/jdbc.properties"));
			user = prop.getProperty("jdbc.username");
			psw = prop.getProperty("jdbc.passwd");
			url = prop.getProperty("jdbc.url");
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("读取数据库文件异常", e);
		}
	}


	/* 防止通过构造器实例化 */
	private DBUtils() {
	}


	/**
	 * 获取数据库的连接
	 * 
	 * @return conn
	 */
	public static Connection getConnection() {
		if (null == connection) {
			try {
				connection = DriverManager.getConnection(url, user, psw);
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		return connection;
	}


	/**
	 * 执行修改添加删除操作
	 * 
	 * @param sql
	 * @param params
	 * @return
	 * @throws SQLException
	 */
	public boolean updateByPreparedStatment(String sql, List<?> params) throws SQLException {
		boolean flag = false;
		int result = -1;// 添加删除修改时所影响的数据库行数
		pstmt = connection.prepareStatement(sql);
		int index = 1;
		// 填充sql的占位符
		if (params != null && !params.isEmpty()) {
			for (int i = 0; i < params.size(); i++) {
				pstmt.setObject(index++, params.get(i));
			}
		}
		result = pstmt.executeUpdate();
		flag = result > 0 ? true : false;
		return flag;
	}


	/**
	 * 
	 * @param sql
	 * @param params
	 */
	public List<Map<String, Object>> findResult(String sql, List<?> params) throws SQLException {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		int index = 1;
		pstmt = connection.prepareStatement(sql);
		if (params != null && !params.isEmpty()) {
			for (int i = 0; i < params.size(); i++) {
				pstmt.setObject(index++, params.get(i));
			}
		}
		rs = pstmt.executeQuery();
		ResultSetMetaData metaData = rs.getMetaData();
		int cols_len = metaData.getColumnCount();
		while (rs.next()) {
			Map<String, Object> map = new HashMap<String, Object>();
			for (int i = 0; i < cols_len; i++) {
				String cols_name = metaData.getColumnName(i + 1);
				Object cols_value = rs.getObject(cols_name);
				if (cols_value == null) {
					cols_value = "";
				}
				map.put(cols_name, cols_value);
			}
			list.add(map);
		}
		return list;
	}


	/**
	 * 释放资源
	 * 
	 * @param conn
	 * @param pstmt
	 * @param rs
	 */
	public void closeResources(Connection conn, PreparedStatement pstmt, ResultSet rs) {


		if (null != rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			} finally {
				if (null != pstmt) {
					try {
						pstmt.close();
					} catch (SQLException e) {
						e.printStackTrace();
						throw new RuntimeException(e);
					} finally {
						if (null != conn) {
							try {
								conn.close();
							} catch (SQLException e) {
								e.printStackTrace();
								throw new RuntimeException(e);
							}
						}
					}
				}
			}
		}
	}
}
<pre name="code" class="plain">jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.passwd=passwd
jdbc.url=jdbc:mysql://120.27.100.226:3306/authority
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值