Re: Packages--JLink class is loaded but cannot be found?

*To*: mathgroup at smc.vnet.net*Subject*: [mg66582] Re: [mg66548] Packages--JLink class is loaded but cannot be found?*From*: Todd Gayley <tgayley at wolfram.com>*Date*: Sat, 20 May 2006 04:48:28 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

Walter, First off, I want to point out that people who have Mathematica 5.1 and later have the DatabaseLink application, which is what one would use to interact with Oracle or other databases. You have Mathematica 5.0, so you are stuck with using "raw" J/Link as in Brian's JDBC example code. The problem you are having is a subtle classloading problem. The JDBC DriverManager uses a rather unfortunate scheme for locating driver classes. The DriverManager only sees classes that are loaded by the same classloader that loaded the DriverManager class itself. When using J/Link, the DriverManager class is loaded by the system classloader (because it is a built-in part of Java), but the driver class is loaded by the JLinkClassLoader (because it is found via the classpath). Although there is another way around this, an easy thing to do is to put the ojdbc14.jar file into the Java runtime's "extensions" directory, which is <mathematica dir>/SystemFiles/Java/Windows/lib/ext. Then it will be found by the DriverManager and you won't need to use AddToClassPath or any other classpath-related tweaks. One final thing. Whenever you type a string literal in Mathematica (just as in C, Java, etc.), you have to use double backslashes, as a single backslash is an escape character: AddtoClassPath["c:\\oracle\\product\\9.2\\db_1\\jdbc\\lib\\ojdbc14.jar"] Todd Gayley Wolfram Research At 02:40 AM 5/19/2006, wbogorad at yahoo.com wrote: >Hi, >I'm very new to Mathematica and JLink but I know a thing or two about >Java. I'm using Mathematica 5.0 and I'm trying to follow Brian Higgins' >example (http://www.higgins.ucdavis.edu/JLink/JavaDatabaseConnect.nb) to >connect to Oracle (not MySQL database and I'm having a strange problem >that the system seems is loading oracle jdbc driver but than cannot see it. > >Here how I do it > >Needs["JLink`"] >InstallJava[]; \ >AddToClassPath["c:\oracle\product\9.2\db_1\jdbc\lib\ojdbc14.jar"] > >--- at this point the system seems to be happy with the classpath: >{C:\Program Files\WolframResearch\Mathematica\5.0\SystemFiles\Java\, \ >C:\Program Files\WolframResearch\Mathematica\5.0\.\, \ >c:\oracle\product\9.2\db_1\jdbc\lib\ojdbc14.jar, C:\Program \ >Files\WolframResearch\Mathematica\5.0\SystemFiles\Java\Windows\lib\tools.jar} > >Off[General::"spell1"] >myDriverManager = LoadJavaClass["java.sql.DriverManager"] >Methods[myDriverManager] >myClass = LoadJavaClass["java.lang.Class"] >LoadJavaClass["com.wolfram.jlink.JLinkClassLoader"]; >cls = JLinkClassLoader`classFromName["oracle.jdbc.driver.OracleDriver"] >cls[newInstance[]] >-- the output is: Â«JavaObject[oracle.jdbc.driver.OracleDriver]Â» so it >seems that the driver instance is created. > >myDriverList = java`sql`DriverManager`getDrivers[] >s = {}; >While[myDriverList[hasMoreElements[]], AppendTo[s, \ >myDriverList[nextElement[]]]] >s >-- but when I try to get a list of loaded drivers the list is empty; the >output is: {} > >My question is what have I done wrong?