示例#1
0
  public static void main(String[] argv) {
    try {

      String dmds_name = null;
      String cpds_name = null;
      String pbds_name = null;

      if (argv.length == 3) {
        dmds_name = argv[0];
        cpds_name = argv[1];
        pbds_name = argv[2];
      } else usage();

      InitialContext ctx = new InitialContext();
      DataSource dmds = (DataSource) ctx.lookup(dmds_name);
      dmds.getConnection().close();
      System.out.println(
          "DriverManagerDataSource " + dmds_name + " sucessfully looked up and checked.");
      ConnectionPoolDataSource cpds = (ConnectionPoolDataSource) ctx.lookup(cpds_name);
      cpds.getPooledConnection().close();
      System.out.println(
          "ConnectionPoolDataSource " + cpds_name + " sucessfully looked up and checked.");
      DataSource pbds = (DataSource) ctx.lookup(pbds_name);
      pbds.getConnection().close();
      System.out.println(
          "PoolBackedDataSource " + pbds_name + " sucessfully looked up and checked.");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public Connection getConnection() throws Exception {
    InitialContext ctx = null;
    ctx = new InitialContext();

    if (getDataSourceJndiName() == null)
      throw (new Exception("Data Source JNDI name is null. Check whether the JNDI name is null."));

    DataSource ds = (javax.sql.DataSource) ctx.lookup(getDataSourceJndiName());
    Connection conn = ds.getConnection();

    return conn;
  }
示例#3
0
  /**
   * Constructor encargado de iniciar el contexto y el objeto DataSource.
   *
   * @param dataSourceName Nombre del DataSource que se empleará
   * @throws Exception La excepción que puede arrojarse se debe a que no se cree el objeto
   *     DataSource.
   */
  public DbConnection(String dataSourceName) throws Exception {
    try {
      this.dataSourceName = dataSourceName;
      InitialContext iCtx = new InitialContext();
      this.dataSource = (javax.sql.DataSource) iCtx.lookup(dataSourceName);
    } catch (Exception e) {
      // System.out.print("Error en constructor de DBConnection: "+e.getMessage());

      StringWriter sw = new StringWriter();
      e.printStackTrace(new PrintWriter(sw));
      String msg = sw.toString();
      // System.out.print("Error en constructor de DBConnection: " + msg);

      e.printStackTrace();
    }
  }
示例#4
0
  public ResultSet getPrefixName() throws SQLException, NamingException {

    String sql_PrefixName = "SELECT prefix_id,prefixname,abbreviation FROM hex.ref_prefixname";

    ctx = new InitialContext();
    ds = (DataSource) ctx.lookup("jdbc/HEX");
    conn = ds.getConnection();
    pstmt = conn.prepareStatement(sql_PrefixName);
    rs = pstmt.executeQuery();

    return rs;
  }
示例#5
0
 public void closeConnection() throws SQLException, NamingException {
   ctx.close();
   pstmt.close();
   rs.close();
   conn.close();
 }
示例#6
0
 public static Connection getConnect() throws SQLException, NamingException {
   InitialContext ic = new InitialContext();
   DataSource ds = (DataSource) ic.lookup("java:/comp/env/jdbc/chatdb");
   return ds.getConnection();
 }