日期:2014-05-18  浏览次数:20642 次

这是什么原因?
package oa.com.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {
private static String drivername;
private static String url;
private static String username;
private static String password;
private static String initialSize;
private static String maxActive;
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
static{

Properties p = new Properties();
try {
p.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));

drivername = p.getProperty("driverClassName");
url = p.getProperty("url");
username = p.getProperty("username");
password = p.getProperty("password");
initialSize = p.getProperty("initialSize");
maxActive = p.getProperty("maxActive");
Class.forName(drivername);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("读取文件失败");
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("加载文件失败");
}
}
public static Connection getConnection(){
Connection con = tl.get();

if(con ==null){
try {
//连接数据库

con = DriverManager.getConnection(url,username,password);
//放入线程池
tl.set(con);

} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("获取连接失败");
}
}
return con;
}
public static void colseConnection(){
Connection con = tl.get();
if(con != null){
try {
con.close();
tl.set(null);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception{
Connection con = getConnection();
System.out.println(con);
}
}
=============================================================================================
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)