JDBC通用操作类

  关于数据库的主要操作有:获取数据库连接;数据库查询、插入、修改、删除;
  断开数据库连接。这些数据库操作对于操作不同的数据表应该说都是统一的,
  因此,数据库的JDBC操作是可以做成一个通用类,这样就能达到重用目的。

package com.jdon.simpleregister;

import java.sql.*;
import javax.sql.*;
import java.io.*;
import javax.naming.*;

public class Mysql {

  private Connection conn = null;
  private Statement stmt = null;
  private PreparedStatement prepstmt = null;

  /**
   * 以创建Statement 初始化Mysql
   */
  public Mysql() {
    try {
      getDataSource();
      stmt = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Mysql init error: " + e);
    }
  }

  private void getDataSource() {
    try {
      Context ctx = new InitialContext();
      if (ctx == null)
        throw new Exception("Boom - No Context");

      DataSource ds =
          (DataSource) ctx.lookup("java:comp/env/jdbc/userDB");
      if (ds != null)
        conn = ds.getConnection();

    } catch (Exception e) {
      System.err.println("getDataSource() error: " + e);
    }
  }

//数据库Connection是使用DataSource接口建立,它为用户提供的能力比DriverManager提供的基本Connection对象的能力要多得多v

  private void getDirectConn(){
    try {
      Class.forName(Constants.dbdriver).newInstance();
      conn = DriverManager.getConnection(Constants.dburl);
    } catch (Exception e) {
      System.err.println("getDataSource() error: " + e);
    }

  }

  /**
   * 以创建PreparedStatement 初始化Mysql
   */
  public Mysql(String sql) {
    try {
      getDataSource();
      prepareStatement(sql);
    } catch (Exception e) {
      System.err.println("Mysql init error: " + e);
    }
  }

  public Connection getConnection() {
    return conn;
  }

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

  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);
  }

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

  public PreparedStatement getPreparedStatement() {
    return prepstmt;
  }

  public Statement getStatement() {
    return stmt;
  }

  /**
   * 执行Statement查询语句
   * @param sql
   * @return
   * @throws SQLException
   */
  public ResultSet executeQuery(String sql) throws SQLException {
    if (stmt != null) {
      return stmt.executeQuery(sql);
    } else
      return null;
  }

  /**
   * 执行PreparedStatement查询语句
   * @return
   * @throws SQLException
   */
  public ResultSet executeQuery() throws SQLException {
    if (prepstmt != null) {
      return prepstmt.executeQuery();
    } else
      return null;
  }

  /**
   * 执行Statement更改语句
   * @param sql
   * @throws SQLException
   */
  public void executeUpdate(String sql) throws SQLException {
    if (stmt != null)
      stmt.executeUpdate(sql);
  }

  /**
   * 执行PreparedStatement更改语句
   * @throws SQLException
   */
  public void executeUpdate() throws SQLException {
    if (prepstmt != null)
      prepstmt.executeUpdate();
  }

  /**
   * 关闭连接
   */
  public void close() {
    try {
      if (stmt != null) {
        stmt.close();
        stmt = null;
      }
      if (prepstmt != null) {
        prepstmt.close();
        prepstmt = null;
      }
      conn.close();
      conn = null;
    } catch (Exception e) {
      System.err.println("Mysql close error: " + e);
    }

  }
}

你可能感兴趣的:(JDBC通用操作类)