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

📄 connectionpooling.java

📁 主要为一个空间信息管理系统
💻 JAVA
字号:
/*
 * ConnectionPooling.java
 *
 */

package edu.whu.database;

import java.io.InputStream;
import java.io.IOException;
import java.io.FileInputStream;

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

public class ConnectionPooling
{
  private static ConnectionPooling instance = null;
  private static Connection conn = null;
  private static String HOME_DIRECTORY = "";

  /**
   * Establish the connection to database.
   * All of the parameter comes from the configure file
   * named as the db.properties placed in the initial
   * directory.  //synchronized delete by dengshijun
   * 2003-06-12
   */
  public static Connection getConnection()
  {
    if (instance == null)
    {
      instance = new ConnectionPooling();
    }
    if ( (conn == null) || isClosed())
    {
      return instance._getConnection();
    }
    else
    {
      return conn;
    }
  }

  public static void setHomeDirectory(String home)
  {
    HOME_DIRECTORY = home;
  }

  private ConnectionPooling()
  {
    super();
  }

  private static boolean isClosed()
  {
    try
    {
      if (conn == null)
        return true;
      else
        return conn.isClosed();
    }
    catch (SQLException e)
    {
      return true;
    }
  }

  private synchronized Connection _getConnection()
  {
    try
    {
      String sDBDriver = null;
      String sConnection = null;
      String sUser = null;
      String sPassword = null;
      Properties p = new Properties();
      InputStream is = new FileInputStream("c:\\conf\\database.properties");
      //getClass().getResourceAsStream("resources/db.properties");
      p.load(is);
      sDBDriver = p.getProperty("DBDriver", sDBDriver);
      sConnection = p.getProperty("Connection", sConnection);
      sUser = p.getProperty("User", "");
      sPassword = p.getProperty("Password", "");
      Properties pr = new Properties();
      pr.put("user", sUser);
      pr.put("password", sPassword);
      pr.put("characterEncoding", "GB2312");
      pr.put("useUnicode", "TRUE");
      Class.forName(sDBDriver).newInstance();
      conn = DriverManager.getConnection(sConnection, pr);
      return conn;
    }
    catch (ClassNotFoundException e)
    {
      e.printStackTrace();
      return null;
    }
    catch (InstantiationException e)
    {
      e.printStackTrace();
      return null;
    }
    catch (IllegalAccessException e)
    {
      e.printStackTrace();
      return null;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      return null;
    }
    catch (IOException e)
    {
      e.printStackTrace();
      return null;
    }
  }

  /**
   * Execute Query, this method will return the result
   * from the query.
   * @param sql is the SQL String such as "SELECT * FROM <table name> <condition clause>".
   */
  public static ResultSet executeQuery(String sql)
  {
    ResultSet rs = null;
    try
    {
      rs = getConnection().createStatement().executeQuery(sql);
    }
    catch (SQLException e)
    {
      //e.printStackTrace();
    }
    return rs;
  }

  /**
   * Execute update, this method will update the database.
   * @param sql is the SQL String such as "UPDATE <table name>
   * SET <column name>=<value> [<column name>=<value>...]
   * <condition clause>".
   */
  public static synchronized boolean executeUpdate(String sql) throws
      IOException
  {
    try
    {
      getConnection().createStatement().executeUpdate(sql);
      return true;
    }
    catch (SQLException e)
    {
      //e.printStackTrace();
      //throw new IOException(e.toString());
      return false;
    }
  }

  /**
   * Close the instance and the Connection.
   */
  public static void close()
  {
    if (conn != null)
    {
      try
      {
        conn.close();
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    if (instance != null)
    {
      instance = null;
    }
  }
}

⌨️ 快捷键说明

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