[英]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. 我正在使用Confluence脚本插件在Confluence中编写Jython宏。 I have some code that recently stopped working after an upgrade to Confluence and the Plugin. 我有一些升级到Confluence和插件后最近停止工作的代码。 I've tried various possible solutions found here on SO and the web without any luck. 我尝试了在SO和Web上找到的各种可能的解决方案,但没有任何运气。
I'm trying to connect to an Oracle database to query some values. 我正在尝试连接到Oracle数据库以查询一些值。 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()[1])
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): 运行此命令将导致以下错误(注意:真实主机名,端口和sid已删除):
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. 这个错误使我认为找不到ojdbc6.jar或不在正确的目录中。 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. 将其添加到类路径后,我通过检查进程args验证了是否正确设置了类路径,并且我发现在catalina.out中正在处理ojdbc6.jar。 (note: real install path removed) (注意:实际安装路径已删除)
Process args: 处理参数:
argv[21]: -classpath
argv[22]: :<install_path>/atlassian-confluence-4.3.1/lib/ojdbc6.jar:<install_path>/atlassian-confluence-4.3.1/bin/bootstrap.jar
catalina.out: catalina.out:
*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. 我还验证了我们当前正在使用jdk1.6.0_33,因此ojdbc6.jar应该是正确的瘦客户端。 Anybody know what could be causing the error? 有人知道是什么原因引起的错误吗?
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. 但是,当我切换到使用Oracle Call Interface(OCI)时,我遵循了Oracle教程(在这里: http : //www.oracle.com/technetwork/articles/dsl/mastering-oracle-python-providers-1395759.html )。 )上面文章中所述的方法,一切都正常运行。 As the article outlines, using OCI has several advantages such as connection pooling, etc. Here is the code that worked for me: 如本文所述,使用OCI具有诸如连接池等优点。这是对我有用的代码:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.