?? mysql.java
字號:
package fengyun.Fastmail.beans;
import java.sql.*;
/**
* 處理數據庫的連接和訪問
* @author sanware
* @version 1.01
*/
public class Mysql {
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;
private static BeansConstants CONST = BeansConstants.getInstance();
/**
* 構造數據庫的連接和訪問類
*/
public Mysql() throws Exception {
Class.forName(CONST.dbdriver);
conn = DriverManager.getConnection(CONST.dburl);
stmt = conn.createStatement();
}
public Mysql(String sql) throws Exception {
Class.forName(CONST.dbdriver);
conn = DriverManager.getConnection(CONST.dburl);
this.prepareStatement(sql);
}
/**
* 返回連接
* @return Connection 連接
*/
public Connection getConnection() {
return conn;
}
/**
* PreparedStatement
* @return sql 預設SQL語句
*/
public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
}
/**
* 設置對應值
* @param index 參數索引
* @param value 對應值
*/
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 clearParameters()
throws SQLException
{
prepstmt.clearParameters();
}
/**
* 返回預設狀態
*/
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
/**
* 返回狀態
* @return Statement 狀態
*/
public Statement getStatement() {
return stmt;
}
/**
* 執行SQL語句返回字段集
* @param sql SQL語句
* @return ResultSet 字段集
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
}
else return null;
}
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
}
else return null;
}
/**
* 執行SQL語句
* @param sql SQL語句
*/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
public void executeUpdate() throws SQLException {
if (prepstmt != null)
prepstmt.executeUpdate();
}
/**
* 關閉連接
*/
public void close() throws Exception {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
conn.close();
conn = null;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -