-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect.py
103 lines (89 loc) · 3.37 KB
/
connect.py
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
import jaydebeapi
import os
import csv
import re
def csv_reader(path):
with open(path, "r") as csvfile:
tmp = {}
reader = csv.reader(csvfile, delimiter='=')
for line in reader:
tmp[line[0]] = line[1]
return tmp
config = csv_reader("properties.settings")
rechnername = config["rechnername"]
username = config["username"]
password = config["password"]
database = config["database"]
class DBUtil:
def __init__(self):
pass
def getConnection(self):
try:
import jpype
if jpype.isJVMStarted() and not jpype.isThreadAttachedToJVM():
jpype.attachThreadToJVM()
jpype.java.lang.Thread.currentThread().setContextClassLoader(
jpype.java.lang.ClassLoader.getSystemClassLoader())
conn = jaydebeapi.connect("com.ibm.db2.jcc.DB2Driver",
"jdbc:db2:{database}".format(
database=database
),
{
'securityMechanism': "4"
},
"jdbc-1.0.jar"
)
return conn
except Exception as e:
print(e)
def getExternalConnection(self):
try:
# Fix
import jpype
if jpype.isJVMStarted() and not jpype.isThreadAttachedToJVM():
jpype.attachThreadToJVM()
jpype.java.lang.Thread.currentThread().setContextClassLoader(
jpype.java.lang.ClassLoader.getSystemClassLoader())
conn = jaydebeapi.connect("com.ibm.db2.jcc.DB2Driver",
"jdbc:db2://"
"{rechnername}.is.inf.uni-due.de:50{gruppennummer}/{database}".format(
rechnername=rechnername,
gruppennummer=re.match(r"([a-z]+)([0-9]+)", username, re.I).groups()[1],
database=database
#user=username.strip()
),
{
'user': username,
'password': password,
'securityMechanism': "3"
},
os.path.join(os.getcwd(), 'jdbc-1.0.jar')
)
#conn.autocommit = False
return conn
except Exception as e:
print(e)
def checkDatabaseExists(self):
exists = False
conn = False
try:
conn = self.getConnection()
if conn is not None:
exists = True
except Exception as e:
print(e)
finally:
conn.close()
return exists
def checkDatabaseExistsExternal(self):
exists = False
conn = False
try:
conn = self.getExternalConnection()
if conn is not None:
exists = True
except Exception as e:
print(e)
finally:
conn.close()
return exists