[英]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.