Refine search
DataSource dataSource = null; try { Context context = new InitialContext(); dataSource = (DataSource) context.lookup("Database"); } catch (NamingException e) { // Couldn't find the data source: give up }
public static void destroyTable(String tableName) throws NamingException, SQLException { Context ctx = cache.getJNDIContext(); DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource"); Connection conn = ds.getConnection(); // System.out.println (" trying to drop table: " + tableName); String sql = "drop table " + tableName; Statement sm = conn.createStatement(); sm.execute(sql); conn.close(); }
ctx = (props != null) ? new InitialContext(props): new InitialContext(); ds = ctx.lookup(url); if (!isAlwaysLookup()) { this.datasource = ds; return (((XADataSource) ds).getXAConnection().getConnection()); } else if (ds instanceof DataSource) { return ((DataSource) ds).getConnection(); } else { throw new SQLException("Object at JNDI URL '" + url + "' is not a DataSource.");
Context context = new InitialContext(); DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myDB");
ctx = (props != null) ? new InitialContext(props): new InitialContext(); ds = ctx.lookup(url); if (!isAlwaysLookup()) { this.datasource = ds; return (((XADataSource) ds).getXAConnection().getConnection()); } else if (ds instanceof DataSource) { return ((DataSource) ds).getConnection(); } else { throw new SQLException("Object at JNDI URL '" + url + "' is not a DataSource.");
public static void listTableData(String tableName) throws NamingException, SQLException { Context ctx = cache.getJNDIContext(); DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource"); String sql = "select * from " + tableName; Connection conn = ds.getConnection(); Statement sm = conn.createStatement(); ResultSet rs = sm.executeQuery(sql); while (rs.next()) { System.out.println("id " + rs.getString(1) + " name " + rs.getString(2)); } rs.close(); conn.close(); }
@Bean public DataSource dataSource() throws Exception { Context ctx = new InitialContext(); return (DataSource) ctx.lookup("java:jboss/datasources/mySQLDB"); }
// Obtain our environment naming context Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); // Look up our data source DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB"); // Allocate and use a connection from the pool Connection conn = ds.getConnection();
/** * This method is used to return number rows from the timestamped table created by createTable() * in CacheUtils class. */ public int getRows(String tableName) throws NamingException, SQLException { Context ctx = cache.getJNDIContext(); DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource"); String sql = "select * from " + tableName; Connection conn = ds.getConnection(); Statement sm = conn.createStatement(); ResultSet rs = sm.executeQuery(sql); int counter = 0; while (rs.next()) { counter++; // System.out.println("id "+rs.getString(1)+ " name "+rs.getString(2)); } rs.close(); conn.close(); return counter; }
@Bean(destroyMethod = "") public DataSource dataSource() throws NamingException{ Context context = new InitialContext(); return (DataSource)context.lookup("jdbc.mydatasource"); }
@Override public Connection get() { try { Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup(jndiResource); Connection conn = ds.getConnection(); return conn; } catch (SQLException e) { throw new SQLRuntimeException(e); } catch (NamingException e) { throw new RuntimeException(e); } }
public static void createTable(String tableName) throws NamingException, SQLException { Context ctx = cache.getJNDIContext(); DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource"); // String sql = "create table " + tableName + " (id number primary key, name varchar2(50))"; // String sql = "create table " + tableName + " (id integer primary key, name varchar(50))"; String sql = "create table " + tableName + " (id integer NOT NULL, name varchar(50), CONSTRAINT the_key PRIMARY KEY(id))"; System.out.println(sql); Connection conn = ds.getConnection(); Statement sm = conn.createStatement(); sm.execute(sql); sm.close(); sm = conn.createStatement(); for (int i = 1; i <= 100; i++) { sql = "insert into " + tableName + " values (" + i + ",'name" + i + "')"; sm.addBatch(sql); System.out.println(sql); } sm.executeBatch(); conn.close(); }
Context initialContext = new InitialContext(); String myvar = (String) initialContext.lookup("java:comp/env/myvar");
/** * Get a configured database connection via JNDI. */ public static Statement open(String jndiPath) { try { Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup(jndiPath); Connection conn = ds.getConnection(); return conn.createStatement(); } catch (NamingException e) { throw new DukeException("No database configuration found via JNDI at " + jndiPath, e); } catch (SQLException e) { throw new DukeException("Error connecting to database via " + jndiPath, e); } }
/** * This method is used to delete all rows from the timestamped table created by createTable() in * CacheUtils class. */ public int deleteRows(String tableName) throws NamingException, SQLException { Context ctx = cache.getJNDIContext(); DataSource da = (DataSource) ctx.lookup("java:/SimpleDataSource"); // doesn't req txn Connection conn = da.getConnection(); Statement stmt = conn.createStatement(); int rowsDeleted = 0; // assume that rows are always inserted in CacheUtils String sql = ""; sql = "select * from " + tableName; ResultSet rs = stmt.executeQuery(sql); if (rs.next()) { sql = "delete from " + tableName; rowsDeleted = stmt.executeUpdate(sql); } rs.close(); stmt.close(); conn.close(); return rowsDeleted; }
import javax.naming.Context; import javax.naming.InitialContext; ... // Get the base naming context Context env = (Context)new InitialContext().lookup("java:comp/env"); // Get a single value String dbhost = (String)env.lookup("dbhost");
/** * Opens a connection from JNDI based on a registered name. This assumes that there is a <code>jndi.properties</code> * file with proper JNDI configuration in it. * * @param jndiName name of a configured data source. */ public DB open(String jndiName) { checkExistingConnection(name); try { Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup(jndiName); Connection connection = ds.getConnection(); LogFilter.log(LOGGER, LogLevel.DEBUG, "Opened connection: " + connection); ConnectionsAccess.attach(name, connection, jndiName); return this; } catch (Exception e) { throw new InitException("Failed to connect to JNDI name: " + jndiName, e); } }
/** * This method is used to search for pattern which is the PK in the timestamped table created by * createTable() in CacheUtils class. */ public boolean checkTableAgainstData(String tableName, String pattern) throws NamingException, SQLException { Context ctx = cache.getJNDIContext(); DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource"); boolean found = false; String id_str = ""; String sql = "select * from " + tableName; Connection conn = ds.getConnection(); Statement sm = conn.createStatement(); ResultSet rs = sm.executeQuery(sql); while (rs.next()) { System.out.println("id:" + rs.getString(1)); System.out.println("name:" + rs.getString(2)); id_str = rs.getString(1); if (id_str.equals(pattern)) { found = true; break; } else continue; } rs.close(); conn.close(); return found; }
Hashtable env = new Hashtable(5); env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); //Assuming weblogic server is running on localhost at port 7001 env.put(Context.PROVIDER_URL, "t3://localhost:7001"); Context ic = new InitialContext(env); //obtain a reference to the home or local home interface FooHome fooHome = (FooHome)ic.lookup("MyBeans/FooHome"); //Get a reference to an object that implements the beans remote (component) interface Foo foo = fooHome.create(); //call the service exposed by the bean foo.shoutFoo()
/** * Opens a new connection from JNDI data source by name using explicit JNDI properties. This method can be used in cases * when file <code>jndi.properties</code> cannot be easily updated. * * @param jndiName name of JNDI data source. * @param jndiProperties JNDI properties */ public DB open(String jndiName, Properties jndiProperties) { checkExistingConnection(name); try { Context ctx = new InitialContext(jndiProperties); DataSource ds = (DataSource) ctx.lookup(jndiName); Connection connection = ds.getConnection(); LogFilter.log(LOGGER, LogLevel.DEBUG, "Opened connection: " + connection); ConnectionsAccess.attach(name, connection, jndiProperties.contains("url") ? jndiProperties.getProperty("url") : jndiName); return this; } catch (Exception e) { throw new InitException("Failed to connect to JNDI name: " + jndiName, e); } }