将Android应用程序连接到Microsoft SQL Server 2012

时间:2016-03-10 08:24:34

标签: android sql-server asp.net-mvc server

我使用了一个连接类来连接本地Android应用程序到Sql Server 2012然后它的工作很好。但是,当我与真正的IP连接时,它不起作用。我的连接类代码如下..请帮帮我

public class ConnectionClass 
{
    String ip = "120.50.9.28";// here I used 192.168.0.131  then its work
    String classs = "net.sourceforge.jtds.jdbc.Driver";
    String db = "AndroMain";
    String un = "sa";
    String password = "Server23";
    @SuppressLint("NewApi")
    public Connection CONN() 
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
          .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection conn = null;
        String ConnURL = null;

        try 
        {
           Class.forName(classs);
           ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
              + "databaseName=" + db + ";user=" + un + ";password="
              + password + ";";
           conn = DriverManager.getConnection(ConnURL);
        } 
        catch (SQLException se) 
        {
           Log.e("ERRO", se.getMessage());
        } 
        catch (ClassNotFoundException e) 
        {
            Log.e("ERRO", e.getMessage());
        } 
        catch (Exception e) 
        {
           Log.e("ERRO", e.getMessage());
        }

        return conn;
    }
}

0 个答案:

没有答案