KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > db > DBProc


1 package org.ashkelon.db;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.DriverManager JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.sql.Statement JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import org.ashkelon.util.Logger;
12
13 /**
14  * @author Eitan Suez
15  */

16 public class DBProc
17 {
18    static Map JavaDoc scriptMap = null;
19    private Logger log;
20    
21    public DBProc()
22    {
23       if (scriptMap == null)
24       {
25          scriptMap = new HashMap JavaDoc();
26          scriptMap.put("reset", "org/ashkelon/db/ashkelon.sql");
27          scriptMap.put("reset_seqs", "org/ashkelon/db/init.sql");
28          scriptMap.put("add_idx", "org/ashkelon/db/ashkelon_addidx.sql");
29          scriptMap.put("del_idx", "org/ashkelon/db/ashkelon_dropidx.sql");
30       }
31       log = Logger.getInstance();
32       //log.setPrefix("DBProc");
33
//log.setTraceLevel(Logger.DEBUG);
34
}
35    
36    // for 8i lite, signature must be:
37
//public static void entryPoint(Connection conn, String action)
38
// for 8i standard/enterprise, signature must be:
39
public static void entryPoint(String JavaDoc action)
40    {
41       DBProc proc = new DBProc();
42       String JavaDoc script = (String JavaDoc) scriptMap.get(action);
43       proc.log.debug("Script is: "+script);
44       if (script == null) return;
45       boolean warn = (action.indexOf("idx") > -1) ? false : true;
46       List JavaDoc commands = ScriptParser.parse(script, warn);
47       proc.log.debug("Commands length: "+commands.size());
48
49       try
50       {
51          // this is how to get ref to conn using 8i standard/enterprise:
52
Connection JavaDoc conn = DriverManager.getConnection("jdbc:default:connection:");
53          
54          Statement JavaDoc stmt = conn.createStatement();
55          String JavaDoc command = "";
56          for (int i=0; i<commands.size(); i++)
57          {
58             command = (String JavaDoc) commands.get(i);
59             proc.log.debug("Cmd: "+command);
60             try { // desired behavior: if one statement fails, don't block rest
61
stmt.execute(command);
62             } catch (SQLException JavaDoc ex)
63             {
64                proc.log.error("Cmd Failed: "+command);
65             }
66          }
67          stmt.close();
68       } catch (SQLException JavaDoc ex)
69       {
70          proc.log.error("Failed: "+ex.getMessage());
71          DBUtils.logSQLException(ex);
72       }
73    }
74    
75    public void doAction(Connection JavaDoc conn, String JavaDoc action) throws SQLException JavaDoc
76    {
77       String JavaDoc script = (String JavaDoc) scriptMap.get(action);
78       if (script == null)
79       {
80          log.brief("cannot fulfill request to "+action);
81          return;
82       }
83       // script = StringUtils.substitute(script, "\\$dbtype", DBMgr.getInstance().getDbtype());
84
boolean warn = (action.indexOf("idx") > -1) ? false : true;
85       List JavaDoc commands = ScriptParser.parse(script, warn);
86       DBUtils.submitBatch(conn, commands);
87    }
88    
89    public static void main(String JavaDoc args[]) throws SQLException JavaDoc
90    {
91       //DBMgr mgr = DBMgr.getInstance();
92
//Connection conn = mgr.getConnection();
93
if (args.length == 0)
94       {
95          Logger.getInstance().traceln("Must provide an action");
96          return;
97       }
98       //entryPoint(conn, args[0]);
99
entryPoint(args[0]);
100       //mgr.releaseConnection(conn);
101
}
102    
103 }
104
Popular Tags