gpt4 book ai didi

java - 错误 : Could not find or load main class JDBCExample

转载 作者:行者123 更新时间:2023-11-29 13:30:24 25 4
gpt4 key购买 nike

你好,我正在尝试在 cmd (Windows) 中运行一个 java 文件:

C:\test>java -cp c:\test\pos​​tgresql-9.3-1102.jdbc41.jar;c:\test JDBCExample

其中 postgresql-9.3-1102.jdbc41.jar 和 JDBCExample 已经在文件夹“test”中。JDBCExample.java 是:


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

public class JDBCExample {

public static void main(String[] args) {

System.out.println("-------- PostgreSQL "
+ "JDBC Connection Testing ------------");

try {

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

} catch (ClassNotFoundException e) {

System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;

}

System.out.println("PostgreSQL JDBC Driver Registered!");

Connection connection = null;

try {

connection = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/testdb", "java_postgres",
"4scoreand7yearsago");

} catch (SQLException e) {

System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;

}

if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}

}

我试图在同一个“测试”文件夹中运行一个简单的“Hello World”java 文件,但它也显示相同的错误。接下来,我还尝试将目录更改为 .java 文件的源文件夹,我真的确定它在 Netbeans 或 Eclipse 中工作得很好,(排序算法等),但仍然在 cmd 中它在编译时说同样的错误. :(

有什么帮助吗?此外,我已经在我的计算机环境变量的路径中添加了一个“C:\Program Files\Java\jdk1.7.0_45\bin;”并添加了一个名为 CLASSPATH 的新文件,它指向一个“C:\

或者你们中的任何一个都可以帮助我找到另一种设置 JDBC 驱动器的方法。谢谢你。 T_T

最佳答案

我的错,这只是编译和运行问题。

/> javac JDBCExample.java
/> java JBDCExample

打扰了大家! xD

关于java - 错误 : Could not find or load main class JDBCExample,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25187161/

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