Sql Database连接使用JDBC驱动程序和android
我正在尝试使用Android应用程序连接到SQL Server使用JDBC。 我将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 dbConnect(String Host, String Port, String db_userid, String db_password) { List Db_list = new ArrayList(); 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)
任何帮助都会很棒,并提前致谢
更改Dirver名称,然后导入lib文件jtds 1.3.0.jar文件。并在代码下面更改它将正常工作………….
public List dbConnect(String Host, String Port, String db_userid, String db_password) { List Db_list = new ArrayList (); 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; }