⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 60bc1db1b29c001c1d70bb5a864c2e99

📁 源代码
💻
字号:
/**
 *  @author: Wangmin
 *  School: Computer School
 *  University: Sichuan University
 *  Date: 2007-11
 *  Filename:DBConnect.java
 **/

package staff.database; 
import java.sql.*;

public class DBConnect {
	private Connection conn = null;

	private Statement stmt = null;

	private PreparedStatement prepstmt = null;

	private String dbUrl;
        private String password;
        private String username;
        private String jdbcClassName;


	 public DBConnect() throws Exception
    {
		dbUrl = "jdbc:postgresql:postgres";
		jdbcClassName = "org.postgresql.Driver";
		username = "postgres";
		password = "123";
		connect();
    }
    public DBConnect( String inUrl, String inJdbcClassName, String inUserName, String inPassWord )
    		throws Exception
    {
		dbUrl = inUrl;
		jdbcClassName = inJdbcClassName;
		username = inUserName;
		password = inPassWord;
		connect();
    }
    private boolean connect()throws Exception 
    {
		boolean opened = false;
		// Set the JDBC class name.
		Driver driver = (Driver) Class.forName(jdbcClassName).newInstance();
		DriverManager.registerDriver(driver);
		
		// Try to open a connection the database.
		conn = DriverManager.getConnection(dbUrl, username, password);
		opened = true;
	    return opened;
    }

	/**
	 * PreparedStatement
	 * 
	 * @param  sql 预设SQL语句
	 */
	public void prepareStatement(String sql) throws SQLException {
		prepstmt = conn.prepareStatement(sql);
	}

	public void prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		prepstmt = conn.prepareStatement(sql, resultSetType,
				resultSetConcurrency);
	}

	/**
	 * 设置对应值
	 * 
	 * @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 setTimestamp(int index, Timestamp value) throws SQLException {
		prepstmt.setTimestamp(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 setBytes(int index, byte[] value) throws SQLException {
		prepstmt.setBytes(index, value);
	}

	public void clearParameters() throws SQLException {
		prepstmt.clearParameters();
		prepstmt = null;
	}

	/**
	 * 返回预设状态
	 */
	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();
	}


    /**
	 * 返回连接
	 * 
	 * @return Connection 连接
	 */
	public Connection getConnection() {
		return conn;
	}



	/**
	 * 关闭连接
	 */
	public void close() throws Exception {
		if (stmt != null) {
			stmt.close();
			stmt = null;
		}
		if (prepstmt != null) {
			prepstmt.close();
			prepstmt = null;
		}
		if (conn!=null) {
			conn.close();
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -