各位高手救命 为什么下面的代码总是抛错 java.sql.SQLException: No suitable driver found for jdbc:Cache://localhost:1972/SAMPLES可以确认 jdbc string 是正确的
jar包位置是正确的import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;public class GetConnection {
public static Connection GetConn(String JarName,String ClassName,String ConnectString,String UserName ,String PassWord){
Connection conn = null;
Class c = null;
Object o = null;
try {
System.out.println(JarName);
JarClassLoader jarLoader = new JarClassLoader (JarName);
/* Load the class from the jar file and resolve it. */
c = jarLoader.loadClass (ClassName, true);
try {
o = c.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Class.forName(ClassName, true, c.getClassLoader());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
Driver dr = (Driver)o;
DriverManager.registerDriver(dr);
System.out.println( dr.getClass().toString());
System.out.println( dr.jdbcCompliant() );
System.out.println( "UserName : " + UserName);
System.out.println( "PassWord : " + PassWord);
conn = DriverManager.getConnection( ConnectString,UserName,PassWord);
}
catch (SQLException ex)
{
ex.printStackTrace();
} if (conn!=null)
return conn;
else
return null;
}
public static void main(String [] args){
GetConn("c:/CacheDB.jar","com.intersys.jdbc.CacheDriver" , "jdbc:Cache://localhost:1972/SAMPLES","_system","sys");
}
}public class JarClassLoader extends MultiClassLoader {
private JarResources jarResources; public JarClassLoader(String jarName) {
// Create the JarResource and suck in the jar file.
jarResources = new JarResources(jarName);
} protected byte[] loadClassBytes(String className) {
// Support the MultiClassLoader's class name munging facility.
className = formatClassName(className);
// Attempt to get the class data from the JarResource.
return (jarResources.getResource(className));
}
}
import java.io.*;
import java.util.*;
import java.util.zip.*;/**
* JarResources: JarResources maps all resources included in a
* Zip or Jar file. Additionaly, it provides a method to extract one
* as a blob.
*/
public final class JarResources
{ // external debug flag
public boolean debugOn=false; // jar resource mapping tables
private Hashtable htSizes=new Hashtable();
private Hashtable htJarContents=new Hashtable(); // a jar file
private String jarFileName; /**
* creates a JarResources. It extracts all resources from a Jar
* into an internal hashtable, keyed by resource names.
* @param jarFileName a jar or zip file
*/
public JarResources(String jarFileName)
{
this.jarFileName=jarFileName;
init();
} /**
* Extracts a jar resource as a blob.
* @param name a resource name.
*/
public byte[] getResource(String name)
{
return (byte[])htJarContents.get(name);
} /** initializes internal hash tables with Jar file resources. */
private void init()
{
try
{
// extracts just sizes only.
ZipFile zf=new ZipFile(jarFileName);
Enumeration e=zf.entries();
while (e.hasMoreElements())
{
ZipEntry ze=(ZipEntry)e.nextElement(); if (debugOn)
{
System.out.println(dumpZipEntry(ze));
} htSizes.put(ze.getName(),new Integer((int)ze.getSize()));
}
zf.close(); // extract resources and put them into the hashtable.
FileInputStream fis=new FileInputStream(jarFileName);
BufferedInputStream bis=new BufferedInputStream(fis);
ZipInputStream zis=new ZipInputStream(bis);
ZipEntry ze=null;
while ((ze=zis.getNextEntry())!=null)
{
if (ze.isDirectory())
{
continue;
} if (debugOn)
{
System.out.println("ze.getName()="+ze.getName()+
","+"getSize()="+ze.getSize() );
} int size=(int)ze.getSize();
// -1 means unknown size.
if (size==-1)
{
size=((Integer)htSizes.get(ze.getName())).intValue();
} byte[] b=new byte[(int)size];
int rb=0;
int chunk=0;
while (((int)size - rb) > 0)
{
chunk=zis.read(b,rb,(int)size - rb);
if (chunk==-1)
{
break;
}
rb+=chunk;
} // add to internal resource hashtable
htJarContents.put(ze.getName(),b); if (debugOn)
{
System.out.println( ze.getName()+" rb="+rb+
",size="+size+
",csize="+ze.getCompressedSize() );
}
}
}
catch (NullPointerException e)
{
System.out.println("done.");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
} /**
* Dumps a zip entry into a string.
* @param ze a ZipEntry
*/
private String dumpZipEntry(ZipEntry ze)
{
StringBuffer sb=new StringBuffer();
if (ze.isDirectory())
{
sb.append("d ");
}
else
{
sb.append("f ");
} if (ze.getMethod()==ZipEntry.STORED)
{
sb.append("stored ");
}
else
{
sb.append("defalted ");
} sb.append(ze.getName());
sb.append("\t");
sb.append(""+ze.getSize());
if (ze.getMethod()==ZipEntry.DEFLATED)
{
sb.append("/"+ze.getCompressedSize());
} return (sb.toString());
} } // End of JarResources class.
jar包位置是正确的import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;public class GetConnection {
public static Connection GetConn(String JarName,String ClassName,String ConnectString,String UserName ,String PassWord){
Connection conn = null;
Class c = null;
Object o = null;
try {
System.out.println(JarName);
JarClassLoader jarLoader = new JarClassLoader (JarName);
/* Load the class from the jar file and resolve it. */
c = jarLoader.loadClass (ClassName, true);
try {
o = c.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Class.forName(ClassName, true, c.getClassLoader());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
Driver dr = (Driver)o;
DriverManager.registerDriver(dr);
System.out.println( dr.getClass().toString());
System.out.println( dr.jdbcCompliant() );
System.out.println( "UserName : " + UserName);
System.out.println( "PassWord : " + PassWord);
conn = DriverManager.getConnection( ConnectString,UserName,PassWord);
}
catch (SQLException ex)
{
ex.printStackTrace();
} if (conn!=null)
return conn;
else
return null;
}
public static void main(String [] args){
GetConn("c:/CacheDB.jar","com.intersys.jdbc.CacheDriver" , "jdbc:Cache://localhost:1972/SAMPLES","_system","sys");
}
}public class JarClassLoader extends MultiClassLoader {
private JarResources jarResources; public JarClassLoader(String jarName) {
// Create the JarResource and suck in the jar file.
jarResources = new JarResources(jarName);
} protected byte[] loadClassBytes(String className) {
// Support the MultiClassLoader's class name munging facility.
className = formatClassName(className);
// Attempt to get the class data from the JarResource.
return (jarResources.getResource(className));
}
}
import java.io.*;
import java.util.*;
import java.util.zip.*;/**
* JarResources: JarResources maps all resources included in a
* Zip or Jar file. Additionaly, it provides a method to extract one
* as a blob.
*/
public final class JarResources
{ // external debug flag
public boolean debugOn=false; // jar resource mapping tables
private Hashtable htSizes=new Hashtable();
private Hashtable htJarContents=new Hashtable(); // a jar file
private String jarFileName; /**
* creates a JarResources. It extracts all resources from a Jar
* into an internal hashtable, keyed by resource names.
* @param jarFileName a jar or zip file
*/
public JarResources(String jarFileName)
{
this.jarFileName=jarFileName;
init();
} /**
* Extracts a jar resource as a blob.
* @param name a resource name.
*/
public byte[] getResource(String name)
{
return (byte[])htJarContents.get(name);
} /** initializes internal hash tables with Jar file resources. */
private void init()
{
try
{
// extracts just sizes only.
ZipFile zf=new ZipFile(jarFileName);
Enumeration e=zf.entries();
while (e.hasMoreElements())
{
ZipEntry ze=(ZipEntry)e.nextElement(); if (debugOn)
{
System.out.println(dumpZipEntry(ze));
} htSizes.put(ze.getName(),new Integer((int)ze.getSize()));
}
zf.close(); // extract resources and put them into the hashtable.
FileInputStream fis=new FileInputStream(jarFileName);
BufferedInputStream bis=new BufferedInputStream(fis);
ZipInputStream zis=new ZipInputStream(bis);
ZipEntry ze=null;
while ((ze=zis.getNextEntry())!=null)
{
if (ze.isDirectory())
{
continue;
} if (debugOn)
{
System.out.println("ze.getName()="+ze.getName()+
","+"getSize()="+ze.getSize() );
} int size=(int)ze.getSize();
// -1 means unknown size.
if (size==-1)
{
size=((Integer)htSizes.get(ze.getName())).intValue();
} byte[] b=new byte[(int)size];
int rb=0;
int chunk=0;
while (((int)size - rb) > 0)
{
chunk=zis.read(b,rb,(int)size - rb);
if (chunk==-1)
{
break;
}
rb+=chunk;
} // add to internal resource hashtable
htJarContents.put(ze.getName(),b); if (debugOn)
{
System.out.println( ze.getName()+" rb="+rb+
",size="+size+
",csize="+ze.getCompressedSize() );
}
}
}
catch (NullPointerException e)
{
System.out.println("done.");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
} /**
* Dumps a zip entry into a string.
* @param ze a ZipEntry
*/
private String dumpZipEntry(ZipEntry ze)
{
StringBuffer sb=new StringBuffer();
if (ze.isDirectory())
{
sb.append("d ");
}
else
{
sb.append("f ");
} if (ze.getMethod()==ZipEntry.STORED)
{
sb.append("stored ");
}
else
{
sb.append("defalted ");
} sb.append(ze.getName());
sb.append("\t");
sb.append(""+ze.getSize());
if (ze.getMethod()==ZipEntry.DEFLATED)
{
sb.append("/"+ze.getCompressedSize());
} return (sb.toString());
} } // End of JarResources class.
private Hashtable classes = new Hashtable();
private char classNameReplacementChar;protected boolean monitorOn = false;
protected boolean sourceMonitorOn = true;//---------- Initialization ------------------------------
public MultiClassLoader() {
}
//---------- Superclass Overrides ------------------------
/**
* This is a simple version for external clients since they
* will always want the class resolved before it is returned
* to them.
*/
public Class loadClass(String className) throws ClassNotFoundException {
return (loadClass(className, true));
}
//---------- Abstract Implementation ---------------------
public synchronized Class loadClass(String className,
boolean resolveIt) throws ClassNotFoundException { Class result;
byte[] classBytes;
monitor(">> MultiClassLoader.loadClass(" + className + ", " + resolveIt + ")"); //----- Check our local cache of classes
result = (Class)classes.get(className);
if (result != null) {
monitor(">> returning cached result.");
return result;
} //----- Check with the primordial class loader
try {
result = super.findSystemClass(className);
monitor(">> returning system class (in CLASSPATH).");
return result;
} catch (ClassNotFoundException e) {
monitor(">> Not a system class.");
} //----- Try to load it from preferred source
// Note loadClassBytes() is an abstract method
classBytes = loadClassBytes(className);
if (classBytes == null) {
throw new ClassNotFoundException();
} //----- Define it (parse the class file)
result = defineClass(className, classBytes, 0, classBytes.length);
if (result == null) {
throw new ClassFormatError();
} //----- Resolve if necessary
if (resolveIt) resolveClass(result); // Done
classes.put(className, result);
monitor(">> Returning newly loaded class.");
return result;
}
//---------- Public Methods ------------------------------
/**
* This optional call allows a class name such as
* "COM.test.Hello" to be changed to "COM_test_Hello",
* which is useful for storing classes from different
* packages in the same retrival directory.
* In the above example the char would be '_'.
*/
public void setClassNameReplacementChar(char replacement) {
classNameReplacementChar = replacement;
}
//---------- Protected Methods ---------------------------
protected abstract byte[] loadClassBytes(String className);protected String formatClassName(String className) {
if (classNameReplacementChar == '\u0000') {
// '/' is used to map the package to the path
return className.replace('.', '/') + ".class";
} else {
// Replace '.' with custom char, such as '_'
return className.replace('.',
classNameReplacementChar) + ".class";
}
}
protected void monitor(String text) {
if (monitorOn) print(text);
}
//--- Std
protected static void print(String text) {
System.out.println(text);
}} // End class[/code]
class com.intersys.jdbc.CacheDriver
true
UserName : _system
PassWord : sys
java.sql.SQLException: No suitable driver found for jdbc:Cache://localhost:1972/SAMPLES
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.kioo.JDBC.GetConnection.GetConn(GetConnection.java:40)
at com.kioo.JDBC.GetConnection.main(GetConnection.java:55)