Jython: cannot import Java packages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jindra
    New Member
    • Apr 2012
    • 4

    Jython: cannot import Java packages

    Hello,
    I developed some useful Jython script in Eclipse, using ojdbc6.jar as driver library (attached to the project as "external library"). When debugging in Eclipse, everything goes fine. Also standalone run on the development PC runs OK.

    Then I copied the complete Jython 2.5.2 installation to another machine (with Java JRE 1.6.30), copied my script and the ojdbc6.jar library in the same directory as the script.

    I call it with

    <pathtojava>\ja va.exe -cp ojdbc6.jar -jar ..\jython2.5.2\ jython.jar myscript.py (which works on the original PC)

    Now, it throws exception in this code:

    from oracle.jdbc import OracleDriver
    ...
    class SqlTester:
    ...
    def connect( self ):
    self.driver = OracleDriver()
    self.connection = DriverManager.g etConnection( "jdbc:oracle:th in:@%s:%s:%s" % ( self.host, self.port, self.sid ), self.user, self.psw )
    ...

    The exception is:

    Traceback (most recent call last):
    File "myscript.p y", line 119, in <module>
    tester.run_all_ tests()
    File "myscript.p y", line 79, in run_all_tests
    self.connect()
    File "myscript.p y", line 60, in connect
    self.connection = DriverManager.g etConnection( "jdbc:oracle:th in:@%s:%s:%s" % ( self.host, self.port, self.sid )
    at java.sql.Driver Manager.getConn ection(Unknown Source)
    at java.sql.Driver Manager.getConn ection(Unknown Source)
    at sun.reflect.Nat iveMethodAccess orImpl.invoke0( Native Method)
    at sun.reflect.Nat iveMethodAccess orImpl.invoke(U nknown Source)
    at sun.reflect.Del egatingMethodAc cessorImpl.invo ke(Unknown Source)
    at java.lang.refle ct.Method.invok e(Unknown Source)
    java.sql.SQLExc eption: java.sql.SQLExc eption: No suitable driver found for jdbc:oracle:thi n:@xxxxxx:1521: DWH

    What am I doing wrong?
  • sourabh mittal
    New Member
    • Jun 2011
    • 7

    #2
    Hi! well i'm going to start using jython which i planned already as addicted to easiness of python and performance of java! I don't know but i'm 100% sure this may help you! download this pdf book "The definite guide to Jython and read the importing java packages part. This book also provides precautions while importing java packages! and please also reply my post about java multiplatform deployment! please reply! It's a direct link! you can also resume it using Internet download manager!

    Link : http://serek.eurotrip.pl/Android_boo...20(Apress).pdf

    Comment

    • Jindra
      New Member
      • Apr 2012
      • 4

      #3
      Hello,
      finally resolved. It is not an import problem (driver imports OK, when instantiated, call to main() prints driver info).

      The real problem is non-functional java.sql.Driver Manager in Jython

      java.sql.Driver Manager.getConn ection() will always report "no suitable driver found" or similar, depending if it is Java 5 or Java 6.

      The solution is very simple: since I have the OracleDriver class already imported, I just use it:

      drv = OracleDriver()
      connection = drv.connect( myJdbcUrl, java.util.Prope rties() )

      In Python you can easily select and instantiate your java.sql.Driver for any database connection dynamically. So you don't need java.sql.Driver Manager, which was actually invented in Java to allow for dynamic driver selection...

      I am only puzzled by one thing. This DriverManager problem seems to be the most frequent problem in Jython when using JDBC. Yet, I have not seen a single answer (on Google) to describe the correct way how to use JDBC; on the contrary, jython tutorials I saw all copy the Java stuff using DriverManager. Have nobody ever made any test to see whether what they teach is true?
      Last edited by Jindra; Apr 3 '12, 09:16 AM. Reason: precise expression

      Comment

      • satishmoningi
        New Member
        • Apr 2017
        • 1

        #4
        Hi Jindra,
        I am also facing same issue with following jython script. Could you please verify where I went wrong.
        Code:
        from java.lang import Class
        import sys
        from java.util import Properties
        sys.path.append('/opt/IBM/WASLogs/shared-libs/ojdbc6.jar')
        from oracle.jdbc import OracleDriver
        
        
        driver = OracleDriver()
        myJdbcUrl = "jdbc:oracle:thin:@localhost:2347:PRBPMDB1"
        info = Properties()
        info.put ("user", "user");
        info.put ("password","user1");
        conn = driver.connect(myJdbcUrl,info)
        stmt = conn.createStatement()
        rs = stmt.executeQuery("select * from wom_message_status where id=1067")
        
        while rs.next():
                print "%s" % rs.getString(3)
        
        rs.close()
        stmt.close()
        conn.close()
        Thanks,
        Satish Moningi
        +91 7893742724

        Comment

        Working...