小编典典

Class.forName(“ org.postgresql.Driver”)错误

java

我上课简单。然后,我尝试编译并运行它,看到ClassNotFoundException。

import java.sql.*;

public class DBProcessor{

private static String serverAdres = "127.0.0.1:5432";

private static String DBname = "dota";
private static String clientName = "postgres";
private static String password = "master";
private static Connection connection;

public static void connect() {
    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        System.err.println("Where is your PostgreSQL JDBC Driver? "
                + "Include in your library path!");
        e.printStackTrace();
    }

    try {
        connection = DriverManager.getConnection("jdbc:postgresql://" + serverAdres + "/" + DBname, clientName, password);

    } catch (SQLException e) {
        System.err.println("Connection Failed! Check output console");
        e.printStackTrace();
    }

    if (connection == null) {
        System.err.println("Failed to make connection!");
    }
}

public static void main(String[] args){
    DBProcessor db = new DBProcessor();
    db.connect();
}
}

我正在使用Windows cmd:

C:\rmi2>javac -classpath postgresql.jar DBProcessor.java
C:\rmi2>java DBProcessor
Where is your PostgreSQL JDBC Driver? Include in your library path!
java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at DBProcessor.connect(DBProcessor.java:12)
at DBProcessor.main(DBProcessor.java:34)
Connection Failed! Check output console
java.sql.SQLException: No suitable driver found for jdbc:postgresql://127.0.0.1:
5432/dota
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DBProcessor.connect(DBProcessor.java:20)
at DBProcessor.main(DBProcessor.java:34)
Failed to make connection!

我做错了什么?因此,这是postgrsql库
https://www.dropbox.com/s/idx5l0kub5rn1b8/postgresql.jar?dl=0


阅读 1163

收藏
2020-11-26

共1个答案

小编典典

在命令行上,

java DBProcessor

应该

java -cp .;postgresql.jar DBProcessor

而且,因为它包含java.sql.Driver,所以当您使用它时,可以将其删除

// try {
//    Class.forName("org.postgresql.Driver");
// } catch (ClassNotFoundException e) {
//    System.err.println("Where is your PostgreSQL JDBC Driver? "
//            + "Include in your library path!");
//    e.printStackTrace();
// }

根据DriverManagerJavadoc,

增强了这些DriverManager方法getConnectiongetDrivers以支持Java Standard Edition
Service Provider机制。JDBC 4.0驱动程序必须包含该文件META- INF/services/java.sql.Driver。该文件包含的JDBC驱动程序实现的名称java.sql.Driver。例如,要加载my.sql.Driver类,该META- INF/services/java.sql.Driver文件将包含以下条目:

 my.sql.Driver

应用程序不再需要使用显式加载JDBC驱动程序Class.forName()。当前使用JDBC驱动程序加载的现有程序Class.forName()将继续运行,而无需进行任何修改。

2020-11-26