KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ziclix > python > sql > connect > Connect


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: Connect.java,v 1.4 2005/02/23 04:26:19 bzimmer Exp $
5  *
6  * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
7  *
8  */

9 package com.ziclix.python.sql.connect;
10
11 import java.sql.*;
12 import java.util.*;
13
14 import org.python.core.*;
15 import com.ziclix.python.sql.*;
16 import com.ziclix.python.sql.util.*;
17
18 /**
19  * Connect using DriverManager.
20  *
21  * @author brian zimmer
22  * @author last revised by $Author: bzimmer $
23  * @version $Revision: 1.4 $
24  */

25 public class Connect extends PyObject {
26
27     private static final PyString _doc = new PyString("establish a connection through java.sql.DriverManager");
28
29     /**
30      * Default empty constructor.
31      */

32     public Connect() {
33     }
34
35     /**
36      * Method __findattr__
37      *
38      * @param String name
39      * @return PyObject
40      */

41     public PyObject __findattr__(String JavaDoc name) {
42
43         if ("__doc__".equals(name)) {
44             return _doc;
45         }
46
47         return super.__findattr__(name);
48     }
49
50     /**
51      * Establish a connection through DriverManager.
52      */

53     public PyObject __call__(PyObject[] args, String JavaDoc[] keywords) {
54
55         Connection c = null;
56         PyArgParser parser = new PyArgParser(args, keywords);
57         Object JavaDoc arg = parser.arg(0).__tojava__(Connection.class);
58
59         if (arg == Py.NoConversion) {
60             Properties props = new Properties();
61             String JavaDoc url = (String JavaDoc) parser.arg(0).__tojava__(String JavaDoc.class);
62             String JavaDoc user = (String JavaDoc) parser.arg(1).__tojava__(String JavaDoc.class);
63             String JavaDoc password = (String JavaDoc) parser.arg(2).__tojava__(String JavaDoc.class);
64             String JavaDoc driver = (String JavaDoc) parser.arg(3).__tojava__(String JavaDoc.class);
65
66             if (url == null) {
67                 throw zxJDBC.makeException(zxJDBC.DatabaseError, "no url specified");
68             }
69
70             if (driver == null) {
71                 throw zxJDBC.makeException(zxJDBC.DatabaseError, "no driver specified");
72             }
73
74             // the value can't be null
75
props.put("user", (user == null) ? "" : user);
76             props.put("password", (password == null) ? "" : password);
77
78             String JavaDoc[] kws = parser.kws();
79
80             for (int i = 0; i < kws.length; i++) {
81                 Object JavaDoc value = parser.kw(kws[i]).__tojava__(Object JavaDoc.class);
82
83                 props.put(kws[i], value);
84             }
85
86             try {
87                 Class.forName(driver);
88             } catch (Throwable JavaDoc e) {
89                 throw zxJDBC.makeException(zxJDBC.DatabaseError, "driver [" + driver + "] not found");
90             }
91
92             try {
93                 c = DriverManager.getConnection(url, props);
94             } catch (SQLException e) {
95                 throw zxJDBC.makeException(zxJDBC.DatabaseError, e);
96             }
97         } else {
98             c = (Connection) arg;
99         }
100
101         try {
102             if ((c == null) || c.isClosed()) {
103                 throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to establish connection");
104             }
105
106             return new PyConnection(c);
107         } catch (SQLException e) {
108             throw zxJDBC.makeException(zxJDBC.DatabaseError, e);
109         }
110     }
111
112     /**
113      * Method toString
114      *
115      * @return String
116      */

117     public String JavaDoc toString() {
118         return "<connect object instance at " + Py.id(this) + ">";
119     }
120
121     // __class__ boilerplate -- see PyObject for details
122

123     /**
124      * Field __class__
125      */

126     public static PyClass __class__;
127
128     /**
129      * Method getPyClass
130      *
131      * @return PyClass
132      */

133     protected PyClass getPyClass() {
134         return __class__;
135     }
136 }
137
Popular Tags