-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathConnect.java
More file actions
110 lines (90 loc) · 3.04 KB
/
Connect.java
File metadata and controls
110 lines (90 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Jython Database Specification API 2.0
*
*
* Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
*
*/
package com.ziclix.python.sql.connect;
import java.sql.*;
import java.util.*;
import org.python.core.*;
import com.ziclix.python.sql.*;
import com.ziclix.python.sql.util.*;
/**
* Connect using DriverManager.
*
* @author brian zimmer
*/
@Untraversable
public class Connect extends PyObject {
private static final PyString _doc = new PyString("establish a connection through java.sql.DriverManager");
/**
* Default empty constructor.
*/
public Connect() {
}
public PyObject __findattr_ex__(String name) {
if ("__doc__".equals(name)) {
return _doc;
}
return super.__findattr_ex__(name);
}
/**
* Establish a connection through DriverManager.
*/
public PyObject __call__(PyObject[] args, String[] keywords) {
Connection c = null;
PyArgParser parser = new PyArgParser(args, keywords);
Object arg = parser.arg(0).__tojava__(Connection.class);
if (arg == Py.NoConversion) {
Properties props = new Properties();
String url = (String) parser.arg(0).__tojava__(String.class);
String user = (String) parser.arg(1).__tojava__(String.class);
String password = (String) parser.arg(2).__tojava__(String.class);
String driver = (String) parser.arg(3).__tojava__(String.class);
if (url == null) {
throw zxJDBC.makeException(zxJDBC.DatabaseError, "no url specified");
}
if (driver == null) {
throw zxJDBC.makeException(zxJDBC.DatabaseError, "no driver specified");
}
// the value can't be null
props.put("user", (user == null) ? "" : user);
props.put("password", (password == null) ? "" : password);
String[] kws = parser.kws();
for (int i = 0; i < kws.length; i++) {
Object value = parser.kw(kws[i]).__tojava__(Object.class);
props.put(kws[i], value);
}
try {
Class.forName(driver);
} catch (Throwable e) {
throw zxJDBC.makeException(zxJDBC.DatabaseError, "driver [" + driver + "] not found");
}
try {
c = DriverManager.getConnection(url, props);
} catch (SQLException e) {
throw zxJDBC.makeException(zxJDBC.DatabaseError, e);
}
} else {
c = (Connection) arg;
}
try {
if ((c == null) || c.isClosed()) {
throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to establish connection");
}
return new PyConnection(c);
} catch (SQLException e) {
throw zxJDBC.makeException(zxJDBC.DatabaseError, e);
}
}
/**
* Method toString
*
* @return String
*/
public String toString() {
return "<connect object instance at " + Py.id(this) + ">";
}
}