我有一个Java类,它通过JDBC访问MySQL数据库,我在Tomcat上运行的JSP中使用,我得到No Driver Found Exception.
我有一个方法:
private static Statement makeStatement() {
try{
com.mysql.jdbc.Driver d = null;
try{d = new com.mysql.jdbc.Driver();}catch(Exception e){
System.out.println("ERROR BY NEW DRIVER " + e.toString() +
"\n");e.printStackTrace();}
Connection con = DriverManager.getConnection(url, user, password);
return con.createStatement();
}catch(java.sql.SQLException ex){
System.out.println("ERROR IN makeStatement " + "\nERROR - " +
ex.toString() + "\n ERROR CODE:\n " + ex.getErrorCode() +
"\nSQLSTATE:\n " + ex.getSQLStat e());ex.printStackTrace();}
return null;
}
Run Code Online (Sandbox Code Playgroud)
这引发了一个错误.Connection con = DriverManager.getConnection(url, user, password);这是我的catalina.out打印输出:
Received Parameters
ERROR IN makeStatement
ERROR - …Run Code Online (Sandbox Code Playgroud) 我想使用JDBC 4.1驱动程序连接到PostgreSQL数据库.我在以下内容中声明了以下依赖项pom.xml:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1101-jdbc41</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这将postgresql-9.3-1101-jdbc41.jar进入我的类路径.我已经读过,Class.forName()如果指定了驱动程序,则不再需要加载驱动程序META-INF/services/java.sql.Driver.driver-jar有这个文件,但我仍然收到以下错误:
No suitable driver found for jdbc:postgresql://localhost:5432
Run Code Online (Sandbox Code Playgroud)
我只是在测试中调用然后使用mvn test以下命令运行测试:
DriverManager.getConnection("jdbc:postgresql://localhost:5432");
Run Code Online (Sandbox Code Playgroud)
即使我Class.forName()在收到错误之前打电话.如何正确加载驱动程序?