數據庫操作封裝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 {
/**
* 取得一個數據庫連接
* @return
* @throws SQLException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws ClassNotFoundException
*/
public Connection getConnection() throws SQLException,
InstantiationException, IllegalAccessException,
ClassNotFoundException {
Connection conn = null;
//加載數據庫驅動類
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
.newInstance();
//數據庫連接URL
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//數據庫用戶名
String user = "sa";
//數據庫密碼
String password = "1985315";
//根據數據庫參數取得一個數據庫連接
conn = DriverManager.getConnection(url, user, password);
return conn;
}
/**
* 根據傳入的SQL語句返回一個結果集
* @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());
}
}
/**
* 根據傳入的SQL語句向數據庫增加一條記錄
* @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());
}
}
/**
* 根據傳入的SQL語句更新數據庫記錄
* @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());
}
}
/**
* 根據傳入的SQL語句刪除一條數據庫記錄
* @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;//總頁數
private int currentPage;//當前頁數
private int totalRecord;//總的記錄條數
private int currentRecord;//當前記錄的條數
private int pageSize = 6;//每頁顯示的記錄數量,這里默認每頁顯示6條
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentRecord,int pageSize ) {
//如果當前記錄數除以每頁顯示條數可以整除,商就是當前的頁碼
if(currentRecord%pageSize == 0)
{
currentPage = currentRecord/pageSize;
}else
{
//如果當前記錄數除以每頁顯示條數不能整除,商加1才是當前的頁碼
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) {
//如果總記錄數除以每頁顯示條數可以整除,商就是總頁碼
if(totalRecord%pageSize == 0)
{
totalPage = totalRecord/pageSize;
}else
{
//如果總記錄數除以每頁顯示條數不能整除,商加1才是總頁碼
totalPage = totalRecord/pageSize+1;
}
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
}
作者:beijiguangyong 發表于2012-2-29 23:52:40
原文鏈接