📄 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 + -