如何从我的Android应用程序连接sql server 2008

时间:2014-06-22 11:20:10

标签: android sql-server-2008 database-connection

我正在开发Android应用程序以连接SQL Server 2008,这是我的源代码 DBConn.java

public class DBConn {
    private static Connection conn = null ;
    private static String DRIVER = "net.sourceforge.jtds.jdbc.Driver";
    private static String DB_NAME = "TestDB" ;
    private static String DB_USER = "sa" ;
    private static String DB_PASSWORD = "sa" ;

    public boolean conSqlServer() {
        Log.d("ERR","start connect SQL SERVER") ;

        try {
            Class.forName(DRIVER).newInstance();
            String connString = "jdbc:jtds:sqlserver://192.168.1.100:1433/TestDB; instance=SQLEXPRESS; user=sa;password=sa";
            conn = DriverManager.getConnection(connString);
            Log.d("SUCCESS","Connection successful ");
            return true;
        }
        catch (SQLException e) {
            Log.d("ERR","ERROR CONNECTION 1 = " + e.getMessage());
            e.printStackTrace();
            return false ;
        }
        catch (Exception ex ) {
            Log.d("ERR","ERROR CONNECTION 2 = " + ex.getMessage());
            ex.printStackTrace();
            return false ;
        }
    }
}

我从MainActivity.java中调用它

public class MainActivity extends ActionBarActivity {
....
....
    DBConn dbconn = new DBConn();
    dbconn.conSqlServer() ;
...
}

的AndroidManifest.xml

<manifest>
    .....
    <uses-permission android:name="android.permission.INTERNET" />
    .....
</manifest>

但是我得到了这样的错误

java.sql.SQLException: Network error IOException: socket failed: EACCES (Permission denied)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
at java.sql.DriverManager.getConnection(DriverManager.java:179)
at java.sql.DriverManager.getConnection(DriverManager.java:213)
at com.noh.connectdb.app.core.DBConn.conSqlServer(DBConn.java:35)
at com.noh.connectdb.app.MainActivity.onCreate(MainActivity.java:20)
....
....
  • 我通过wifi从Android设备运行
  • 我检查连接我的设备可以ping服务器
  • 防火墙已关闭
  • 端口1433已打开

任何人都可以帮助我吗?

0 个答案:

没有答案