提示:以下是本篇文章正文内容,下面案例可供参考
因为开始在csdn上一直搜不到相关的资料,所以准备自己写一个,服务大家,提升自己!
进入微软中国官网,下载jar包,网址:https://www.microsoft.com/zh-cn/download/details.aspx?id=57175
代码如下:
请注意,可以照葫芦画瓢,修改其中的用户名和密码还有数据库的名称,这里面用的是我自己的。
import java.sql.*;
import java.security.Security;
public class sql {
public static void main(String[] args) {
Security.setProperty("jdk.tls.disabledAlgorithms","SSLv3, RC4, DES, MD5withRSA, DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, include jdk.disabled.namedCurves");
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=teach";
String userName="sa";
String userPwd="123456";
try {
Class.forName(driverName);
System.out.println("加载驱动成功!");
} catch (Exception e) {
//TODO: handle exception
e.printStackTrace();
System.out.println("加载驱动失败!");
}
try {
Connection dbConn=DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("连接数据库成功!");
} catch (Exception e) {
//TODO: handle exception
e.printStackTrace();
System.out.println("SQL Server连接失败!");
}
}
}
将jar包直接拖入lib文件夹,之后我们修改classpath配置,ctrl+shift+p输入命令classpath
此处点击add,把lib文件夹里的包导入进来。
我们打开配置管理器,
把这三个协议都启用,有条件的话可以重启一下电脑,确保配置成功。
这里不再赘述,我建的数据库的名称是teach
因此很好理解,这里的Databasename是teach
如果出现下列字样,ohhhhhh!恭喜你,连接成功了!
失败了也没有关系,毕竟我也失败过很多次,哈哈,可以把问题打在评论区,我们一起交流讨论,一定能解决的!
问题描述:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client preferences [TLS12]”。 ClientConnectionId:2e5b4fba-881c-4694-aeb6-2f70dd51b175
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2924)
at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1881)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2484)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2142)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1993)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1164)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:760)
at com.diwork.intelliv.datasourceplugin.sqlserver.MsSQLActuator.createConnection(MsSQLActuator.java:82)
… 24 more
Caused by: javax.net.ssl.SSLHandshakeException: The server selected protocol version TLS10 is not accepted by client preferences [TLS12]
at sun.security.ssl.Alert.createSSLException(Alert.java:131)
at sun.security.ssl.Alert.createSSLException(Alert.java:117)
at sun.security.ssl.TransportContext.fatal(TransportContext.java:311)
at sun.security.ssl.TransportContext.fatal(TransportContext.java:267)
at sun.security.ssl.TransportContext.fatal(TransportContext.java:258)
at sun.security.ssl.ServerHello S e r v e r H e l l o C o n s u m e r . o n S e r v e r H e l l o ( S e r v e r H e l l o . j a v a : 948 ) a t s u n . s e c u r i t y . s s l . S e r v e r H e l l o ServerHelloConsumer.onServerHello(ServerHello.java:948) at sun.security.ssl.ServerHello ServerHelloConsumer.onServerHello(ServerHello.java:948)atsun.security.ssl.ServerHelloServerHelloConsumer.consume(ServerHello.java:874)
at sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:377)
at sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:444)
at sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:422)
at sun.security.ssl.TransportContext.dispatch(TransportContext.java:182)
at sun.security.ssl.SSLTransport.decode(SSLTransport.java:152)
at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1383)
at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1291)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:435)
at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1799)
… 30 more
原因:
jdk高版本默认不在支持TLSv1、TLSv1.1协议,比如:jdk 1.8.0_292
解决方案:
1、推荐:动态修改安全配置,在程序代码中将禁用TLSv1、TLSv1.1的配置去掉。
默认配置:jdk.tls.disabledAlgorithms = SSLv3, TLSv1, TLSv1.1, RC4, DES, MD5withRSA, DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, include jdk.disabled.namedCurves
修改后:SSLv3, RC4, DES, MD5withRSA, DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, include jdk.disabled.namedCurves
修改方法:添加以下代码
import java.security.Security;
Security.setProperty(“jdk.tls.disabledAlgorithms”,“SSLv3, RC4, DES, MD5withRSA, DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, include jdk.disabled.namedCurves”);
2、思路同1,修改配置文件,将禁用TLSv1、TLSv1.1的配置去掉
文件所在目录:jdk1.8.0_292\jre\lib\securityjava.security