贴代码了!这个是ClassLoader,从别人那里偷的 /* * Created on 2004/01/09 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package lib;import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Enumeration; import java.util.Properties; import java.util.StringTokenizer; import java.util.Vector; import java.util.zip.ZipEntry; import java.util.zip.ZipFile;/** * @author * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public class LibraryClassLoader extends ClassLoader { /** scanned class path */ private Vector fPathItems; /** default excluded paths */ private String[] defaultExclusions= { "junit.framework.", "junit.extensions.", "junit.runner." }; /** name of excluded properties file */ static final String EXCLUDED_FILE= "excluded.properties"; /** excluded paths */ private Vector fExcluded;
/** * Constructs a TestCaseLoader. It scans the class path * and the excluded package paths */ public LibraryClassLoader() { this(System.getProperty("java.class.path")); }
/** * Constructs a TestCaseLoader. It scans the class path * and the excluded package paths */ public LibraryClassLoader(String classPath) { scanPath(classPath); readExcludedPackages(); } private void scanPath(String classPath) { String separator= System.getProperty("path.separator"); fPathItems= new Vector(10); StringTokenizer st= new StringTokenizer(classPath, separator); while (st.hasMoreTokens()) { fPathItems.addElement(st.nextToken()); } }
public URL getResource(String name) { return ClassLoader.getSystemResource(name); }
public InputStream getResourceAsStream(String name) { return ClassLoader.getSystemResourceAsStream(name); }
public boolean isExcluded(String name) { for (int i= 0; i < fExcluded.size(); i++) { if (name.startsWith((String) fExcluded.elementAt(i))) { return true; } } return false; }
public synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
Class c= findLoadedClass(name); if (c != null) return c; // // Delegate the loading of excluded classes to the // standard class loader. // if (isExcluded(name)) { try { c= findSystemClass(name); return c; } catch (ClassNotFoundException e) { // keep searching } } if (c == null) { byte[] data= lookupClassData(name); if (data == null) throw new ClassNotFoundException(); System.out.println(data.length); try { c= defineClass(name, data, 0, data.length); } catch (Exception e) { e.printStackTrace(); } } if (resolve) resolveClass(c); return c; }
搂主是不是拿oracle的jdbc driver访问的sqlserver数据库 或者mysql数据库而且sql也写错了 String sql = "select * from table";
肯定没有,再说sql写错也不会出 java.sql.SQLException: No suitable driver at java.sql.DriverManager.getConnection(DriverManager.java:532) at java.sql.DriverManager.getConnection(DriverManager.java:171) at lib.TestRun.main(TestRun.java:42) 再说,我这里除了oracle没有其他的DB了
这个是不是说明了.你的程序中有错误.或者路径问题.
如果可以.你把主要的source拿来,看看!
要不,Parse Source?
/*
* Created on 2004/01/09
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package lib;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;/**
* @author
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/public class LibraryClassLoader extends ClassLoader {
/** scanned class path */
private Vector fPathItems;
/** default excluded paths */
private String[] defaultExclusions= {
"junit.framework.",
"junit.extensions.",
"junit.runner."
};
/** name of excluded properties file */
static final String EXCLUDED_FILE= "excluded.properties";
/** excluded paths */
private Vector fExcluded;
/**
* Constructs a TestCaseLoader. It scans the class path
* and the excluded package paths
*/
public LibraryClassLoader() {
this(System.getProperty("java.class.path"));
}
/**
* Constructs a TestCaseLoader. It scans the class path
* and the excluded package paths
*/
public LibraryClassLoader(String classPath) {
scanPath(classPath);
readExcludedPackages();
} private void scanPath(String classPath) {
String separator= System.getProperty("path.separator");
fPathItems= new Vector(10);
StringTokenizer st= new StringTokenizer(classPath, separator);
while (st.hasMoreTokens()) {
fPathItems.addElement(st.nextToken());
}
}
public URL getResource(String name) {
return ClassLoader.getSystemResource(name);
}
public InputStream getResourceAsStream(String name) {
return ClassLoader.getSystemResourceAsStream(name);
}
public boolean isExcluded(String name) {
for (int i= 0; i < fExcluded.size(); i++) {
if (name.startsWith((String) fExcluded.elementAt(i))) {
return true;
}
}
return false;
}
public synchronized Class loadClass(String name, boolean resolve)
throws ClassNotFoundException {
Class c= findLoadedClass(name);
if (c != null)
return c;
//
// Delegate the loading of excluded classes to the
// standard class loader.
//
if (isExcluded(name)) {
try {
c= findSystemClass(name);
return c;
} catch (ClassNotFoundException e) {
// keep searching
}
}
if (c == null) {
byte[] data= lookupClassData(name);
if (data == null)
throw new ClassNotFoundException();
System.out.println(data.length);
try {
c= defineClass(name, data, 0, data.length);
} catch (Exception e) {
e.printStackTrace();
}
}
if (resolve)
resolveClass(c);
return c;
}
private byte[] lookupClassData(String className) throws ClassNotFoundException {
byte[] data= null;
for (int i= 0; i < fPathItems.size(); i++) {
String path= (String) fPathItems.elementAt(i);
String fileName= className.replace('.', '/')+".class";
if (isJar(path)) {
data= loadJarData(path, fileName);
} else {
data= loadFileData(path, fileName);
}
if (data != null)
return data;
}
throw new ClassNotFoundException(className);
}
boolean isJar(String pathEntry) {
return pathEntry.endsWith(".jar") || pathEntry.endsWith(".zip");
} private byte[] loadFileData(String path, String fileName) {
File file= new File(path, fileName);
if (file.exists()) {
return getClassData(file);
}
return null;
}
private byte[] getClassData(File f) {
try {
FileInputStream stream= new FileInputStream(f);
ByteArrayOutputStream out= new ByteArrayOutputStream(1000);
byte[] b= new byte[1000];
int n;
while ((n= stream.read(b)) != -1)
out.write(b, 0, n);
stream.close();
out.close();
return out.toByteArray(); } catch (IOException e) {
}
return null;
} private byte[] loadJarData(String path, String fileName) {
ZipFile zipFile= null;
InputStream stream= null;
File archive= new File(path);
if (!archive.exists())
return null;
try {
zipFile= new ZipFile(archive);
} catch(IOException io) {
return null;
}
ZipEntry entry= zipFile.getEntry(fileName);
if (entry == null)
return null;
int size= (int) entry.getSize();
try {
stream= zipFile.getInputStream(entry);
byte[] data= new byte[size];
int pos= 0;
while (pos < size) {
int n= stream.read(data, pos, data.length - pos);
pos += n;
}
zipFile.close();
return data;
} catch (IOException e) {
} finally {
try {
if (stream != null)
stream.close();
} catch (IOException e) {
}
}
return null;
}
private void readExcludedPackages() {
fExcluded= new Vector(10);
for (int i= 0; i < defaultExclusions.length; i++)
fExcluded.addElement(defaultExclusions[i]);
InputStream is= getClass().getResourceAsStream(EXCLUDED_FILE);
if (is == null)
return;
Properties p= new Properties();
try {
p.load(is);
}
catch (IOException e) {
return;
} finally {
try {
is.close();
} catch (IOException e) {
}
}
for (Enumeration e= p.propertyNames(); e.hasMoreElements(); ) {
String key= (String)e.nextElement();
if (key.startsWith("excluded.")) {
String path= p.getProperty(key);
path= path.trim();
if (path.endsWith("*"))
path= path.substring(0, path.length()-1);
if (path.length() > 0)
fExcluded.addElement(path);
}
}
}
}下面是测试用main方法
/*
* Created on 2004/01/12
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package lib;import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestRun { public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver";
LibraryClassLoader ld = new LibraryClassLoader("F:/bigdoll/testejb/ora/classes12.zip");
Class c;
try {
c = ld.loadClass(driver, false);
DriverManager.registerDriver((Driver)c.newInstance());
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@172.16.10.4:1521:dbname","user","pass");
String sql = "select * from table";
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static final String EXCLUDED_FILE= "excluded.properties";
所以java/lang/Object这个类就会找不到static final String EXCLUDED_FILE= "excluded.properties";
这个文件中肯定是java的classpath的包的设置之类的东西
excluded.1=com.sun.*
excluded.2=org.omg.*
excluded.3=javax.*
excluded.4=sunw.*
excluded.5=java.*
excluded.6=org.w3c.dom.*
excluded.7=org.xml.sax.*
excluded.8=net.jini.*
这就是exclude.properties中的全部内容
那我的其他的类怎么装入的?
System.getProperty("java.class.path") 就是系统的java.class.path参数
set java.class.path="... ..." 就是这样了另一个就是这个文件了
static final String EXCLUDED_FILE= "excluded.properties";不过这个程序没问题啊
你的excluded.properties是放在包含LibraryClassLoader.class这个类文件的目录了么
因为LibraryClassLoader扩展了ClassLoader
所以调用这个LibraryClassLoader的defineClass
而defineClass需要载入java.lang.Object类
所以也是应该调用LibraryClassLoader这个ClassLoader载入java.lang.Object
这就是出错的原因不知道写的情不清楚
classloader使用的时机是什么呀
为什么要自己做一个这样的逻辑呢
我没有用到过,也不知道这是干什么
请给说一下好么,谢谢!!
String sql = "select * from table";
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:532)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at lib.TestRun.main(TestRun.java:42)
再说,我这里除了oracle没有其他的DB了
比如数据库用的9i 驱动程序用的oracle7的驱动这段程序不会有问题的 我测试过了
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password); 2、DB2数据库
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample为你的数据库名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password); 3、Sql Server7.0/2000数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password); 4、Sybase数据库
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";
//myDB为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps); 5、Informix数据库
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url =
"jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";
//myDB为数据库名
Connection conn= DriverManager.getConnection(url); 6、MySQL数据库
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//myDB为数据库名
Connection conn= DriverManager.getConnection(url); 7、PostgreSQL数据库
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB"
//myDB为数据库名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
我使用DriverManager.getDrivers()来查看,发现Driver并没有被注册进去,而是在注册过程中被skip掉了。原因好像还是DriverManager使用ClassLoader装入该驱动的时候没找到。
classloader使用的时机是什么呀
为什么要自己做一个这样的逻辑呢
我没有用到过,也不知道这是干什么
请给说一下好么,谢谢!!
你要是有特别的需要 你就扩展他比如安全方面 你的classloader加过密 只有你的classloader能解密
当然需要自己写了
我如果不用ClassLoader去装入,直接用Class.forName,然后指定驱动的位置也没有问题。就是这样使用ClassLoader时候才出错,你不是在你的Classpath里设置了classes12.zip吧?!
skipping: driver[className=oracle.jdbc.driver.OracleDriver,oracle.jdbc.driver.OracleDriver@360be0]
DriverManager.getConnection("jdbc:oracle:thin:@172.16.10.4:1521:sd02db")
skipping: driver[className=oracle.jdbc.driver.OracleDriver,oracle.jdbc.driver.OracleDriver@18d107f]
skipping: driver[className=oracle.jdbc.driver.OracleDriver,oracle.jdbc.driver.OracleDriver@360be0]
getConnection: no suitable driver
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:532)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at lib.TestRun.main(TestRun.java:46)
SQLException: SQLState(08001)
加到classpath里就不是实现动态加载驱动了,那样还费这么大劲干吗?
直接class.forName不就完了。
我是要动态的得到classes12.zip的位置,并加在这个oracleDriver,来连结DB。
如果classes12.zip的位置固定,就没有意义了。当然,如果能把运行时的classpath动态改了也可以,不过好像还没找到方法。
Hit uncaught exception java.lang.ClassNotFoundException
是在URLClassLoader类中抛出的!程序在以前运行好用,我将jbulider10的reg361.txt宁文件放在指定的目录下。程序编译无问题,但运行就有异常!
TO:xzwsun(异常) 我用的是eclipse