英文:
How can I connect a JDBC azure sql server to android application?
问题
我一直在使用Android Studio编写一个需要访问云数据库的Android应用程序。我正在使用Microsoft Azure作为我的数据库,并且已经创建了它,但是我在尝试从Android应用程序查询它时遇到了困难。当我在Android Studio中尝试时,出现了一个与未找到JDBC有关的错误,然而我已经尝试添加了相关的.jar文件,但没有成功。我还尝试过按照Azure网站上的建议创建一个Maven项目,在这个项目中我可以正常访问数据库,然而一旦我尝试在Android Studio中将由Maven创建的包作为模块使用,SQL查询就不再起作用,而是转到异常情况。下面您可以看到我正在尝试在Maven项目中访问的函数:
public static ArrayList<Double[]> getIncidentDetails() {
// Connect to database
String url = "jdbc:sqlserver://............"; // 将............ 替换为我的连接字符串
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项目,都将非常感谢。
英文:
I have been writing an android application in android studio that needs access to a cloud database. I am using Microsoft Azure for my database and have already created it, however I am struggling to query it from the android application. When trying from Android Studio, I get an error to do with JDBC not being found, however I have tried adding the relevant .jar files with no luck. 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 and instead goes to the exception. Below you can see the function I am trying to access in the maven project:
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;
}
Any ideas on how I could connect to the database from Android Studio, with or without the Maven project would be greatly appreciated.
答案1
得分: 0
尝试在打开连接之前加载该类:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
英文:
Try to load the class before you open the connection:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
专注分享java语言的经验与见解,让所有开发者获益!
评论