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

📄 connectbean.java

📁 考试管理系统:实现学生信息的注册、修改、查询、删除;以及对试题进行管理:增加试题、修改试题、删除试题、查询试题
💻 JAVA
字号:
/**
 * 数据库连接bean
 */
package edu.sccp.examManager.database;

import java.util.Properties;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectBean {
  private Connection conn = null;
  private ResultSet result = null;
  private PreparedStatement ps = null;
  private String driver;
  private String url;
  private String user;
  private String password;
  public Connection getConnection() {
    return conn;
  }

  public void setConnection(Connection conn) {
    this.conn = conn;
  }

  public ConnectBean() {

  }

  /**
   * 打开连接
   * @return boolean
   */
  public boolean openConnection() {
    boolean bl = false;
    if (!this.initProperty())
      return false;
    try {
      Class.forName(this.driver);
      if (conn == null) {
        this.conn = DriverManager.getConnection(this.url, this.user,
                                                this.password);
        System.out.println("conn ok!!! not datasource");
        bl = true;
      }
    }
    catch (Exception e) {
      System.out.println(e);
      return false;
    }
    return bl;
  }

  /**
   * 数据库连接初始化
   * Database connection initialization.
   * @return boolean
   */
  public boolean initProperty() {
    try {
      Properties ps = new Properties();
      ps.load(getClass().getResourceAsStream("/configure/db.properties"));//resort to API.......Properties class:load() method
      this.driver = ps.getProperty("driver");
      this.url = ps.getProperty("url");
      this.user = ps.getProperty("user");
      this.password = ps.getProperty("password");
      System.out.print("驱动:"+driver);
      System.out.print(" 协议:"+url);
      System.out.print(" 用户名:"+user);
      System.out.println("密 码:"+password+" ok!!!");
      return true;
    }
    catch (Exception e) {
      System.out.println(e);
      return false;
    }
  }

  /**
   * 数据库prepareStatement()操作
   * Database prepareStatement() operation.
   * @param sqlString String
   * @param isScrollAndUpdateTable boolean
   * @return PreparedStatement
   * @throws SQLException
   */
  public PreparedStatement createPreparedStatement(String sqlString,
      boolean isScrollAndUpdateTable) throws SQLException {
    if (isScrollAndUpdateTable)
      ps = conn.prepareStatement(sqlString,
                                 ResultSet.TYPE_SCROLL_SENSITIVE,//resort to API:java.sql.Connection.prepareStatement
                                 ResultSet.CONCUR_UPDATABLE);
    else
      ps = conn.prepareStatement(sqlString);
    return ps;
  }

  /**
   * 重载上个方法
   * overload the previous method.
   * @param sqlString String
   * @return PreparedStatement
   * @throws SQLException
   */
  public PreparedStatement createPreparedStatement(String sqlString) throws
      SQLException {
    ps = conn.prepareStatement(sqlString);
    return ps;
  }

  /**
   * 数据库executeUpdate操作
   * Database executeUpdate operation.
   * @return int
   * @throws SQLException
   */
  public int executeUpdate() throws SQLException {
    return ps.executeUpdate();
  }

  /**
   *
   * @return boolean
   * @throws SQLException
   */

  public boolean next() throws SQLException {
    return result != null ? result.next() : false;
  }

  public ResultSet executeQuery() throws SQLException {
    this.result = ps.executeQuery();//查询结果放入结果集result
    return result;
  }

  public String getString(int index) throws SQLException {
    return result.getString(index);
  }

  public boolean absolute(int index) throws SQLException {
    return result.absolute(index);
  }

  public boolean last() throws SQLException {
    return result.last();
  }

  public boolean first() throws SQLException {
    return result.first();
  }

  public boolean previous() throws SQLException {
    return result.previous();
  }

  public int getRow() throws SQLException {
    return result.getRow();
  }

  public int getInt(int index) throws SQLException {
    return result.getInt(index);
  }

  public float getFloat(int index) throws SQLException {
    return result.getFloat(index);
  }
/*
  public Date getDate(int index) throws SQLException{
      return result.getDate(index);
  }
*/

  public void setString(int index, String data) throws SQLException {
    ps.setString(index, data);
  }

  public void setInt(int index, int data) throws SQLException {
    ps.setInt(index, data);
  }

  public void setFloat(int index, float data) throws SQLException {
    ps.setFloat(index, data);
  }

  public void resetResult() throws SQLException {
    this.result = null;
  }

  public String iso2gb(String qs) {
    try {
      if (qs == null)
        return "NULL";
      else {
        // Used for TurboLinux
        return new String(qs.getBytes("iso-8859-1"), "gb2312");//构造一个新的 String,方法是使用指定的字符集解码指定的字节数组。查API:String
      }
    }
    catch (Exception e) {
      System.err.println("iso2gb error:" + e.getMessage());
    }
    return "NULL";
  }

  public String gb2iso(String qs) {
    try {
      if (qs == null)
        return "NULL";
      else {
        //Used for TurboLinux
        return new String(qs.getBytes("gb2312"), "iso-8859-1");
      }
    }
    catch (Exception e) {
      System.err.println("gb2iso error:" + e.getMessage());
    }
    return "NULL";
  }

  public void close() throws SQLException {
    System.out.println("conn Close()");
    this.conn.close();
  }

  public void finalize() throws Throwable {
    System.out.println("conn Close()...");
  }
}

⌨️ 快捷键说明

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