java.lang.ClassNotFoundException: org.postgresql.Driver, Android [英] java.lang.ClassNotFoundException: org.postgresql.Driver, Android
问题描述
我在 Windows 上运行 Eclipse.
I am running Eclipse on Windows.
遵循本教程,我下载了 JDBC4,使用 Project 将其添加到我的构建路径中>属性>添加外部 JAR,浏览文件,它起作用了(.classpath
文件显示了正确的 lib 路径).
Following this tutorial I downloaded JDBC4, added it to my build path using Project>Properties>add External JAR, browsed for the file, it worked (.classpath
file shows the correct lib path).
该包出现在我的 Referenced Libraries 文件夹中,所以我继续本教程.
The package appears in my Referenced Libraries folder, so I continue the tutorial.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
....
public void open ()
{
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
}
我认为就这么简单,但是我遇到了这个以
I think it would be as simple as that but I get hit with this big long stack trace starting with
java.lang.ClassNotFoundException: org.postgresql.Driver
(如果需要,我可以提供更多)
(I can provide more if needed)
我试过 include org.postgresql.*;
但这也没有帮助.我也尝试过 JDBC3,但也没有运气.
I tried include org.postgresql.*;
but that didn't help either. I have also tried the JDBC3 but no luck there either.
我查看了 Driver JDBC PostgreSQL with Android,它提供了一个模糊的答案,说我只使用 HTTP+JSON 会更好.我从来没有用过.
I looked at Driver JDBC PostgreSQL with Android which provided a vague answer saying I would be better off just using HTTP+JSON. Which I have never used.
我是 Android、postgresql、Web 开发的新手,所以如果能提供简单的答案,我们将不胜感激.
I'm brand new to Android, postgresql, web development, so a simple answer would be appreciated.
推荐答案
您需要在您的项目中添加 PostgreSQL JDBC 驱动程序,如 search.maven.org.
You need to add the PostgreSQL JDBC Driver in your project as mentioned in search.maven.org.
Gradle:
implementation 'org.postgresql:postgresql:42.2.10'
Maven:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.10</version>
</dependency>
您还可以下载 JAR 并导入到您的项目中手动.
You can also download the JAR and import to your project manually.
注意:不推荐使用此答案中使用的编译.根据 3 替换为实现.
NOTE: Compile as used in this answer is deprecated. Replace with implementation as per 3.
这篇关于java.lang.ClassNotFoundException: org.postgresql.Driver, Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!