0

I'm doing as a school project a multi-platform Distributed Data Base System

I need to extract data from the Data Base in Java so i dynamically load my jdbc connector

Works Perfect in Windows

But in Linux I got the error:

"No suitable driver found for jdbc:mysql://..."

This is the code:

File f = new File("mysql-connector-java-5.1.24-bin.jar");
URLClassLoader urlCl = new URLClassLoader(new URL[] { f.toURL()},System.class.getClassLoader());
Class conector = urlCl.loadClass("com.mysql.jdbc.Driver");
conector.newInstance();


Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost/test","root",""); 
Statement instruccion = conexion.createStatement();
ResultSet tabla = instruccion.executeQuery("select * from prueba where uno=1");
while(tabla.next())
{
     System.out.println(tabla.getString(1));
     System.out.println(tabla.getString(2));
}

 conexion.close();

I don't know what can I do.

This it's made to avoid the installation of the connector on each site

I pass a file with the configuration for each DB, if is postgresql load postgres jdbc conector if is mysql etc...

Suggestions?

1 Answers1

1

Why don't just put the connector into a lib sub-directory and then passed all jar contained in this one to the Java Class-Path ?

Samples folder tree :

  • MyApp
    • bin
      • launcher.sh
      • MyApp.jar
    • lib
      • myLib.jar

Here is the launcher.sh script :

#!/bin/sh
#Set basedir
LAUNCHER_DIR=$(cd $(dirname $0); pwd)

#Set Java Class-Path
CLASSPATH="$LAUNCHER_DIR/bin/MyApp.jar"$(find "$LAUNCHER_DIR" -name '*.jar' -printf ":%p")

#Launch application
java -cp "$CLASSPATH" com.company.MyApp $*

EDIT: It is not recommanded to directly use File.toURL as describe in the documentation, you must do File.toURI().toURL().

LoganMzz
  • 1,597
  • 3
  • 18
  • 31
  • The idea is add nothing to the classpath, if the app need to connect to a postgresql database for example just load the jdbc connector and you're done. The whole app uses sockets to send the ToDo list, this one have the user,password,database,connector,etc... of each database imagine that you need to connect for a first time to a Oracle database Just send the jar of the connector, then the ToDo and your done You don't have to properly install the jdbc connector, Do you get my point? – Rodrigo Chaparro Jun 13 '13 at 10:39
  • Why don't directly used the instanciated driver ? – LoganMzz Jun 13 '13 at 11:39