數(shù)據(jù)庫操作封裝JavaBean

在使用Hibernate之前常常使用這個JavaBean,類似于Net中的sqlHelper。

package beans;

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 DBUtil {
	/**
	 * 取得一個數(shù)據(jù)庫連接
	 * @return
	 * @throws SQLException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 */
	public Connection getConnection() throws SQLException,
			InstantiationException, IllegalAccessException,
			ClassNotFoundException {
		Connection conn = null;
		//加載數(shù)據(jù)庫驅(qū)動類
		Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
				.newInstance();
		//數(shù)據(jù)庫連接URL
		String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
		//數(shù)據(jù)庫用戶名
		String user = "sa";
		//數(shù)據(jù)庫密碼
		String password = "1985315";
		//根據(jù)數(shù)據(jù)庫參數(shù)取得一個數(shù)據(jù)庫連接
	    conn = DriverManager.getConnection(url, user, password);
		return conn;
	}

	/**
	 * 根據(jù)傳入的SQL語句返回一個結(jié)果集
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public ResultSet select(String sql) throws Exception {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			conn = getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			return rs;
		} catch (SQLException sqle) {
			throw new SQLException("select data exception: "
					+ sqle.getMessage());
		} catch (Exception e) {
			throw new Exception("System e exception: " + e.getMessage());
		} 
		
	}

	/**
	 * 根據(jù)傳入的SQL語句向數(shù)據(jù)庫增加一條記錄
	 * @param sql
	 * @throws Exception
	 */
	public void insert(String sql) throws Exception {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			ps.executeUpdate();
		} catch (SQLException sqle) {
			throw new Exception("insert data exception: " + sqle.getMessage());
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (Exception e) {
				throw new Exception("ps close exception: " + e.getMessage());
			}
		}
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			throw new Exception("connection close exception: " + e.getMessage());
		}
	}

	/**
	 * 根據(jù)傳入的SQL語句更新數(shù)據(jù)庫記錄
	 * @param sql
	 * @throws Exception
	 */
	public void update(String sql) throws Exception {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			ps.executeUpdate();
		} catch (SQLException sqle) {
			throw new Exception("update exception: " + sqle.getMessage());
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (Exception e) {
				throw new Exception("ps close exception: " + e.getMessage());
			}
		}
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			throw new Exception("connection close exception: " + e.getMessage());
		}
	}

	/**
	 * 根據(jù)傳入的SQL語句刪除一條數(shù)據(jù)庫記錄
	 * @param sql
	 * @throws Exception
	 */
	public void delete(String sql) throws Exception {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			ps.executeUpdate();
		} catch (SQLException sqle) {
			throw new Exception("delete data exception: " + sqle.getMessage());
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (Exception e) {
				throw new Exception("ps close exception: " + e.getMessage());
			}
		}
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			throw new Exception("connection close exception: " + e.getMessage());
		}
	}
}


分頁操作JavaBean

在操作報表的時候常常用到,方便分頁顯示。

package beans;

public class Page {
    private int totalPage;//總頁數(shù)
    private int currentPage;//當(dāng)前頁數(shù)
    private int totalRecord;//總的記錄條數(shù)
    private int currentRecord;//當(dāng)前記錄的條數(shù)
    private int pageSize = 6;//每頁顯示的記錄數(shù)量,這里默認(rèn)每頁顯示6條
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentRecord,int pageSize ) {
        //如果當(dāng)前記錄數(shù)除以每頁顯示條數(shù)可以整除,商就是當(dāng)前的頁碼
		if(currentRecord%pageSize == 0) 
		{
			currentPage = currentRecord/pageSize;
		}else
		{
           //如果當(dāng)前記錄數(shù)除以每頁顯示條數(shù)不能整除,商加1才是當(dāng)前的頁碼
			currentPage = currentRecord/pageSize+1;
		}
	}
	public int getCurrentRecord() {
		return currentRecord;
	}
	public void setCurrentRecord(int currentRecord) {
		this.currentRecord = currentRecord;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalRecord,int pageSize) {
        //如果總記錄數(shù)除以每頁顯示條數(shù)可以整除,商就是總頁碼
		if(totalRecord%pageSize == 0) 
		{
			totalPage = totalRecord/pageSize;
		}else
		{
           //如果總記錄數(shù)除以每頁顯示條數(shù)不能整除,商加1才是總頁碼
			totalPage = totalRecord/pageSize+1;
		}
	}
	public int getTotalRecord() {
		return totalRecord;
	}
	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}
    
}


作者:beijiguangyong 發(fā)表于2012-2-29 23:52:40 原文鏈接
閱讀:677 評論:15 查看評論