装载 JDBC/ODBC 驱动程序失败
我已经导入了mysql-connector-java-5.1.6-bin.jar包还是抛出:
db.DBConnection@1220b36装载 JDBC/ODBC 驱动程序失败。
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
加载驱动的类
public class DBConnection{
private String url = "jdbc:mysql://localhost:3306/dahee2008";
private String username = "root";
private String password = "123456";
private Connection conn;
public Connection getConnection(){
// 加载驱动程序以连接数据库
try {
Class.forName( "com.mysql.jdbc.Driver" );
conn = DriverManager.getConnection(url, username, password );
}
// 捕获加载驱动程序异常
catch ( ClassNotFoundException cnfex ) {
System.err.println("装载 JDBC/ODBC 驱动程序失败。");
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
// 捕获连接数据库异常
catch ( SQLException sqlex ) {
System.err.println( "无法连接数据库" );
sqlex.printStackTrace();
System.exit( 1 ); // terminate program
}
return conn;
}
}