11

I am trying to connect an Oracle database to Mathematica 8. Another question already says how it can be done in for a MySQL database but it does not work for me:

Needs["DatabaseLink"] AND conn = OpenSQLConnection[JDBC["MySQL(Connector/J)", 
"yourserver/yourdatabase"], "Username" -> "yourusername", "Password" -> "yourpassword"]

The following information is available for me on my desktop:

filepath = "C:\oracle\ora92\network\ADMIN\tnsnames.ora"; HOST; PORT; username; password;

conn = OpenSQLConnection[JDBC["MySQL(Connector/J)", HOST], "Username" -> username, "Password" -> password]

Error message: JDBC::error: 
    Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. 
    The driver has not received any packets from the server. >>

Does anyone know how I can connect or continue?

Chenmunka
  • 685
  • 4
  • 21
  • 25

4 Answers4

12

Mathematica 8 does not come pre-equipped with an Oracle driver, a fact that can be verified by evaluating these expressions:

Needs["DatabaseLink`"]
JDBCDriverNames[]
(*
Out[2]= {Microsoft Access(ODBC),hsqldb,HSQL(Memory),HSQL(Server),
         HSQL(Server+TLS),HSQL(Standalone),HSQL(Webserver),HSQL(Webserver+TLS),
         jtds_sqlserver,jtds_sybase,mysql,MySQL(Connector/J),ODBC(DSN),odbc,
         PostgreSQL,Microsoft SQL Server(jTDS),Sybase(jTDS),HSQL 2.0.1}
*)

We will have to follow the instructions in the Mathematica documentation that describes how to install a new JDBC driver.

First, we will have to create a new resource directory in which to place the necessary JDBC driver JAR file:

$jarDirectory =
  CreateDirectory @
    FileNameJoin @
      {$UserBaseDirectory, "Applications", "Oracle", "Java"}

Next, we must select a JDBC driver to use. Visit the relevant Oracle page to find the correct JDBC driver for your database.

Chose a driver version that is compatible with Java 6, the version that Mathematica 8 uses internally. For this example, I chose to use the Oracle 11.2.0.2.0 driver for Java 6. Download the file and then move it into the resource directory just created:

SystemOpen[$jarDirectory]

Next, we will create a JDBC driver configuration file so that the new driver is registered with Mathematica:

$configDirectory =
  CreateDirectory @
    FileNameJoin @
      {$UserBaseDirectory, "Applications", "Oracle", "DatabaseResources"}

Export[
  FileNameJoin @ {$configDirectory, "Oracle.m"}
, JDBCDriver[
    "Name" -> "Oracle"
  , "Driver" -> "oracle.jdbc.driver.OracleDriver"
  , "Protocol" -> "jdbc:oracle:thin:@"
  , "Version" -> 1
  ]
, "Text"
]

The driver is now installed:

JDBCDriverNames[]
(*
Out[9]= {Oracle,Microsoft Access(ODBC),hsqldb,HSQL(Memory),HSQL(Server),
         HSQL(Server+TLS),HSQL(Standalone),HSQL(Webserver),HSQL(Webserver+TLS),
         jtds_sqlserver,jtds_sybase,mysql,MySQL(Connector/J),ODBC(DSN),odbc,
         PostgreSQL,Microsoft SQL Server(jTDS),Sybase(jTDS),HSQL 2.0.1}
*)

If the fates are smiling, we can now establish a connection and execute an SQL query:

$connection =
  OpenSQLConnection[
    JDBC["Oracle", "myserver:1521:mysid"]
  , "Username" -> "scott"
  , "Password" -> "tiger"
  ]

SQLExecute[$connection, "SELECT 'success!' FROM DUAL"]

... where myserver is the database server name, 1521 is the listener port number and mysid is the Oracle System ID (SID).

Oracle JDBC URLs come in many forms. For details, take a look at the Oracle FAQ.

Andrew Moylan
  • 2,893
  • 18
  • 20
WReach
  • 18,098
  • 3
  • 49
  • 93
  • How is the file for the driver called exactly? I tried "ojdbc6.jar" but Mathematica still does not see Oracle. – Patrick Bernhard Aug 25 '11 at 17:06
  • Hit enter accidentally - thank you very much for your already really helpful comments and insights! Patrick – Patrick Bernhard Aug 25 '11 at 17:09
  • @Patrick `ojdbc6.jar` is the correct file. I have had problems before where a database resource file is not recognized when it starts with a comment. Try running the `Export` expression again, this time with "`Text`" added as the last argument. I have updated that expression in my response so you can copy it from there. – WReach Aug 25 '11 at 19:54
  • Almost there - have Oracle installed successfully. It seems that only the SID is missing and I tried to insert that at several places, what did not work. – Patrick Bernhard Aug 26 '11 at 15:38
  • JDBC::error: ""Listener refused the connection with the following error:\\ nORA-12504, TNS:listener was not given the SID in CONNECT_DATA " – Patrick Bernhard Aug 26 '11 at 15:41
  • @Patrick You can add the SID after the port number, e.g. `myserver:1521:SID`. – WReach Aug 26 '11 at 16:55
  • Success - I am connected :-) thank you very, very much for your help! – Patrick Bernhard Aug 29 '11 at 13:21
  • on my installation, "User" had to be "Username". Otherwise perfect answer and very well stated! – Reb.Cabin Mar 06 '13 at 21:46
  • @PatrickBernhard may I suggest that you mark WReach's answer as "accepted?" It seems to have worked for a lot of people (including me) – Reb.Cabin Mar 06 '13 at 21:48
6

I suspect that you are using the wrong JDBC driver - you should be using the Oracle JDBC driver, rather than MySQL one. When I was using DatabaseLink to connect to an Oracle database, I used this command:

OpenSQLConnection[
  JDBC[
   "oracle.jdbc.driver.OracleDriver", 
   "jdbc:oracle:thin:@server:port:dbname"
  ], 
  "Name" -> "dbname", 
  "Username" -> "YourUserName", 
  "Password" -> "YourPassword"
]

You should make sure to put the proper Oracle JDBC driver (corresponding to your Oracle db version) into a place where Mathematica can find it. This procedure is described in the documentation for the DatabaseLink, section JDBC Connections. You can test which JDBC drivers are visible to Mathematica by executing JDBCDrivers[]. Make sure that you install and use the correct driver corresponding to your DB version, b.t.w. - incorrect driver versions may result in very nasty and non-obvious bugs (this is unrelated to Mathematica).

Leonid Shifrin
  • 22,449
  • 4
  • 68
  • 100
1

Whilst the answer from WReach above is correct it may also be helpful to know that there are 2 additional lines that are useful - namely to make sure Jlink is loaded and the Java ClassPath is correct and pointing to your oracle jdbc6.jar file.

Needs["JLink`"]
AddToClassPath[
FileNameJoin[{$UserBaseDirectory, "Applications", "Oracle", 
"Java"}]];

or if using the answer verbatim just

Needs["JLink`"]
AddToClassPath[$jarDirectory];
Gordon Coale
  • 111
  • 3
1

My guess would be that you shouldn't use MySQL JBDC connections for Oracle. Although it is for Mathematica 5.2, here is an article that you perhaps can use as something to go from.

I've barely used Mathematica, and certainly not with a database, but from that page, it looks like you can do this:

OpenSQLConnection[JDBC["oracle","server.business.com:1999"],
    Username -> "you"]
bhamby
  • 15,112
  • 1
  • 45
  • 66