Class.forName("org.postgresql.Driver")错误 [英] Class.forName("org.postgresql.Driver") error

查看:187
本文介绍了Class.forName("org.postgresql.Driver")错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have simple class. Then I try to compile and run it I see 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:

I'm using 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

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

推荐答案

在命令行上,此

java DBProcessor

应该是

java -cp .;postgresql.jar DBProcessor

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

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

// 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 Javadoc ,

Per the DriverManager Javadoc,

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

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

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

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.

这篇关于Class.forName("org.postgresql.Driver")错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆