?? mysql.java
字號:
/*
* Created on 2005-4-14
*
* @author guopengfei_8218@163.com
* -------------
* 2006-5-13 修改
* 1. 加入了數據庫連接池 ,節約系統資源。
*/
package com.jxyd.sql;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 訪問數據庫操作的通用類
*/
public class Mysql {
// 數據庫連接
private Connection _conn;
// 執行語句
private Statement _stmt;
// 預存儲執行語句
private PreparedStatement _pstmt;
Linkdb linkdb;
/**
* 默認的構造方法 建立數據庫連接
*
*/
public Mysql() {
initConnection();
}
/**
* 用現有的數據連接初始化當前連接
*
* @param conn
*/
public Mysql(Connection conn) {
_conn = conn;
}
/**
* 返回當前連接
*
* @return
*/
public Connection getConnection() {
return _conn;
}
/**
* 執行預存儲語句
*
* @param sql
* @throws SQLException
*/
public void prepareStatement(String sql) throws SQLException {
_pstmt = _conn.prepareStatement(sql);
}
/**
* 執行預存儲語句
*
* @param sql
* @param isScrollSensitive
* 是否可以自由滾動
* @throws SQLException
*/
public void prepareStatement(String sql, boolean isScrollSensitive)
throws SQLException {
if (!isScrollSensitive) {
_pstmt = _conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
} else {
_pstmt = _conn
.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
}
/**
* 執行預存儲語句
*
* @param sql
* @param resultSetType
* 記錄集類型
* @param resultSetConcurrency
* @throws SQLException
*/
public void prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
_pstmt = _conn.prepareStatement(sql, resultSetType,
resultSetConcurrency);
}
/**
*
* @param index
* @param value
* @throws SQLException
*/
public void setString(int index, String value) throws SQLException {
_pstmt.setString(index, value);
/**
*
* @param index
* @param value
* @throws SQLException
*/
public void setInt(int index, int value) throws SQLException {
_pstmt.setInt(index, value);
}
/**
*
* @param index
* @param value
* @throws SQLException
*/
public void setBoolean(int index, boolean value) throws SQLException {
_pstmt.setBoolean(index, value);
}
/**
*
* @param index
* @param value
* @throws SQLException
*/
public void setDate(int index, Date value) throws SQLException {
_pstmt.setDate(index, value);
}
/**
*
* @param index
* @param value
* @throws SQLException
*/
public void setLong(int index, long value) throws SQLException {
_pstmt.setLong(index, value);
}
/**
*
* @param index
* @param value
* @throws SQLException
*/
public void setFloat(int index, float value) throws SQLException {
_pstmt.setFloat(index, value);
}
/**
*
* @param index
* @param value
* @throws SQLException
*/
public void setDouble(int index, double value) throws SQLException {
_pstmt.setDouble(index, value);
}
/**
*
* @param index
* @param in
* @param length
* @throws SQLException
*/
public void setBinaryStream(int index, InputStream in, int length)
throws SQLException {
_pstmt.setBinaryStream(index, in, length);
}
/**
* 返回預存儲語句參數
*
* @return
*/
public PreparedStatement getPreparedStatement() {
return _pstmt;
}
/**
* 清除預存儲語句參數
*
*/
public void clearParameters() {
try {
_pstmt.clearParameters();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
/**
* 執行插入或更新操作
*
* @param sql
* @throws SQLException
*/
public void execute(String sql) throws SQLException {
if (_stmt == null) {
_stmt = _conn.createStatement();
}
_stmt.execute(sql);
}
/**
* 執行插入或更新操作
*
* @throws SQLException
*/
public boolean execute() throws SQLException {
if (_pstmt != null) {
return _pstmt.execute();
}
return false;
}
/**
* 執行查詢
*
* @param sql
* @return
* @throws SQLException
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (_stmt == null) {
_stmt = _conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
return _stmt.executeQuery(sql);
}
/**
* 執行查詢
*
* @return
* @throws SQLException
*/
public ResultSet executeQuery() throws SQLException {
if (_pstmt != null) {
return _pstmt.executeQuery();
} else {
return null;
}
}
/**
* 執行更新操作
*
* @param sql
* @throws SQLException
*/
public void executeUpdate(String sql) throws SQLException {
if (_stmt == null) {
_stmt = _conn.createStatement();
}
_stmt.executeUpdate(sql);
}
/**
* 執行更新操作
*
* @throws SQLException
*/
public int executeUpdate() throws SQLException {
if (_pstmt != null) {
return _pstmt.executeUpdate();
}
return 0;
}
/**
* 關閉連接,釋放資源
*
*/
public void freeResource() {
try {
if (_stmt != null) {
_stmt.close();
}
if (_pstmt != null) {
_pstmt.close();
}
if (_conn != null) {
_conn.close();
}
_stmt = null;
_pstmt = null;
_conn = null;
linkdb.releaseConnection();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
/**
* 設置事務的提交方式
*
* @param autoCommit
*/
public void setAutoCommit(boolean autoCommit) {
try {
_conn.setAutoCommit(autoCommit);
} catch (SQLException se) {
se.printStackTrace();
}
}
/**
* 事務提交
*
*/
public void commit() {
try {
_conn.commit();
} catch (SQLException se) {
se.printStackTrace();
}
}
/**
* 事務回滾
*
*/
public void rollback() {
try {
_conn.rollback();
} catch (SQLException se) {
se.printStackTrace();
}
}
/**
* 初始化數據庫連接
*/
private void initConnection() {
try {
linkdb = new Linkdb();
linkdb.openConnection("dbConn");
this._conn = linkdb.getConnection();
this._conn.setAutoCommit(false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -