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

📄 dbmanager.java

📁 实习时做的人事系统
💻 JAVA
字号:
/**
 * 数据库操作类
 */
package com.personnel;

/**
 * DbManager.java
 * 
 * @author 陈辉鸿 Dec 7, 2007
 */
import java.sql.*;

public class DbManager {
	// 数据库的驱程标识符
	public final static String JAVUSQL_DRIVER = "com.javujavu.javusql.Driver";
	public final static String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
	// 数据库的URL
	public final static String JAVUSQL_URL = "jdbc:javusql:chat_record\\message.dbs";
	public final static String MYSQL_URL = "jdbc:mysql://localhost:3306/person?zeroDateTimeBehavior=convertToNull";
	// 指示结果集只可前向操作
	public final static int TYPE_FORWARD = ResultSet.TYPE_FORWARD_ONLY;
	// 指示结果集支持滚动
	public final static int TYPE_SCROLL_SENSITIVE = ResultSet.TYPE_SCROLL_SENSITIVE;
	// 指示结果集只可用于读取
	public final static int CURRENCY_READ = ResultSet.CONCUR_READ_ONLY;
	public final static int CONCUR_UPDATABLE = ResultSet.CONCUR_UPDATABLE;
	// 命令执行对象
	private Statement st = null;
	// 数据库连接
	private Connection connection = null;

	public DbManager(String driver, String url, String user, String password,
			int type, int concurrency) {

		if (!this.initialize(driver, url, user, password, type, concurrency)) {
			System.err.println("数据库初始化失败");
			return;
		}
	}

	public Connection getConnection() {
		return this.connection;
	}

	/**
	 * 执行初始化工作
	 * 
	 * @param driver
	 *            数据库驱程
	 * @param url
	 *            数据库url
	 * @param user
	 *            用户名
	 * @param password
	 *            密码
	 * @param type
	 *            结果集类型
	 * @param concurrency
	 *            结果集并发性
	 * @return 成功返回true
	 */
	private boolean initialize(String driver, String url, String user,
			String password, int type, int concurrency) {

		if (!this.installDbDriver(driver)) {
			return false;
		}

		this.connection = this.createConnection(url, user, password);
		if (this.connection == null) {
			return false;
		}

		this.st = this.createStatement(type, concurrency);
		if (this.st == null) {
			return false;
		}

		return true;
	}

	/**
	 * 加载数据库驱程
	 * 
	 * @param dbDriver
	 *            驱程名
	 * @return 加载成功返回true
	 */
	private boolean installDbDriver(String dbDriver) {

		try {
			Class.forName(dbDriver);
		} catch (ClassNotFoundException e) {
			System.err.println("无法加载数据库驱程" + e);
			return false;
		}

		return true;
	}

	/**
	 * 创建数据库链接
	 * 
	 * @param dbURL
	 *            数据库url
	 * @param user
	 *            用户名
	 * @param password
	 *            密码
	 * @return 创建成功返回相应链接
	 */
	private Connection createConnection(String dbURL, String user,
			String password) {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(dbURL, user, password);
		} catch (SQLException e) {
			System.err.println("无法连接数据库" + dbURL + e);
			return null;
		}
		return conn;
	}

	/**
	 * 创建命令执行对象
	 * 
	 * @param type
	 *            ResultSet类型
	 * @param concurrency
	 *            ResultSet并发性
	 * @return 创建成功返回相应命令执行对象
	 */
	private Statement createStatement(int type, int concurrency) {

		Statement statement = null;
		try {
			statement = this.connection.createStatement(type, concurrency);
		} catch (SQLException e) {
			System.err.println("创建命令对象失败" + e);
			return null;
		}
		return statement;
	}

	/**
	 * 执行数据库查询操作
	 * 
	 * @param sql
	 *            查询语句
	 * @return 结果集
	 */
	public ResultSet executeQuery(String sql) {
		ResultSet rs = null;
		try {
			rs = this.st.executeQuery(sql);
		} catch (SQLException e) {
			System.out.println("执行sql语句 '" + sql + "' 出错" + e);
			return null;
		}
		return rs;
	}

	/**
	 * 执行数据库更新操作,包括update和insert等
	 * 
	 * @param sql
	 *            更新语句
	 * @return 影响到的行数
	 */
	public int executeUpdate(String sql) {
		int result;

		try {
			result = this.st.executeUpdate(sql);
		} catch (SQLException e) {
			System.out.println("执行sql语句 '" + sql + "' 出错" + e);
			return -1;
		}
		return result;
	}

	public boolean executePrepareStatement(String sql, Object[] values) {
		try {
			PreparedStatement p = this.connection.prepareStatement(sql);
			for (int i = 0; i < values.length; i++) {
				p.setObject(i + 1, values[i]);
			}
			p.execute();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * 关闭数据库连接
	 * 
	 * @return 成功关闭返回true
	 */
	public boolean closeDb() {

		try {

			this.st.close();
			this.connection.close();
		} catch (SQLException e) {
			System.err.println("关闭连接失败" + e);
			return false;
		}
		return true;
	}

}

⌨️ 快捷键说明

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