【发布时间】:2018-12-21 01:11:30
【问题描述】:
我无法在我的 Android 应用中连接到远程 mysql 数据库,因为该应用找不到合适的驱动程序。
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
applicationId "com.example.test.testapp"
minSdkVersion 20
targetSdkVersion 21
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
//https://stackoverflow.com/questions/37048731/gson-library-in-android-studio/37049457
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.android.support:design:26.1.0'
// implementation 'org.mariadb.jdbc:mariadb-java-client:2.1.2'
compile 'com.android.support:support-annotations:27.1.1'
// implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// implementation files('libs/mysql-connector-java-5.1.46.jar')
// compile project(':mysql-connector-java-5.1.46')
// compile project(':libs/mysql-connector-java-5.1.46.jar')
implementation files('libs/mysql-connector-java-5.1.46-bin.jar')
}
这是我发生异常的java类。
public ServerSave() throws SQLException, ClassNotFoundException {
// load and register JDBC driver for MySQL
Class.forName("com.mysql.jdbc.Driver");
this.conn = getConnection("jdbc:mariadb://remote.host.example/" + SQL_DATABASE, SQL_USER_NAME, SQL_PASSWORD);
}
我认为这是一个配置问题,但我不知道如何解决它。 我将 mysql jar 文件复制到我的 android 项目文件夹中的 libs 目录中。 我正在为这个项目使用 android studio。
感谢您的帮助。
【问题讨论】:
-
将您的 Android 应用程序远程连接到 MySQL 通常是一个非常糟糕的主意,因为将您的 MySQL 实例暴露在一般互联网上是非常危险的。是否可以在应用程序和数据库之间构建 API 层?这样您就可以创建一个安全缓冲区并拥有更细粒度的访问控制。
-
我的想法是用mysql用户访问权限和存储过程来处理这个
-
这是对数据库安全模型的用途和功能的常见误解。 API 可以实现非常复杂的访问控制系统。 MySQL 本身确实不能。它不打算作为公共服务公开。存储过程没有足够的保护。系统中最有价值的资产是数据库,应该尽可能地锁定和保护它。如果您提供直接访问权限,您的风险就会增加几个数量级。
-
@AceofSpade 甚至可以进入您的数据库,您已经在 APK 中提供了数据库的密码。到时候你就完蛋了。这是你不应该做的第一件事。
-
我的 api 中的某些东西不是比数据库中的错误更有可能吗?但是当然两者都使用更好。