I'm trying to connect my basic JavaRMI app to an oracle database.
But I'm stuck with the ojdbc jar and ClassNotFoundException
.
I can't use some IDE (teacher don't want), so I have to do all by notepad and terminal prompt.
I use Ant to compile the application.
There is the structure :
- Build (contains all .class and stub)
- lib/oracle/ (conatains the oracle-full package found on the website, with ojdbc7.jar)
- src (contains all .java)
- build.xml (for ant)
I founded that I must use Class.forName("oracle.jdbc.driver.OracleDriver");
. But didn't work alone.
So next i founded some people who said that we have to set the classpath,
so I do :
export CLASSPATH=$CLASSPATH:./lib/oracle/ojdbc7.jar
but nothing happens.
I begin to try with Maven but I've surrender because the dependencies can't be dowload easily (Oracle license).
See behind an extract of my code :
import java.rmi.server.*;
import java.net.*;
import java.rmi.*;
import java.sql.*;
import java.util.ArrayList;
public class ObjectFactoryCompte extends UnicastRemoteObject implements IObjectFactoryCompte {
private Connection dbConnection;
private ArrayList<Compte> instantiatedAccounts = new ArrayList<Compte>();
public static void main(String[] args) {
try {
ObjectFactoryCompte serveur = new ObjectFactoryCompte();
String nom = "compteFactory";
// Enregistrement
Naming.rebind(nom, serveur);
System.out.println("Serveur enregistré avec succès !");
} catch (Exception e) {
System.err.println("Erreur : " + e);
}
}
public ObjectFactoryCompte() throws RemoteException {
super();
try {
connectToDatabase();
} catch (ClassNotFoundException e) {
System.err.println("Impossible de trouver le .jar correspondant au jdbc Oracle.");
System.err.println("Fin de l'application.");
System.exit(2);
} catch (SQLException e) {
System.err.println("Une erreur s'est produite lors de la connexion à la base de données avec pour cause :");
e.printStackTrace();
System.err.println("Fin de l'application.");
System.exit(1);
}
}
private void connectToDatabase() throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
String host = "jdbc:oracle:thin:@srv:port:thing";
String user = "usr";
String pwd = "pwd";
dbConnection = DriverManager.getConnection(host, user, pwd);
}
}
Can you help me to clear this issue please ? I'm running out of idea... :/
Thanks