Android应用连接ElephantSQL数据库遇驱动及加密异常求助
Android连接ElephantSQL数据库的驱动及加密算法问题
我正尝试将Android应用连接至ElephantSQL数据库,最初遇到如下错误:
org.postgresql.util.PSQLException: Something unusual has occurred to cause the driver to fail. Please report this exception.
以下是我的database.java文件代码(包含大量已尝试过的注释内容):
try { //Class.forName("org.postgresql.Driver"); //Enumeration<Driver> drivers = DriverManager.getDrivers(); //System.out.printf("\n\n\n1: %s\n\n\n", drivers.nextElement().getClass().getName()); //Class.forName("org.postgresql.Driver"); String url = "jdbc:postgresql://temp.db.elephantsql.com:5432/"; //System.out.printf("\n\n\n2: %s\n\n\n",DriverManager.getDriver(url).getClass().getName()); String username = "redacted"; String password = "redacted"; //Properties props = new Properties(); //props.setProperty("user", username); //props.setProperty("password", password); //props.setProperty("ssl", "true"); Connection c = DriverManager.getConnection(url, username,password); /* String databaseUrl = "YOUR_DATABASE_URL"; connection = DriverManager.getConnection(databaseUrl); DatabaseMetaData metaData = connection.getMetaData(); System.out.println("JDBC Driver Name: " + metaData.getDriverName()); System.out.println("JDBC Driver Version: " + metaData.getDriverVersion()); */ //Connection c = DriverManager.getConnection(database_url); if (c == null) { throw new Exception("Error: DriverManager.getConnection() returned a null object"); }
模块级build.gradle配置
plugins { id 'com.android.application' id 'com.google.gms.google-services' } android { compileSdkVersion 30 defaultConfig { applicationId "com.example.coachal2021" minSdkVersion 28 targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } configurations.all { resolutionStrategy.dependencySubstitution { substitute module('org.hamcrest:hamcrest-core:1.1') with module('junit:junit:4.10') } } dependencies { implementation 'com.google.code.gson:gson:2.8.6' implementation 'com.googlecode.json-simple:json-simple:1.1.1' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.android.volley:volley:1.1.1' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation platform('com.google.firebase:firebase-bom:28.4.0') implementation project(path: ':wahoo_fitness_android_api_1.8.2.0') implementation 'com.google.firebase:firebase-database' implementation 'com.squareup.retrofit2:converter-gson:2.6.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' implementation 'org.postgresql:postgresql:42.7.1' testImplementation files('libs/postgresql-42.6.0.jar') }
项目级build.gradle配置
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:7.2.2' classpath 'com.google.gms:google-services:4.3.10' classpath files('libs/postgresql-42.6.0.jar') // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
我的Java版本为1.8,模块build.gradle中PostgreSQL驱动版本为42.7.1。网上多数解决方案指向URL错误或build.gradle未引入JDBC依赖,但我这两项都没问题。
我已尝试:
- 将
implementation 'org.postgresql:postgresql:42.6.0'升级至42.7.1 - 调整URL格式以匹配ElephantSQL要求
- 添加
Class.forName("org.postgresql.Driver");语句
另外,我注意到build.gradle中targetSdkVersion为30,但本地SDK版本更高,这是否会导致问题?若会,如何更新SDK版本?
编辑:经过数小时排查,发现之前使用的PostgreSQL驱动版本不合适,仅42.2.9版本包含所有必需方法,更换后解决了上述驱动失败问题,但又出现新错误:
org.postgresql.util.PSQLException: Could not find a java cryptographic algorithm: jks KeyStore not available.
相关资料极少,恳请各位提供帮助。
内容的提问来源于stack exchange,提问作者Khofo
相关产品推荐
相关产品推荐

