Many a time, you might want to test if you could connect to a database or not. The simplest way is of course having a database client installed and use it. However, this method may not be possible in all cases. For example, on an ITM warehouse proxy, you'll have only Db2 JDBC type 4 drivers, (which are nothing but two files, db2jcc.jar and db2jcc_license_cu.jar). How do you test connectivity on such systems? Here is a Jython script.
Why Jython?
In addition to the self-serving opportunity of me learning Jython, Jython can reuse the same JDBC drivers that TDW uses. So there is no need to setup Perl DBI or database client. We could write the code in Java, but that's an overkill because of compilation requirements. Setting up Jython is very easy. Please refer to this page for detailed installation steps.
Code
Here is the sample code. Just be sure to change the connection settings.
Note: Do NOT cut-paste the code from below listing. Jython, like python, is sensitive to tabs. So, download the code by clicking here.
from com.ziclix.python.sql import zxJDBC
import java.sql
conn = "jdbc:db2://server:50000/warehous"
user = "user"
passwd = "passwd"
driver = "com.ibm.db2.jcc.DB2Driver"
try:
db = zxJDBC.connect(conn, user, passwd, driver)
print "Connection successful"
except zxJDBC.DatabaseError, e:
print "Connection failed:", e
No comments:
Post a Comment