0

I'm trying to connect to SQL Serverusing JDBC in an android app. I imported sqljdbc4.jar into my application but when i get to

conn = DriverManager.getConnection(connString);

i get the error below: the connection string is

jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;encrypt=fasle;user=username;password=password;

Can any one tell me what is going wrong? If i put the same code into a regular java application it runs fine.

Basic 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 Code:

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;
    }

Error

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)

any help would be great and Thanks in advance

2
  • Make sure that your database server is up and listening to the port specified. Commented Jul 24, 2014 at 12:50
  • Make sure that you don't have that port firewalled, and that sqlserver is accepting connections from the LAN, not just localhost. (Try it on a desktop that isn't the one hosting the database). Commented Jul 24, 2014 at 13:28

1 Answer 1

1

Change the Dirver Name and before import the lib file jtds 1.3.0.jar file.and change below the code it will work fine.............

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;
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.