Jython - Trying to connect to database results in java.sq l.SQLException: No suitable driver found
I'm using the Confluence Script Plugin to write Jython macros in Confluence. I have some code that recently stopped working after an upgrade to Confluence and the Plugin. I've tried various possible solutions found here on SO and the web without any luck.
I'm trying to connect to an Oracle database to query some values. Here's my code:
import sys from oracle.jdbc.driver import OracleDriver from java.sql import DriverManager, SQLRecoverableException def connect(user, pw, sid, host, port): connection = None DriverManager.registerDriver(OracleDriver()) try: connection = DriverManager.getConnection("jdbc:oracle:thin:@"+host+":"+port+":"+sid, user, pw) except SQLRecoverableException, e: print 'ERROR: Cannot connect to database %s %s %s: %s.' % (host, port, sid, e) except: print 'ERROR: Cannot connect to database %s %s %s: %s.' % (host, port, sid, sys.exc_info()) return connection conn = connect(db_user, db_pw, db_sid, db_host, db_port)
Running this results in the following error (note: real hostname, port, and sid removed):
ERROR: Cannot connect to database host port sid: java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@host:port:sid.
This error made me think that ojdbc6.jar couldn't be found or isn't in the right directory. Just in case, I explicitly added it to the classpath. This still didn't work. After adding it to the classpath, I verified that the classpath is properly being set by checking the process args and I see that ojdbc6.jar is being processed in catalina.out. (note: real install path removed)
argv: -classpath argv: :<install_path>/atlassian-confluence-4.3.1/lib/ojdbc6.jar:<install_path>/atlassian-confluence-4.3.1/bin/bootstrap.jar
*sys-package-mgr*: processing new jar, '<install_path>/atlassian-confluence-4.3.1/lib/ojdbc6.jar'
I've also verified that we're currently using jdk1.6.0_33, so ojdbc6.jar should be the right thin client. Anybody know what could be causing the error?
- JDK: Sun 1.6.0_33-b03
- Jython: 2.5.2
- Confluence: 4.3.1
- Script Plugin: 4.1.0
- OS: SunOS 5.10
- App Server: Apache Tomcat/6.0.32
I could not get this working either. I followed the Oracle tutorial (here: http://www.oracle.com/technetwork/articles/dsl/mastering-oracle-python-providers-1395759.html ), however, when I switched to using the Oracle Call Interface (OCI) method described in the article above, everything worked flawlessly. As the article outlines, using OCI has several advantages such as connection pooling, etc. Here is the code that worked for me:
import sys import java.sql.SQLException from oracle.jdbc.pool import OracleDataSource cs = "jdbc:oracle:thin:@localhost:1521:XE" ods = OracleDataSource() ods.setURL(cs) ods.setUser("hr") ods.setPassword("hr") try: conn = ods.getConnection() except java.sql.SQLException, e: print "Problem connecting to \"%s\"" % cs sys.exit() stmt = conn.createStatement() rs = stmt.executeQuery("select * from departments order by 2") while rs.next(): print rs.getString(2) rs.close() stmt.close() conn.close()