?? databaseaccess.java
字號:
package phoneBookManager;
//描述: 提供JDBC與數據庫之間的連接,用作與數據庫有關的伺服對象的父類或成員。
//服務:query(sql) - 執行SQL語句sql的數據庫查詢操作
// callQuery(procedure) - 利用存儲過程procedure執行數據庫查詢操作
// update(sql) - 執行SQL語句sql的數據庫更新操作
// callUpdate(procedure) - 利用存儲過程procedure執行數據庫更新操作
//作者: 周曉聰、李文軍
//描述: 提供JDBC與數據庫之間的連接,用作與數據庫有關的伺服對象的父類或成員。
//作者: 周曉聰、李文軍
//版本: 1.01
//日期: 2003.09.11-2000.11.15
import java.sql.*;
import java.io.*;
public class DatabaseAccess {
protected final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
protected final String source = "jdbc:odbc:PhoneBook";
protected Connection connection; // 為數據庫建立的連接
protected Statement statement; // 將執行的SQL語句
protected PreparedStatement prepared; // 將執行的預編譯SQL語句
// 功能: 構造方法,建立與數據庫的連接。
public DatabaseAccess() throws SQLException {
try {
// 查找用于JDBC驅動的類,這種查找可向驅動器管理器注冊該數據庫驅動程序
Class.forName(driver);
} catch (ClassNotFoundException exc) {
// 當沒有驅動程序時,應用程序無法繼續運行,故退出程序
System.out.println("沒有發現驅動程序:" + driver);
exc.printStackTrace(); System.exit(1);
}
// 建立與指定數據庫的連接
connection = DriverManager.getConnection(source);
// 如果連接成功則檢測是否有警告信息
SQLWarning warn = connection.getWarnings();
while (warn != null) {
System.out.println(warn.getMessage());
warn = warn.getNextWarning();
}
// 創建一個用于執行簡單SQL的語句對象
statement = connection.createStatement();
}
// 功能: 關閉數據庫的連接。
public void close() throws SQLException {
// 關閉連接
if (prepared != null) prepared.close();
if (connection != null) connection.commit();
if (connection != null) connection.close();
}
// 功能: 利用一條SQL語句執行數據庫查詢操作。
// 參數: sql - SQL查詢語句串(如"SELECT * FROM user")
// 返回: 查詢結果集。
// 備注: sql語句是Unicode,必須轉換為ISO字符串才可執行(下同)。
public ResultSet query(String sql) throws SQLException {
ResultSet rs = statement.executeQuery(sql);
return rs;
}
// 功能: 利用一條SQL語句執行數據庫更新操作。
// 參數: sql - SQL更新語句串
public void update(String sql) throws SQLException {
statement.executeUpdate(sql);
}
//// 以預編譯SQL語句查詢與更新數據庫的方法
// 功能: 準備一條預編譯的SQL語句。
// 參數: sql - 執行查詢或更新的SQL語句
public void prepare(String sql) throws SQLException {
prepared = connection.prepareStatement(sql);
}
// 功能: 執行已編譯好的SQL語句以完成數據庫查詢操作。
// 返回: 查詢結果集
public ResultSet preparedQuery() throws SQLException {
ResultSet result = prepared.executeQuery();
return result;
}
// 功能: 執行已編譯好的SQL語句以完成數據庫更新操作。
public void preparedUpdate() throws SQLException {
prepared.executeUpdate();
}
// 功能: 封裝設置預編譯SQL語句參數的過程,這里使用重載函數的形式支持使用者
// 使用各種類型的參數設置參數。
public void setParameter(int index, boolean value) throws SQLException {
prepared.setBoolean(index, value);
}
public void setParameter(int index, byte value) throws SQLException {
prepared.setByte(index, value);
}
public void setParameter(int index, short value) throws SQLException {
prepared.setShort(index, value);
}
public void setParameter(int index, int value) throws SQLException {
prepared.setInt(index, value);
}
public void setParameter(int index, long value) throws SQLException {
prepared.setLong(index, value);
}
public void setParameter(int index, float value) throws SQLException {
prepared.setFloat(index, value);
}
public void setParameter(int index, double value) throws SQLException {
prepared.setDouble(index, value);
}
public void setParameter(int index, String value) throws SQLException {
prepared.setString(index, value);
}
public void setParameter(int index, byte[] value) throws SQLException {
prepared.setBytes(index, value);
}
public void setParameter(int index, Date value) throws SQLException {
prepared.setDate(index, value);
}
public void setParameter(int index, Timestamp value) throws SQLException {
prepared.setTimestamp(index, value);
}
public void setParameter(int index, Object value, int targetSqlType)
throws SQLException {
prepared.setObject(index, value, targetSqlType);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -