2013-08-18 28 views
0

我有以下代碼,它沒有得到.setText(「Successful」)語句,表明drivermanager.getConnection statemenet(我認爲)的問題。它從net.sourceforge找到我正在使用的數據庫驅動程序。但沒有異常錯誤信息拋出,沒有任何反應:從android(java)連接到SQL Server 2008數據庫

 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()); 
} 
+1

您應該在所有catch子句中打印該異常。從Android應用程序直接連接到數據庫是一個非常糟糕的設計,實現一些中間件。 – samlewis

+0

這個怎麼辦? –

+0

爲您的android應用程序實現服務器有很多方法。 http://jersey.java.net使暴露RESTful服務變得非常簡單。 – samlewis

回答

1

您應該通過Web服務(JAX-WS或JAX-RS)訪問您的數據。這是您可以使用的最佳建築。如上所述,您應該簡單地開發一箇中間件。