[英]How can I connect a JDBC azure sql server to android application?
我一直在 android 工作室中編寫需要訪問雲數據庫的 android 應用程序。 我正在為我的數據庫使用 Microsoft Azure 並且已經創建了它,但是我正在努力從 android 應用程序中查詢它。 從 Android Studio 嘗試時,我收到與 JDBC 未找到有關的錯誤,但是我嘗試添加相關的.jar 文件但沒有運氣。 I have also tried creating a maven project as suggested on the Azure site, and with this I can access the database fine, however as soon as I try using the package created by maven as a module in Android Studio, the sql query no longer works而是轉到異常。 您可以在下面看到我在 maven 項目中嘗試訪問的 function:
public static ArrayList<Double[]> getIncidentDetails() {
// Connect to database
String url = "jdbc:sqlserver://............"; //Take ............ to be my connection string
Connection connection = null;
ArrayList<Double[]> results = new ArrayList<Double[]>();
try {
connection = DriverManager.getConnection(url);
System.out.println("Connected");
String selectSQL = "SELECT [latitude], [longitude]" + "FROM [Incidents]";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSQL)) {
while (resultSet.next()) {
System.out.println(resultSet.getString(2));
Double latitude = Double.parseDouble(resultSet.getString(1));
Double longitude = Double.parseDouble(resultSet.getString(2));
results.add(new Double[] {latitude, longitude});
}
connection.close();
}
} catch (Exception e){
System.out.println(e.getMessage());
}
return results;
}
任何關於如何從 Android Studio 連接到數據庫的想法,無論是否有 Maven 項目,都將不勝感激。
在打開連接之前嘗試加載 class:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.