我想开发Android应用程序,将连接到微软Azure SQL数据库在Linux(版本14.04)Android Studio。我使用最新版本的Android Studio 4.1.2。我尝试了一些java代码的数据库连接。但我不能数据库。
我的问题是如何连接到SQL数据库和如何从SQL数据库获取数据到我的安卓应用程序。我的数据存储在Azure SQL数据库->流分析(预览).
Java代码:String Connect_String = "jdbc:sqlserver://sqlserver.database.windows.net:1433;database=iwave_db_1;user=username@sqlserver;password=*******;" +
"encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
String Username = "username@sqlserver";
String Password = "*******";
try {
System.out.println("Connecting to the Database");
//Class.forName("com.mysql.cj.jdbc.Driver");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
//Class.forName("com.sourceforge.jtds.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection(Connect_String, Username, Password);
if (connection != null) {
System.out.println("Done ........");
System.out.println("Database connection success");
}
Connection connection = DriverManager.getConnection(Connect_String, Username, Password);
这一行出现错误。应用程序正在崩溃,无法连接到SQL数据库。
错误信息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.iwavesys.cloudazure, PID: 6864
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1450)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
at java.net.InetAddress.getLocalHost(InetAddress.java:851)
at com.microsoft.sqlserver.jdbc.Util.lookupHostName(Util.java:652)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2690)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2362)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2213)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1276)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:861)
at java.sql.DriverManager.getConnection(DriverManager.java:569)
at java.sql.DriverManager.getConnection(DriverManager.java:219)
at com.iwavesys.cloudazure.ToDoActivity.Azure_Connection(ToDoActivity.java:222)
at com.iwavesys.cloudazure.ToDoActivity.access$100(ToDoActivity.java:43)
at com.iwavesys.cloudazure.ToDoActivity$2.onClick(ToDoActivity.java:154)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我的链接所有库文件在我的gradle文件。构建。Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.iwavesys.cloudazure"
minSdkVersion 19
targetSdkVersion 30
multiDexEnabled true
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
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'org.conscrypt:conscrypt-android:2.2.1'
implementation 'org.apache.httpcomponents:httpclient-android:4.3.5'
//implementation 'net.sourceforge.jtds:jtds:1.3.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.microsoft.azure:azure-mobile-android:3.1.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.google.guava:guava:24.1-jre'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.microsoft.azure.android:azure-storage-android:2.0.0@aar'
}
我使用以下版本的.jar文件进行连接:
- jtds-1.3.1.jar
- mssql-jdbc-8.4.1.jre8.jar
- mysql-connector-java-8.0.22.jar
- azure云中的防火墙设置请查看下面的链接。https://i.stack.imgur.com/q37Sj.png
我错过了什么??我的代码是否正确??任何人知道请帮助我。提前谢谢你。
由于Android中的Java风格,您将需要jtds驱动程序。更新您的连接字符串以使用jtds
,如下所示。注意
- 添加"jtds"前"sqlserver">
- "DatabaseName"而不是"database">
jdbc:jtds:sqlserver://sqlserver.database.windows.net:1433;DatabaseName=iwave_db_1;user=username@sqlserver;password=*******;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
另外,请确保在清单中启用所需的权限以允许连接到internet。还要检查SQL Azure实例的防火墙设置,以允许来自所需ip的流量。
裁判:https://www.youtube.com/watch?v=WJBs0zKGqH0
注意:从移动应用程序直接连接到db不是一个好的设计。我宁愿在两者之间有一些服务器端API服务。