gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:16:43 24 4
gpt4 key购买 nike

我有简单的类(class)。然后我尝试编译并运行它,我看到了 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 命令:

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

最佳答案

在命令行中,这个

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();
// }

根据 DriverManager Java文档,

The DriverManager methods getConnection and getDrivers have 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.Driver class, the META-INF/services/java.sql.Driver file 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.

关于java - Class.forName ("org.postgresql.Driver") 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26766843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com