📄 databaseaccess.java
字号:
package olts.data;
import java.sql.*;
public class DatabaseAccess {
protected final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
protected final String source = "jdbc:odbc:";
protected Connection connection; // 为数据库建立的连接
// 与特定数据库的连接(会话)。在连接上下文中执行 SQL 语句并返回结果。
protected Statement statement; // 将执行的SQL语句
protected PreparedStatement prepared; // 将执行的预编译SQL语句
/**
* 利用数据源名称建立与数据库的连接
* @param datasource 数据源名称
* @throws SQLException
*/
public DatabaseAccess(String datasource) throws SQLException {
try {
// 查找用于JDBC驱动的类,这种查找可向驱动器管理器注册该数据库驱动程序
Class.forName(driver);
} catch (ClassNotFoundException exc) {
// 当没有驱动程序时,应用程序无法继续运行,故退出程序
exc.printStackTrace();
System.exit(1);
}
// 建立与指定数据库的连接
connection = DriverManager.getConnection(this.source+datasource);
// 创建一个用于发送和执行简单SQL语句的对象
statement = connection.createStatement();
}
/**
* 关闭数据库的连接
* @throws SQLException
*/
public void close() throws SQLException {
// 关闭连接
if (prepared != null) prepared.close();
// 使上一次提交/回滚后进行的所有更改成为持久更改,并释放此 Connection 对象当前持有的所有数据库锁
if (connection != null) connection.commit();
if (connection != null) connection.close();
}
/**
* 不带参数简单SQL查询,支持一条SQL语句
* @param sql SQL查询语句串
* @return 查询结果集
* @throws SQLException
*/
public ResultSet query(String sql) throws SQLException {
ResultSet rs = statement.executeQuery(sql);
return rs;
}
/**
* 利用不带参数的SQL语句执行数据库更新操作
* @param sql sql语句
* @throws SQLException
*/
public void update(String sql) throws SQLException {
statement.executeUpdate(sql);
}
/**
* 以预编译参数的SQL语句查询与更新数据库的方法
* @param sql sql语句
* @throws SQLException
*/
public void prepare(String sql) throws SQLException {
prepared = connection.prepareStatement(sql);
}
/**
* 执行已编译好的SQL语句以完成数据库查询操作
* @return 查询结果集
* @throws SQLException
*/
public ResultSet preparedQuery() throws SQLException {
ResultSet result = prepared.executeQuery();
return result;
}
/**
* 执行已编译好的SQL语句以完成数据库更新操作。
* @throws SQLException
*/
public void preparedUpdate() throws SQLException {
prepared.executeUpdate();
}
/**
* 使用重载方法的形式封装设置预编译SQL语句参数的过程
* @param index sql参数位置标签
* @param value sql参数值
* @throws SQLException
*/
public void setParameter(int index, boolean value) throws SQLException {
// 将指定参数设置为给定 Java boolean 值。在将此值发送到数据库时,驱动程序将它转换成一个 SQL BIT 或 BOOLEAN 值
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);
}
/**
* 使用给定对象设置指定参数的值
* @param index 同上重载方法
* @param value 同上重载方法
* @param targetSqlType 将发送给数据库的 SQL 类型(定义于 java.sql.Types 中)
* @throws SQLException
*/
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 + -