Sql Database连接使用JDBC驱动程序和android

时间:2014-07-24 12:47:00

标签: java android sql ssl jdbc

我正在尝试使用Android应用程序连接到使用JDBC的SQL Server。我将sqljdbc4.jar导入到我的应用程序中但是当我到达

  

conn = DriverManager.getConnection(connString);

我收到以下错误:连接字符串是

  

JDBC:SQLSERVER://xxx.xxx.xxx.xxx:1433;加密= FASLE;用户=用户名,密码=密码;

任何人都可以告诉我出了什么问题吗?  如果我将相同的代码放入常规的Java应用程序中,它运行正常。

基本Java

public class Test {
    public void dbConnect(String db_connect_string, String db_userid,
            String db_password) {
        try {

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection conn = DriverManager.getConnection(db_connect_string,
                    db_userid, db_password);
            System.out.println("connected");
            Statement statement = conn.createStatement();
            String queryString = "select * from sysobjects where type='u'";
            ResultSet rs = statement.executeQuery(queryString);
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Test connServer = new Test();
        connServer.dbConnect("jdbc:sqlserver://Serverlocalip:1433", "sa",
                "sqlserver");
    }
}

Android代码:

public List<String> dbConnect(String Host, String Port, String db_userid,
            String db_password) {
        List<String> Db_list = new ArrayList<String>();
        try {
            // String ConnectionString = "jdbc:sqlserver://" + Host + ":" +
            // Port;
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:sqlserver://Serverlocalip:1433", db_userid,
                    db_password);
            System.out.println("connected");
            Statement statement = conn.createStatement();
            String queryString = "select * from sysobjects where type='u'";
            ResultSet rs = statement.executeQuery(queryString);
            while (rs.next()) {
                System.out.println(rs.getString(1));
                Db_list.add(rs.getString(1));
            }
        } catch (Exception e) {
            Db_list.add("Error");
            e.printStackTrace();
        }
        return Db_list;
    }

错误

07-24 17:25:02.058: W/System.err(22227): com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Socket closed".
07-24 17:25:02.228: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1352)
07-24 17:25:02.228: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1466)
07-24 17:25:02.228: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1042)
07-24 17:25:02.228: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
07-24 17:25:02.228: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
07-24 17:25:02.228: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
07-24 17:25:02.228: W/System.err(22227):    at java.sql.DriverManager.getConnection(DriverManager.java:175)
07-24 17:25:02.238: W/System.err(22227):    at java.sql.DriverManager.getConnection(DriverManager.java:209)
07-24 17:25:02.238: W/System.err(22227):    at com.example.sqlserverconnector.MainActivity.dbConnect(MainActivity.java:87)
07-24 17:25:02.238: W/System.err(22227):    at com.example.sqlserverconnector.MainActivity$1.onClick(MainActivity.java:39)
07-24 17:25:02.238: W/System.err(22227):    at android.view.View.performClick(View.java:3574)
07-24 17:25:02.238: W/System.err(22227):    at android.view.View$PerformClick.run(View.java:14293)
07-24 17:25:02.238: W/System.err(22227):    at android.os.Handler.handleCallback(Handler.java:605)
07-24 17:25:02.248: W/System.err(22227):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-24 17:25:02.248: W/System.err(22227):    at android.os.Looper.loop(Looper.java:137)
07-24 17:25:02.248: W/System.err(22227):    at android.app.ActivityThread.main(ActivityThread.java:4448)
07-24 17:25:02.248: W/System.err(22227):    at java.lang.reflect.Method.invokeNative(Native Method)
07-24 17:25:02.248: W/System.err(22227):    at java.lang.reflect.Method.invoke(Method.java:511)
07-24 17:25:02.248: W/System.err(22227):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
07-24 17:25:02.248: W/System.err(22227):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
07-24 17:25:02.258: W/System.err(22227):    at dalvik.system.NativeStart.main(Native Method)
07-24 17:25:02.258: W/System.err(22227): Caused by: java.net.SocketException: Socket closed
07-24 17:25:02.318: W/System.err(22227):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
07-24 17:25:02.318: W/System.err(22227):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:410)
07-24 17:25:02.318: W/System.err(22227):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:257)
07-24 17:25:02.318: W/System.err(22227):    at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1433)

任何帮助都会很棒并且提前致谢

1 个答案:

答案 0 :(得分:1)

更改驱动程序名称,然后导入lib文件jtds 1.3.0.jar文件。并在代码下方更改它将正常工作.............

public List<String> dbConnect(String Host, String Port, String db_userid,
                String db_password) {
            List<String> Db_list = new ArrayList<String>();
            try {
                String ConnectionString = "jdbc:jtds:sqlserver://" + Host + ":"
                        + Port;
                // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
                Connection conn = DriverManager.getConnection(ConnectionString,
                        db_userid, db_password);
                System.out.println("connected");
                Statement statement = conn.createStatement();
                String queryString = "select name from sys.databases";
                ResultSet rs = statement.executeQuery(queryString);
                while (rs.next()) {
                    Db_list.add(rs.getString(1));
                }
            } catch (Exception e) {
                Db_list.add("Error");
                e.printStackTrace();
            }
            return Db_list;
        }