这两个jar包是class12.jar和ojdbc14.jar,因为需要操作的数据库版本不同(有gbk8i和gbk11g),需要对指定版本的数据库用指定的jar包的驱动类。
File file = new File("lib" + File.separator + "classes12.jar");
String jarPath = "file:" + file.getAbsolutePath();
System.out.println(file.getAbsolutePath());
try {
URL u = new URL(jarPath);
URLClassLoader oracle10g = new URLClassLoader(new URL[] { u });
Class<?> c = oracle10g.loadClass("com.mysql.jdbc.Driver");
Driver driver = (Driver) c.newInstance();
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/test", "root", "123456");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
打印出来 driver 的值是“com.mysql.jdbc.Driver@de6f34”,说明内存中有值;但是getConnection()方法就会出错java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1:3306/test,说明加载驱动类出了问题。
------解决方案--------------------------------------------------------
在两个jar包中的META-INF/MANIFEST.MF文件中指定Class-Path
------解决方案--------------------------------------------------------
表示相当费解,怎么一会oracle,一会mysql啊?