今天贴一个数据库通用类

通过Orcal和MySql测试过,可行


package com.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * <dl>
 * <dt><b>类机能概要:</b></dt><dd>
 * 
 * </dd>
 * </dl>
 * @copyright :Copyright 2010, IBM ETP. All right reserved.
 *【Update History】
 * Version	Date		Company		Name			Anken-No	Anken-Name
 * -------	----------	----------	--------------	----------	------------
 * 1.00		2010-9-28	IBM			Peng						create
 */
public class DBConnection {
	private final static String DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
	private final static String URL = "jdbc:oracle:thin:@localhost:1521:XE";
	private final static String USERID = "talk";
	private final static String PWD = "sa";
	private Connection conn;
	private PreparedStatement prepstmt;
	private Statement stmt;

	/**
	 * <dl>
	 * <dt><b>Method机能概要:</b></dt>
	 * <dd>构造方法,默认创建一个数据库连接<br>
	 * 并在内部生成一个Statement对象</dd>
	 * </dl>
	 */
	public DBConnection() {
		getConn();
		try {
			stmt = conn.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * <dl>
	 * <dt><b>Method机能概要:</b></dt>
	 * <dd>构造方法,默认创建一个数据库连接<br>
	 * 并使用参数传入的SQL语句,创建一个PreparedStatement对象
	 * </dd>
	 * </dl>
	 * @param sql
	 */
	public DBConnection(String sql) {
		getConn();
		try {
			prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * <dl>
	 * <dt><b>Method机能概要:</b></dt>
	 * <dd>私有方法,创建一个数据库连接
	 * 并设置回滚点</dd>
	 * </dl>
	 */
	private void getConn() {
		try {
			Class.forName(DRIVER_CLASS);
			conn = DriverManager.getConnection(URL, USERID, PWD);
			// 禁止自动提交
			conn.setAutoCommit(false);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * <dl>
	 * <dt><b>Method机能概要:</b></dt>
	 * <dd>得到Connection对象</dd>
	 * </dl>
	 * @return Connection
	 */
	public Connection getConnection() {
		return conn;
	}
	
	/**
	 * <dl>
	 * <dt><b>Method机能概要:</b></dt>
	 * <dd>向数据库提交所有改变</dd>
	 * </dl>
	 * @throws SQLException
	 */
	public void commit() throws SQLException {
		conn.commit();
	}
	
	/**
	 * <dl>
	 * <dt><b>Method机能概要:</b></dt>
	 * <dd>回滚到初始状态</dd>
	 * </dl>
	 * @throws SQLException
	 */
	public void rollback() throws SQLException {
		conn.rollback();
	}

	/**
	 * <dl>
	 * <dt><b>Method机能概要:</b></dt>
	 * <dd>使用PreparedStatement对象处理传入的SQL语句</dd>
	 * </dl>
	 * @param sql
	 * @throws SQLException
	 */
	public void prepareStatement(String sql) throws SQLException {
		prepstmt = conn.prepareStatement(sql);
	}

	public void setString(int index, String value) throws SQLException {
		prepstmt.setString(index, value);
	}

	public void setInt(int index, int value) throws SQLException {
		prepstmt.setInt(index, value);
	}

	public void setBoolean(int index, boolean value) throws SQLException {
		prepstmt.setBoolean(index, value);
	}

	public void setDate(int index, Date value) throws SQLException {
		prepstmt.setDate(index, value);
	}

	public void setLong(int index, long value) throws SQLException {
		prepstmt.setLong(index, value);
	}

	public void setFloat(int index, float value) throws SQLException {
		prepstmt.setFloat(index, value);
	}

	public void setBinaryStream(int index, InputStream in, int length)
			throws SQLException {
		prepstmt.setBinaryStream(index, in, length);
	}

	public void clearParameters() throws SQLException {
		prepstmt.clearParameters();
	}

	public PreparedStatement getPreparedStatement() {
		return prepstmt;
	}

	public Statement getStatement() {
		return stmt;
	}

	/**
	 * 执行Statement查询语句
	 * 
	 * @param sql
	 * @return ArrayList
	 * @throws Exception
	 */
	public ArrayList<HashMap<Object, Object>> executeQuery(String sql) throws Exception {
		if (stmt != null) {
			return this.convertResultSetToArrayList(stmt.executeQuery(sql));
		} else {
			return null;
		}
	}

	/**
	 * 执行PreparedStatement查询语句
	 * 
	 * @return ArrayList
	 * @throws Exception
	 */
	public ArrayList<HashMap<Object, Object>> executeQuery() throws Exception {
		if (prepstmt != null) {
			return this.convertResultSetToArrayList(prepstmt.executeQuery());
		} else {
			return null;
		}
	}
	
	/**
	 * 执行PreparedStatement查询语句
	 * 返回一个ResultSet对象
	 * 
	 * @return ResultSet
	 * @throws Exception
	 */
	public ResultSet executeQueryResultSet() {
		ResultSet resultSet = null;
		return resultSet;
	}

	/**
	 * 执行Statement更改语句
	 * 
	 * @param sql
	 * @throws SQLException
	 */
	public void executeUpdate(String sql) throws SQLException {
		if (stmt != null)
			stmt.executeUpdate(sql);
	}

	/**
	 * 执行PreparedStatement更改语句
	 * 
	 * @throws SQLException
	 */
	public void executeUpdate() throws SQLException {
		if (prepstmt != null)
			prepstmt.executeUpdate();
	}

	/**
	 * 转换记录集对象为数组列表对象
	 * 
	 * @param rs
	 * @return ArrayList
	 * @throws Exception
	 */
	private ArrayList<HashMap<Object, Object>> convertResultSetToArrayList(
			ResultSet rs) throws Exception {
		// 获取rs 集合信息对象
		ResultSetMetaData rsmd = rs.getMetaData();
		// 创建数组列表集合对象
		ArrayList<HashMap<Object, Object>> tempList = new ArrayList<HashMap<Object, Object>>();
		HashMap<Object, Object> tempHash = null;
		// 填充数组列表集合
		while (rs.next()) {
			// 创建键值对集合对象
			tempHash = new HashMap<Object, Object>();
			for (int i = 0; i < rsmd.getColumnCount(); i++) {
				// 遍历每列数据,以键值形式存在对象tempHash中
				tempHash.put(rsmd.getColumnName(i + 1).toUpperCase(), rs
						.getString(rsmd.getColumnName(i + 1)));
			}
			// 第一个键值对,存储在tempList列表集合对象中
			tempList.add(tempHash);
		}
		return tempList;// 返回填充完毕的数组列表集合对象
	}

	/**
	 * 关闭连接
	 */
	public void close() {
		try {
			if (stmt != null) {
				stmt.close();
				stmt = null;
			}
			if (prepstmt != null) {
				prepstmt.close();
				prepstmt = null;
			}
			conn.close();
			conn = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


简单点应用,回去查询结果及List

package com.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.util.DBConnection;

public class GetListAction extends ActionSupport{
	private List list;
	public String execute() throws Exception {
		DBConnection con = new DBConnection();
		String sql ="select * from admin";
		list = con.executeQuery(sql);
		con.commit();
		con.close();
		return SUCCESS;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}

}

通过JSTL标签结合EL表达式,把结果集List显示到前台页面(配置struts.xml就不写了,注意导入JSTL所需要的架包jstl.jar  standard.jar)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>NewList</title>
</head>
<body>
<table width="20%" border="1">
	<tr>
		<td>行号</td>
		<td>姓名</td>
		<td>密码</td>
	</tr>
	<c:forEach items="${list}" var="list" varStatus="i">
		<tr>
		<td>${i.index+1 }</td>
			<td><a href="index.action?username=${list.USERNAME }&password=${list.PASSWORD }">${list.USERNAME }</a></td>
			<td>${list.PASSWORD }</td>
		</tr>
	</c:forEach>
</table>
</body>
</html>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值