package examples.intro;

import java.sql.*;
import java.util.*;
import javax.naming.*;

/**
 *
 * @author Copyright (c) 2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class booksPool {

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

    Context ctx = null;
    Hashtable ht = new Hashtable();
    java.sql.Connection conn = null;
    ht.put(Context.INITIAL_CONTEXT_FACTORY,
           "weblogic.jndi.WLInitialContextFactory");
    ht.put(Context.PROVIDER_URL,
           "t3://localhost:7001");
          
    try {
      ctx = new InitialContext(ht);
      javax.sql.DataSource ds = 
        (javax.sql.DataSource) ctx.lookup("demoDataSource");
      conn = ds.getConnection();
      Statement s = conn.createStatement();

      try {
        // First, drop the "books" table, in case it 
        // already exists. If it does not, then this 
        // statement will get an warning that can be ignored.
        System.out.println("Dropping table books.");
        s.execute("drop table books");
      }
      catch ( SQLException se ) {;}
      

      // Now, create the "books" table and insert some data.
      System.out.println("Creating table books.");
      s.execute("create table books (title varchar(40), " +
                "author varchar(40), publisher varchar(40),"+
                "isbn varchar(20))");

      // Use a PreparedStatement to insert rows in the table.
      System.out.println("Loading table.");
      PreparedStatement ps = conn.prepareStatement(
                 "insert into books " +
                 "(title,author,publisher,isbn) " +
                 " values (?, ?, ?, ?)");

      ps.setString(1, "Java in a Nutshell");
      ps.setString(2, "David Flanagan");
      ps.setString(3, "O'Reilly");
      ps.setString(4, "1-56592-262-X");
      ps.execute();

      ps.setString(1, "Java Examples in a Nutshell");
      //      ps.setString(2, "David Flanagan");
      //      ps.setString(3, "O'Reilly");
      ps.setString(4, "1-56592-371-5");
      ps.execute();
      
      ps.setString(1, "Design Patterns");
      ps.setString(2, "Gamma, Helm, Johnson, Vlissides");
      ps.setString(3, "Addison-Wesley");
      ps.setString(4, "0-201-63361-2");
      ps.execute();
      
      System.out.println("Retrieving rows.");
      s.execute("select * from books");

      ResultSet rs = s.getResultSet();
      while (rs.next()) {
        System.out.println(rs.getString("title") + "\n\t" + 
                           rs.getString("author") + "\n\t" +
                           rs.getString("publisher") + "\n\t" +
                           rs.getString("isbn"));
      }
      rs.close();
      s.close();
      ps.close();
    } finally {
      try {conn.close();} catch (Exception e) {;}
      try {ctx.close();}  catch (Exception e) {;}
    }
  }
}



