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?

4

1 回答 1

1

为什么不直接将连接器放入 lib 子目录,然后将包含在该目录中的所有 jar 传递给 Java Class-Path ?

样本文件夹树:

  • 我的应用
    • 垃圾桶
      • 启动器.sh
      • 我的应用程序.jar
      • myLib.jar

这是 launcher.sh 脚本:

#!/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 $*

编辑:不建议直接使用文档中描述的File.toURL ,您必须执行 File.toURI().toURL()。

于 2013-06-13T10:26:43.620 回答