gpt4 book ai didi

java - 如何解决 jaydebeapi.connect 命令的 "1st arg can' t 被强制为 String 的问题?

转载 作者:行者123 更新时间:2023-11-30 05:21:59 26 4
gpt4 key购买 nike

在 Windows Server 2016 上,我们尝试使用 Jython 脚本通过 JDBC 连接,但我们的 jaydebeapi.connect 语句给出以下错误:

TypeError: getConnection(): 1st arg can't be coerced to String

然而,当我们查看examples of using jaydebeapi时,第一个参数一个字符串。

这是我们的 Python 代码:

jclassname = "com.microsoft.sqlserver.jdbc.SQLServerDriver" 
database = "our_database_name"
db_elem = ";databaseName={}".format(database) if database else ""
host = "###.##.###.###" # ip address
port = "1433"
user = "user_name"
password = "password"
url = (jdbc:sqlserver://{host}:{port}{db_elem}" ";user={user};password={password}".format(host=host, port=port, db_elem=db_elem, er=user, password=password) )
print url
driver_args = [url]
jars = None
libs = None
db = jaydebeapi.connect(jclassname, driver_args, jars=jars, libs=libs)

这就是我们运行 Python 脚本的方式:

C:\jython2.7.0\bin\jython.exe C:\path_to_our_script.py

我们缺少什么?我们如何解决 jaydebeapi.connect 语句的字符串强制转换错误?

最佳答案

来自 JayDeBeApi 文档 -

Basically you just import the jaydebeapi Python module and execute the connect method. This gives you a DB-API conform connection to the database.

The first argument to connect is the name of the Java driver class. The second argument is a string with the JDBC connection URL. Third you can optionally supply a sequence consisting of user and password or alternatively a dictionary containing arguments that are internally passed as properties to the Java DriverManager.getConnection method. See the Javadoc of DriverManager class for details.

您从 DriverManager.getConnection 方法中收到此错误。

来自 DriverManager 的 JavaDocs -

public static Connection getConnection(String url, Properties info)

所以,您的 jaydebeapi.connect 函数调用困惑了。您的第二个参数应该是字符串形式的 url

以下是来自 JayDeBeApi 文档的示例片段。

>>> import jaydebeapi
>>> conn = jaydebeapi.connect("org.hsqldb.jdbcDriver",
... "jdbc:hsqldb:mem:.",
... ["SA", ""],
... "/path/to/hsqldb.jar",)

关于java - 如何解决 jaydebeapi.connect 命令的 "1st arg can' t 被强制为 String 的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59417365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com