java Class.forName("org.postgresql.Driver") 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26766843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:33:41  来源:igfitidea点击:

Class.forName("org.postgresql.Driver") error

javajdbc

提问by otopba

I have simple class. Then I try to compile and run it I see ClassNotFoundException.

我有简单的课。然后我尝试编译并运行它,我看到 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();
}
}

I'm using Windows cmd:

我正在使用 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.run(Unknown Source)
at java.net.URLClassLoader.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!

What I'm doing wrong? So, this is postgrsql library https://www.dropbox.com/s/idx5l0kub5rn1b8/postgresql.jar?dl=0

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

回答by Elliott Frisch

On the command line, this

在命令行上,这个

java DBProcessor

should be

应该

java -cp .;postgresql.jar DBProcessor

And, because it includes java.sql.Driver, when you have it working you could remove

而且,因为它包括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();
// }

Per the DriverManagerJavadoc,

根据DriverManagerJavadoc,

The DriverManagermethods getConnectionand getDrivershave been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver. For example, to load the my.sql.Driverclass, the META-INF/services/java.sql.Driverfile would contain the entry:

 my.sql.Driver

Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName()will continue to work without modification.

DriverManager方法getConnectiongetDrivers已得到增强,支持Java标准版服务提供商的机制。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

应用程序不再需要使用Class.forName(). 当前使用加载 JDBC 驱动程序的现有程序Class.forName()将继续工作而无需修改。

回答by BackGround Soft

You need to link the driver to your IDE. if you're using Netbeans, right click on Libraries, Add jar/folder then browse to the driver.

您需要将驱动程序链接到您的 IDE。如果您使用的是 Netbeans,请右键单击库,添加 jar/文件夹,然后浏览到驱动程序。

回答by Reagan Ochora

If the postgresql.jar file is not accessible to the Java runtime environment you will get a 'java.lang.ClassNotFoundException: org.postgresql.Driver' exception.

如果 Java 运行时环境无法访问 postgresql.jar 文件,您将收到“java.lang.ClassNotFoundException: org.postgresql.Driver”异常。

The easiest way to make the postgresql jdbc driver accessible to your applications is to install it as a Java Extension. The only thing you need to do is cp, mv or ln the postgresql.jar file into the Java extension directory. There is no need to set CLASSPATH if you do this. The Java extension directory is $JAVA_HOME/jre/lib/ext ($JAVA_HOME on my system is /usr/java/jdk1.3) or for Windows C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext

使应用程序可以访问 postgresql jdbc 驱动程序的最简单方法是将其安装为 Java 扩展。您唯一需要做的就是将 postgresql.jar 文件 cp、mv 或 ln 放入 Java 扩展目录中。如果您这样做,则无需设置 CLASSPATH。Java 扩展目录是 $JAVA_HOME/jre/lib/ext(我系统上的 $JAVA_HOME 是 /usr/java/jdk1.3)或 Windows C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext

This makes life REALLY easy; I didn't have to set CLASSPATH at all in my environment again!

这让生活变得非常简单;我根本不需要在我的环境中设置 CLASSPATH!