Par JFD BCX le 15/02/2007 - 17:02
Je tente désespérément de me connecter à une base de données distante (sous serveur PostgreSql 8.2) depuis une appli Eclipse RCP (Java plugin donc).
Je lève systématiquement l'exception suivante : No suitable driver found for jdbc:postgresql://Poweredge/biochemics
J'ai placé strictement le même code dans une application Java standard (toujours sous Eclipse) et cela marche parfaitement.
A noter que 'Class.forName ("org.postgresql.Driver")' ne fonctionne pas dans ce contexte.
package gui_auto;
import org.eclipse.core.runtime.IPlatformRunnable;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import java.sql.*;
import java.util.Enumeration;
import java.lang.reflect.*;
public class Application implements IPlatformRunnable {
public Object run(Object args) throws Exception {
Display display = PlatformUI.createDisplay ();
Class CnxClass = null;
/*// The following snippet does'nt work in a extension, only in a regular application !!!
try {
CnxClass = Class.forName ("org.postgresql.Driver");
System.out.println ("Class found (forName) " + CnxClass.toString ());
} catch (ClassNotFoundException e) {
System.err.println ("Class not found (forName) ! " + e.toString ());
}*/
// Try to load the class from ContextClassLoader
Thread t = Thread.currentThread ();
ClassLoader cl = t.getContextClassLoader ();
Class toRun;
try {
toRun = cl.loadClass ("org.postgresql.Driver");
System.out.println ("Class loaded as " + toRun.toString ());
Method [] methods = toRun.getMethods ();
for (int i = 0; i < methods.length; i++)
System.err.println (methods [i].getName () + " function found in " + toRun.toString ());
} catch (ClassNotFoundException e) {
System.err.println ("Class not found (loadClass) ! " + e.toString ());
}
/* // This works fine too...
try {
Driver driver = (Driver) new org.postgresql.Driver ();
//DriverManager.registerDriver (driver); -- Driver recorded twice !!!
} catch (Exception e) {
System.err.println ("Class not found (complex forName) ! " + e.toString ());
} */
// Check available drivers
for (Enumeration wEnum = DriverManager.getDrivers (); wEnum.hasMoreElements() ;) {
System.out.println ("Driver found : " + wEnum.nextElement().toString ());
}
// Try to connect the database
Connection conn = null;
String url;
try {
url = "jdbc:postgresql://Poweredge/biochemics";
conn = DriverManager.getConnection (url, "postgres", "postgres");
} catch (SQLException E) {
System.err.println ("Unable to connect to biochemics database : " + E.toString ());
E.printStackTrace ();
}
... l'exception est générée ici, la trace du programme suit :
Class loaded as class org.postgresql.Driver
acceptsURL function found in class org.postgresql.Driver
getPropertyInfo function found in class org.postgresql.Driver
jdbcCompliant function found in class org.postgresql.Driver
notImplemented function found in class org.postgresql.Driver
setLogLevel function found in class org.postgresql.Driver
getLogLevel function found in class org.postgresql.Driver
makeSSL function found in class org.postgresql.Driver
sslEnabled function found in class org.postgresql.Driver
connect function found in class org.postgresql.Driver
getMajorVersion function found in class org.postgresql.Driver
getMinorVersion function found in class org.postgresql.Driver
getVersion function found in class org.postgresql.Driver
hashCode function found in class org.postgresql.Driver
getClass function found in class org.postgresql.Driver
wait function found in class org.postgresql.Driver
wait function found in class org.postgresql.Driver
wait function found in class org.postgresql.Driver
equals function found in class org.postgresql.Driver
toString function found in class org.postgresql.Driver
notify function found in class org.postgresql.Driver
notifyAll function found in class org.postgresql.Driver
Driver found : sun.jdbc.odbc.JdbcOdbcDriver@d6c16c
Unable to connect to biochemics database : java.sql.SQLException: No suitable driver found for jdbc:postgresql://Poweredge/biochemics
java.sql.SQLException: No suitable driver found for jdbc:postgresql://Poweredge/biochemics
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at gui_auto.Application.run(Application.java:64)
at org.eclipse.core.internal.runtime.PlatformActivator$1.run(PlatformActivator.java:78)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:92)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:68)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:400)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.core.launcher.Main.invokeFramework(Main.java:336)
at org.eclipse.core.launcher.Main.basicRun(Main.java:280)
at org.eclipse.core.launcher.Main.run(Main.java:977)
at org.eclipse.core.launcher.Main.main(Main.java:952)