【发布时间】:2013-08-18 13:56:32
【问题描述】:
我有以下代码,它没有到达 .setText("Successful") 语句,表明 drivermanager.getConnection statemenet 存在问题(我认为)。它从 net.sourceforge 中找到我正在使用的数据库驱动程序。但是没有抛出异常错误信息,什么也没有发生:
String connectionurl = "jdbc:jtds:sqlserver://41.185.13.201; databaseName=Courses; user=*;Password=*;Persist Security Info=True;";
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(connectionurl);
textview7.setText("Successful");
// Create and execute an SQL statement that returns some data.
String SQL = "INSERT INTO Courses (CourseCode) VALUES ('INFO3002')";
Statement stmt = con.createStatement();
stmt.executeUpdate(SQL);
con.close();
}
catch (ClassNotFoundException e) {
textview7.setText("Could not find the database driver " + e.getMessage());
} catch (SQLException e) {
textview7.setText("Could not connect to the database " + e.getMessage());
}
catch (Exception e) {
//textview7.setText(e.getMessage());
}
【问题讨论】:
-
您可能应该在所有 catch 子句中打印异常。从 Android 应用程序直接连接到数据库是一个非常糟糕的设计,请实现一些中间件。
-
有很多方法可以为您的 android 应用程序实现服务器。 jersey.java.net 使得暴露 RESTful 服务变得非常容易。
标签: java android sql-server-2008 jdbc