package examples.jdbc.datasource;
import java.sql.*;
import java.util.*;
import javax.naming.*;

/**
 * This example demonstrates obtaining a database connection using a <b>DataSource</b> object.
 * <p> This examples requires a running WebLogic Server and that you add the following entries to the weblogic.properties file.  
 * <p>To run this example:
 * <ol>
 * <li>Set up your development shell as described in <a
 * href=../../../techstart/environment.html>Setting up your environment</a>.<p>
 * <li> Add a DataSource object to the weblogic.properties file, which points to the <b>myConnectionPool</b> connection pool:
 * <pre> weblogic.jdbc.DataSource.myNonJtsDataSource=myConnectionPool</pre>
 * <li>Next, define a connection pool in the 
 * weblogic.properties file. If you already have a pool configured to access your DBMS, change the "mypool" portion of the above entry to point to your connection pool. If you do not have a connection pool defined, modify the properties below by changing the url, driver and  props line with the correct connection strings for connection to your DataBase. The example below uses an Oracle database.  
 * <pre> weblogic.jdbc.connectionPool.myConnectionPool=\
 *   url=jdbc:weblogic:oracle,\
 *   driver=weblogic.jdbc.oci.Driver,\
 *   loginDelaySecs=1,\
 *   initialCapacity=4,\
 *   maxCapacity=10,\
 *   capacityIncrement=2,\
 *   allowShrinking=true,\
 *   shrinkPeriodMins=15,\
 *   refreshMinutes=10,\
 *   testTable=dual,\
 *   allow=guest,\
 *   props=user=SCOTT;password=tiger;server=DEMO</pre>
 *  <p>
 * Note that the line "allow=guest" permits guest access to the connection pool. 
 * In a production environment, you should change this to match your security needs
 *
 * <li> Change the line    <pre>  ht.put(Context.PROVIDER_URL,
             "t3://localhost:7001");</pre>
 *
 * substituting the name of the machine hosting your WebLogic Server for <i>localhost</i>
 *  and the port number at which the WebLogic Server is listening for connection requests for <i>7001</i>
 * <p>
 *
 * <li> Start your WebLogic Server<p>
 *
 *<li>Compile this example by executing the following command in your development shell:
 * 
*<p>
 *
 *<font face="Courier New"><b>$ javac -d %CLIENT_CLASSES% simplesql.java</b></font>
 *<p>	
 *
 *<li>Run this example by executing the following command in your development shell:
 *
 *<p>
 *
 *<font face="Courier New"><b>$ java examples.jdbc.oracle.datasource.simplesql</b></font>
 *
 *</ol> *
 * @author Copyright (c) 2000 by BEA Systems, Inc.  All Rights Reserved.
 */



public class simplesql {
  
  public static void main(String argv[])
       throws Exception
  {

      // ============== Make connection to database ==================
      // Obtain a Datasource connection from the WebLogic JNDI tree.
      Context ctx = null;
     
      // Put connection properties in to a hashtable. 
      Hashtable ht = new Hashtable();
      ht.put(Context.INITIAL_CONTEXT_FACTORY,
             "weblogic.jndi.WLInitialContextFactory");
      ht.put(Context.PROVIDER_URL,
             "t3://localhost:7001");

      // Get a context for the JNDI look up
      ctx = new InitialContext(ht);
      javax.sql.DataSource ds
          = (javax.sql.DataSource) ctx.lookup ("myNonJtsDataSource");
      java.sql.Connection conn = ds.getConnection();
      System.out.println("Making connection...\n");
      
      // execute some SQL statements to demonstrate the connection. 
    Statement stmt = conn.createStatement();
    
    try {
      stmt.execute("drop table empdemo");
      System.out.println("Table empdemo dropped.");
    }
    catch (SQLException e) {
      System.out.println("Table empdemo doesn't need to be dropped.");
    }
    
    stmt.execute("create table empdemo (empid integer, name varchar2(30), dept number(4))");
    System.out.println("Table empdemo created.");
    
    int numrows = stmt.executeUpdate("insert into empdemo values (0, 'John Smith', 12)");
    System.out.println("Number of rows inserted = " + numrows);
    
    numrows = stmt.executeUpdate("delete from empdemo where empid = 0");
    System.out.println("Number of rows deleted = " + numrows);
    
    stmt.close();
    conn.close();
  }
    
}





