001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002// The Molly framework is freely distributable under the terms of an
003// MIT-style license. For details, see the molly pages web site at:
004// http://www.mollypages.org/. Use, modify, have fun !
005
006package fc.jdbc;
007
008import java.io.*;
009import java.util.*;
010import java.util.logging.*;
011import java.sql.*;
012
013import fc.io.*;
014import fc.util.*;
015
016/** 
017A simple connection manager that returns a new connection to the
018database every time.
019
020@author hursh jain
021*/
022public class SimpleConnectionMgr extends ConnectionMgr
023{
024
025/**
026{@see ConnectionMgr} 
027**/
028public SimpleConnectionMgr(
029    String jdbc_url, String jdbc_driver, 
030    String jdbc_user, String jdbc_password, 
031    String jdbc_catalog)  
032throws Exception
033  {
034  super(jdbc_url, jdbc_driver, jdbc_user, jdbc_password, jdbc_catalog);
035  }
036
037
038/** {@see ConnectionMgr} **/
039public SimpleConnectionMgr(PropertyMgr props) 
040throws Exception
041  {
042  super(props);
043  }
044
045/** {@see ConnectionMgr} **/
046public SimpleConnectionMgr(PropertyMgr props, String prefix) 
047throws Exception
048  {
049  super(props, prefix);
050  }
051
052/** {@see ConnectionMgr} **/
053public SimpleConnectionMgr(SystemLog log, PropertyMgr props) 
054throws Exception
055  {
056  super(log, props);
057  }
058
059protected boolean handleMgrShutdown()
060  {
061  //nothing to do
062  return true;
063  }
064  
065/**
066Returns a new connection to the DB everytime it's called.
067**/ 
068protected Connection getConImpl() throws SQLException
069  {
070  Connection con = null;
071  log.bug("enter");
072  if (log.getLevel() == Log.DEBUG) {
073    DriverManager.setLogStream(System.err);
074    }
075  try {
076    con = DriverManager.getConnection(url,user,password);
077    log.bug("getOneImpl(), got connection: " + con);  
078    }
079  catch (SQLException e) {  
080    log.warn("getOneConnection(): Could not get a database connection.", e);
081    e.fillInStackTrace();
082    throw e;
083    }
084  return con;
085  } //~ connect()
086    
087/** 
088Unit Test History   
089<pre>
090Tester    Status      Notes       
091hj      passed    
092</pre>
093**/ 
094public static void main(String[] args) throws Exception
095  {
096  Args myargs = new Args(args);
097  myargs.setUsage("java fc.jdbc.SimpleConnectionMgr -conf <path-to-config-file> [-query query-to-execute]");
098  
099  String query    = myargs.get("query", null);
100  String propfile = myargs.getRequired("conf");
101  
102  try {
103    ConnectionMgr mgr = new SimpleConnectionMgr(  
104                  new FilePropertyMgr(
105                    new File(propfile)));
106    
107    Connection con = mgr.getConnection();
108    System.out.println("GOT CONNECTION:" + con);
109
110    if (query != null)
111      {
112      Statement stmt = con.createStatement();
113      ResultSet rs = stmt.executeQuery(query);
114      System.out.println("======= query using connection ===");
115      QueryUtil.printRS(rs);
116      System.out.println("==================================");
117      stmt.close();   
118      }
119    System.out.println("The following should give an error");
120    mgr.close();
121    mgr.getConnection();
122    }
123  catch (Exception e) {
124    e.printStackTrace();
125    } 
126  }
127  
128}  //~class JDBCConnection
129
130