gpt4 book ai didi

java - 安卓java.sql.SQLException : No suitable driver Postgress database query

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

我试图在我的 Android 应用程序中为我的 postgres 数据库使用查询,但我不断收到“java.sql.SQLException:没有合适的驱动程序”错误。

我在依赖项中有驱动程序,所以我不确定发生了什么。

这是使用查询的代码。

public class MainActivity extends Activity {
TextView resultArea;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
resultArea = new TextView(this);
resultArea.setText("Please wait.");
setContentView(resultArea);
new FetchSQL().execute();
}
private class FetchSQL extends AsyncTask<Void,Void,String> {
protected String doInBackground(Void... params) {

String retval = "";
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
retval = e.toString();
}
String url = "jdbc:postgresql://**IPADDRESS**/postgres?user=postgres&password=**PASSWORD**";
Connection conn;
try {
DriverManager.setLoginTimeout(5);
conn = DriverManager.getConnection(url);
Statement st = conn.createStatement();
String sql;
sql = "INSERT INTO Users (username, password) VALUES ('poop','dick');";
System.out.println("*");
ResultSet rs = st.executeQuery(sql);
while(rs.next()) {
retval = rs.getString(1);
}
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
retval = e.toString();
}
return retval;
}
@Override
protected void onPostExecute(String value) {
resultArea.setText(value);
}
}
}

这是我的build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
applicationId "jett.myapplication"
minSdkVersion 8
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile files('E:/Alex Jett/Coding/160/MyApplication/libs/postgresql-9.4-1201.jdbc4.jar')
}

最佳答案

在依赖项中尝试这个:

compile 'org.postgresql:postgresql:9.4-1200-jdbc41'

并删除该行

compile files('E:/Alex Jett/Coding/160/MyApplication/libs/postgresql-9.4-1201.jdbc4.jar')

关于java - 安卓java.sql.SQLException : No suitable driver Postgress database query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30699227/

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