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
006 package fc.jdbc;
007
008 import java.io.*;
009 import java.util.*;
010 import java.util.logging.*;
011 import java.sql.*;
012
013 import fc.io.*;
014 import fc.util.*;
015
016 /**
017 A simple connection manager that returns a new connection to the
018 database every time.
019
020 @author hursh jain
021 */
022 public class SimpleConnectionMgr extends ConnectionMgr
023 {
024
025 /**
026 {@see ConnectionMgr}
027 **/
028 public SimpleConnectionMgr(
029 String jdbc_url, String jdbc_driver,
030 String jdbc_user, String jdbc_password,
031 String jdbc_catalog)
032 throws Exception
033 {
034 super(jdbc_url, jdbc_driver, jdbc_user, jdbc_password, jdbc_catalog);
035 }
036
037
038 /** {@see ConnectionMgr} **/
039 public SimpleConnectionMgr(PropertyMgr props)
040 throws Exception
041 {
042 super(props);
043 }
044
045 /** {@see ConnectionMgr} **/
046 public SimpleConnectionMgr(PropertyMgr props, String prefix)
047 throws Exception
048 {
049 super(props, prefix);
050 }
051
052 /** {@see ConnectionMgr} **/
053 public SimpleConnectionMgr(SystemLog log, PropertyMgr props)
054 throws Exception
055 {
056 super(log, props);
057 }
058
059 protected boolean handleMgrShutdown()
060 {
061 //nothing to do
062 return true;
063 }
064
065 /**
066 Returns a new connection to the DB everytime it's called.
067 **/
068 protected 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 /**
088 Unit Test History
089 <pre>
090 Tester Status Notes
091 hj passed
092 </pre>
093 **/
094 public 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