我正在尝试在我的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')
}
答案 0 :(得分:0)
在依赖项中尝试使用它:
compile 'org.postgresql:postgresql:9.4-1200-jdbc41'
并删除该行
compile files('E:/Alex Jett/Coding/160/MyApplication/libs/postgresql-9.4-1201.jdbc4.jar')