KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > piratepete > util > db > DBWorkerFactory


1 package com.piratepete.util.db;
2
3
4 /**
5  * DBWorkerFactory Class
6  * Copyright (C) 2003 David L. Whitehurst<p>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.<p>
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.<p>
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.<p>
21  *
22  * <a HREF="http://www.piratepetesoftware.com">piratepetesoftware.com</a><br>
23  * <a HREF="mailto:dlwhitehurst@comcast.net">dlwhitehurst@comcast.net</a>
24  *
25  * @version 1.0a
26  */

27 public class DBWorkerFactory {
28     
29     public DBWorkerFactory() {} // constructor
30

31     private String JavaDoc getURL(String JavaDoc raw, String JavaDoc h, String JavaDoc u, String JavaDoc p, String JavaDoc s) {
32         //String start = "jdbc:oracle:thin@#host#:1521:#sid#:#user#:#pass#";
33

34         String JavaDoc a = raw.replaceAll("#host#", h);
35         String JavaDoc b = a.replaceAll("#user#", u);
36         String JavaDoc c = b.replaceAll("#pass#", p);
37         String JavaDoc d = c.replaceAll("#sid#", s);
38         String JavaDoc e = d.replace('^', '&');
39         
40         return e;
41         
42     }
43     
44     public DBWorker createConnection(String JavaDoc user, String JavaDoc password, String JavaDoc hostname, String JavaDoc sid, DBConnectionType dbtype) {
45         DBWorker dbWorker = null;
46         
47         // fix URL
48
String JavaDoc dburl = getURL(dbtype.getURL(), hostname, user, password, sid);
49         
50         // Create
51
dbWorker = new DBWorker();
52         
53         // Connect
54
dbWorker.connect(dbtype.getClassName(), dburl);
55         
56         // Check success
57
if (dbWorker.isConnected()) {
58             return dbWorker;
59         } else {
60             return null;
61         }
62     }
63 }
64
Popular Tags