📄 sqlfacade.java
字号:
package util.database;import java.sql.*;import java.io.*;/** * SqlFacade * 处理数据库的连接和访问 * * @author Michael Zeng * @version 1.0 September 18, 2002 */public class SqlFacade{ private Connection conn = null; private Statement stmt = null; private PreparedStatement prepstmt = null; /** * 构造数据库的连接和访问类,使用Statement * * @param conn 给定的连接 * * @throws SQLException */ public SqlFacade(Connection conn) throws SQLException { this.conn = conn; stmt = this.conn.createStatement(); } /** * 构造数据库的连接和访问类,使用Statement。使用滚动的结果集,默认游标只读。 * * @param conn 给定的连接 * @param isScrollSensitive 滚动敏感性标识 * * @throws SQLException */ public SqlFacade(Connection conn, boolean isScrollSensitive) throws SQLException { this.conn = conn; if(isScrollSensitive == false) this.stmt = this.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); else this.stmt = this.conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); } /** * 构造数据库的连接和访问类,使用PreparedStatement * * @param conn 给定的连接 * @param sql 包含占位符的SQL语句 * * @throws SQLException */ public SqlFacade(Connection conn, String sql) throws SQLException { this.conn = conn; this.prepareStatement(sql); } /** * 构造数据库的连接和访问类,使用PreparedStatement,使用滚动的结果集,默认游标只读。 * * @param conn 给定的连接 * @param sql 包含占位符的SQL语句 * @param isScrollSensitive 滚动敏感性标识 * * @throws SQLException */ public SqlFacade(Connection conn, String sql, boolean isScrollSensitive) throws SQLException { this.conn = conn; if(isScrollSensitive == false) this.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); else this.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); } /** * 返回连接 * * @return Connection 连接 */ public Connection getConnection() { return conn; } /** * PreparedStatement * * @param sql 预设SQL语句 * * @throws SQLException */ public void prepareStatement(String sql) throws SQLException { prepstmt = conn.prepareStatement(sql); } /** * PreparedStatement指定结果集类型和同步类型 * * @param sql 包含占位符的SQL语句 * @param resultSetType 结果集类型 * @param resultSetConcurrency 同步类型 * * @throws SQLException */ public void prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { prepstmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency); } 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 setBinaryStream(int index,InputStream in,int length) throws SQLException { prepstmt.setBinaryStream(index,in,length); } /** * 清除所有设定的参数 * * @throws SQLException */ public void clearParameters() throws SQLException { prepstmt.clearParameters(); } /** * 返回预设状态的准备语句 * * @return PreparedStatement语句 */ public PreparedStatement getPreparedStatement() { return prepstmt; } /** * 返回状态的语句 * @return Statement 状态 */ public Statement getStatement() { return stmt; } /** * 执行SQL语句返回字段集,使用Statement语句 * * @param sql SQL语句 * @return ResultSet 字段集 * * @throws SQLException */ public ResultSet executeQuery(String sql) throws SQLException { if (stmt == null) stmt = this.conn.createStatement(); return stmt.executeQuery(sql); } /** * 执行设定好的PreparedStatement语句返回字段集,使用PreparedStatement语句 * * @return ResultSet 字段集 * * @throws SQLException */ public ResultSet executeQuery() throws SQLException { if (prepstmt != null) return prepstmt.executeQuery(); else return null; } /** * 执行SQL Update语句,使用Statement语句 * * @param sql SQL语句 * * @throws SQLException */ public void executeUpdate(String sql) throws SQLException { if (stmt == null) stmt = this.conn.createStatement(); stmt.executeUpdate(sql); } /** * 执行SQL Update语句,使用PreparedStatement语句 * * @throws SQLException */ public void executeUpdate() throws SQLException { if (prepstmt != null) prepstmt.executeUpdate(); } /** * 关闭Statement和PreparedStatement * * @throws SQLException */ public void close() throws SQLException { if (stmt != null) { stmt.close(); stmt = null; } if (prepstmt != null) { prepstmt.close(); prepstmt = null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -