Advertisement
uopspop

Untitled

Oct 6th, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class SQLAgent {
  4.     private ConnPool connPool;
  5.     private Connection conn;
  6.     private String driverName;
  7.     private String jdbcURL;
  8.     private String username;
  9.     private String password;
  10.  
  11.     public void setConnPool(ConnPool pool) {
  12.         connPool = pool;
  13.     }
  14.  
  15.     public void setDriverName(String drvName) {
  16.         driverName = drvName;
  17.     }
  18.  
  19.     public void setJdbcURL(String url) {
  20.         jdbcURL = url;
  21.     }
  22.  
  23.     public void setUserName(String uname) {
  24.         username = uname;
  25.     }
  26.  
  27.     public void setPassword(String passwd) {
  28.         password = passwd;
  29.     }
  30.  
  31.     public Connection getConnection() {
  32.         return conn;
  33.     }
  34.  
  35.     public void setConnectionSwitch(String on_off) throws SQLException {
  36.         if (on_off.equalsIgnoreCase("ON")) {
  37.             if (connPool != null)
  38.                 openDB(connPool);
  39.             else
  40.                 openDB(driverName, jdbcURL, username, password);
  41.         } else if (on_off.equalsIgnoreCase("OFF"))
  42.             closeDB();
  43.     }
  44.  
  45.     public void openDB(ConnPool pool) throws SQLException {
  46.         if (conn != null && !conn.isClosed())
  47.             throw new SQLException(
  48.                     "The connection has been established already.");
  49.         if (pool == null)
  50.             throw new SQLException("The connection pool cannot be found.");
  51.         connPool = pool;
  52.         conn = connPool.getConnection();
  53.     }
  54.  
  55.     public void openDB(String drvName, String url, String uname, String passwd)
  56.             throws SQLException {
  57.         if (conn != null && !conn.isClosed())
  58.             throw new SQLException(
  59.                     "The connection has been established already.");
  60.         try {
  61.             Class.forName(drvName);
  62.         } catch (ClassNotFoundException ex) {
  63.             throw new SQLException(ex.toString());
  64.         }
  65.         conn = DriverManager.getConnection(url, uname, passwd);
  66.     }
  67.  
  68.     public void closeDB() throws SQLException {
  69.         if (connPool != null) {
  70.             connPool.returnConnection();
  71.             connPool = null;
  72.         } else {
  73.             if (conn == null)
  74.                 throw new SQLException("This connection is null.");
  75.             if (conn.isClosed())
  76.                 throw new SQLException(
  77.                         "This connection has been close already..");
  78.             conn.close();
  79.         }
  80.         conn = null;
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement