Question

So a little background on my problem: I am trying to copy over a table on an Microsoft SQL system from an Oracle database. Besides giving password and user access to the table I cannot edit or do anything to the MSSQL database.

I successfully used the Oracle SQL Developer to connect and view the tables I want (using a third party JDBC driver), but I want to set up an automated copy-over into my Oracle database so I am attempting to use the same driver in some stored java code.

I have a java function that all it should do is go and count the number of entries in the table. So far my code looks like:

public static String getCount() {
Statement stmt = null;
Connection conn = null;
int rowCount = 0;
String message = "";
try {
  Class.forName("net.sourceforge.jtds.jdbc.Driver");
}
catch(ClassNotFoundException e) {
  System.err.println("Error loading driver: " + e);
  message = message + e + " -ER1 \n";
}
try {
  conn = DriverManager.getConnection("jdbc:jtds:sqlserver://site.school.edu:2000/ACCESS", "user", "password");
  stmt = conn.createStatement();
  String strSelect = "select 1 as field;";
  ResultSet rset = stmt.executeQuery(strSelect);
  while (rset.next()) {
   ++rowCount;
 }
}
catch(SQLException ex) {
  ex.printStackTrace();
  message = message + ex.getSQLState() + " -ER2";
}
finally {
  try {
    if (stmt != null) stmt.close(); 
    if (conn != null) conn.close();
  } catch(SQLException ex) {
    ex.printStackTrace();
    message = message + ex.getSQLState() + "-ER3";
  }
}
return message;
}

Which is being calling from a stored function :

CREATE OR REPLACE function Schema.java_testMessage return varchar2
as language java 
name 'ConnectAndQuery.getCount() return java.lang.String';

Which I am calling from a script in TOAD:

set serveroutput on;

declare
 words varchar2(400);
 begin
   words := KSL_ADMIN.java_testMessage;
   dbms_output.put_line(words);
 end;

However the result is that I'm getting:

 java.lang.ClassNotFoundException: net/sourceforge/jtds/jdbc/Driver -ER1 
 08001 -ER2
 PL/SQL procedure successfully completed.

I have the jar file within the class path, I can't think of any reason it shouldn't have the nessecary permissions to see the jar, and as far as I can tell I have everything spelled correctly.

Please help me figure out what I am doing wrong. Or if there is perhaps an easier way to go about connecting an Oracle DB to an MSSQL DB without really installing anything. Any knowledge on this is welcome as I am pretty new to a lot of this.

Was it helpful?

Solution

Oracle has its own internal java virtual machine and it does not use the system classpath. If you need external libraries you must “load” them into the internal JVM. You can do this using Oracle's loadjava tool.

See the Oracle's loadjava documentation (http://docs.oracle.com/cd/B28359_01/java.111/b31225/cheleven.htm#JJDEV10060)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top