Search code examples
javapythonjythonwindows-server-2016jaydebeapi

How to solve a "1st arg can't be coerced to String" for a jaydebeapi.connect command?


On Windows Server 2016, we are trying to connect over JDBC with a Jython script but our jaydebeapi.connect statement is giving the following error:

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

Yet, when we look at examples of using jaydebeapi, the first argument is a string.

This is our Python code:

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)

This is how we are running our Python script:

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

What are we missing? How do we solve this string coercion error for our jaydebeapi.connect statement?


Solution

  • From JayDeBeApi Docs -

    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.

    You are getting this error from the DriverManager.getConnection method.

    From the JavaDocs for DriverManager -

    public static Connection getConnection(String url, Properties info)
    

    So, your jaydebeapi.connect function call is messed up. Your second argument should be the url as a string.

    Following is a sample snippet from the JayDeBeApi docs.

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