【问题标题】:Connecting to SQL Server 2008 database from android (java)从 android (java) 连接到 SQL Server 2008 数据库
【发布时间】: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


【解决方案1】:

您应该通过 Web 服务(JAX-WS 或 JAX-RS)访问您的数据。这是您可以使用的最佳架构。如上所述,您应该简单地开发一个中间件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多