简体   繁体   English

org.postgres.Driver类异常-初学者

[英]org.postgres.Driver Class Exception - Beginner

thanks for any help, I am a complete noob here but trying to learn. 感谢您的帮助,我在这里是一个完整的菜鸟,但尝试学习。 the below code is simply trying to create a connection to a database. 下面的代码只是试图创建与数据库的连接。 I am getting this error back: 我收到此错误:

 java.lang.ClassNotFoundException: org.postgres.Driver 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 CreateDB.main(CreateDB.java:11) java.lang.ClassNotFoundException: org.postgres.Driver 

having researched online the solution i come across is to check the library is added to to build path. 在线研究了我遇到的解决方案是检查是否已将库添加到构建路径。 i can confirm that i have (I think). 我可以确认我有(我认为)。 to do this i right clicked the prject -> Properties -> Java Build Path -> external JARS and navigated to the postgresql - 42.41.4..jar which is located in "...\\eclipse-workspace\\libraries\\" I can see that the library is added under referenced libraries within the project. 为此,我右键单击prject-> Properties-> Java Build Path-> external JARS并导航到位于“ ... \\ eclipse-workspace \\ libraries \\”中的postgresql-42.41.4..jar,我可以看到该库已添加到项目中引用的库下。 not a clue now im stuck. 现在没有任何线索我被困住了。 any help is genuinely appreciated. 真诚地感谢您的任何帮助。

I am learning from here https://www.tutorialspoint.com/postgresql/postgresql_java.htm 我正在从这里学习https://www.tutorialspoint.com/postgresql/postgresql_java.htm

import java.sql.Connection;
import java.sql.DriverManager;

public class CreateDB {
    public static void main(String Args[]) {
        Connection c = null;
        try {
            Class.forName("org.postgres.Driver");
            c = DriverManager.getConnection("jdbc.postgresql://localhost:1080/VEM", "postgres", "Diablo12" ); //creates connection with U&P
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getClass().getName() +": " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Database opened successfully");
    }
}

The correct driver name is: org.postgresql.Driver . 正确的驱动程序名称是: org.postgresql.Driver So your code should look like this: 因此,您的代码应如下所示:

import java.sql.Connection;
import java.sql.DriverManager;

public class CreateDB {
    public static void main(String args[]) {
        Connection c = null;
        try {
            Class.forName("org.postgresql.Driver");
            c = DriverManager.getConnection("jdbc.postgresql://localhost:1080/VEM", "postgres", "Diablo12" ); //creates connection with U&P
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getClass().getName() +": " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Database opened successfully");
   }
}

The false driver name causes java to throw the ClassNotFoundException because the class with the passed name could not be found in the classpath. 错误的驱动程序名称会导致Java引发ClassNotFoundException因为在类路径中找不到具有传递名称的类。 Since Java 6 the loading of the driver via Class.forName() is not needed any more, like a_horse_with_no_name pointed out in his comment. 从Java 6开始,不再需要通过Class.forName()加载驱动程序,就像他的注释中指出的a_horse_with_no_name一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Class 未找到异常 org//h2//驱动程序 - Class Not Found Exception org//h2//Driver Spring JPA - Postgres - 无法加载驱动程序 class:org.ZE4728F444B2428BC8E3F80ADF9 - Spring JPA - Postgres - Cannot load driver class: org.postgresql.Driver 在eclipse插件中不断获取Postgres驱动程序的Class Not Found异常 - Keeps getting Class Not Found Exception for Postgres driver in eclipse plugin failed.org.hibernate.exception.JDBCConnectionException:使用PostGres Hibernate调用Driver#connect时出错 - failed.org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect with PostGres Hibernate 属性“driverClassName”抛出异常; 无法加载 JDBC 驱动程序类 [org.postgresql.Driver] - Property 'driverClassName' threw exception; Could not load JDBC driver class [org.postgresql.Driver] h2数据库class.forname(“ org.h2.Driver”)的ClassNotFound异常 - ClassNotFound exception for h2 database class.forname(“org.h2.Driver”) Hibernate + PostgreSQL 抛出异常:找不到 JDBC 驱动程序类:org.postgresql.Drive - Hibernate + PostgreSQL throws an Exception: JDBC Driver class not found: org.postgresql.Drive 无法加载类[org.postgresql.Driver] - Unable to load class [org.postgresql.Driver] 无法加载驱动程序 class:org.postgresql.Driver - Cannot load driver class: org.postgresql.Driver 无法加载驱动程序 class:org.h2.Driver - Cannot load driver class: org.h2.Driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM