当前位置:   article > 正文

使用Java连接SQLserver数据库_com.microsoft.sqlserver.jdbc.sqlserverdatasource

com.microsoft.sqlserver.jdbc.sqlserverdatasource

0、准备

使用win10、SQLServer2012、eclipse2020-6、Java1.8.0_311

1、安装微软官方的JDBC,并配置环境变量 Download Microsoft JDBC Driver 6.0 for SQL Server from Official Microsoft Download Center

 

 下载sqljdbc_6.0.8112.200_chs.tar.gz,再将其解压sqljdbc_6.0.8112.200_chs.tar

 这里x64是在适于64位,x86是适于32位,选择合适自己电脑的,这里我的是64位的计算机,选择x64文件中的文件,下如图。

将此文件sqljdbc_auth.dll复制到D:\Java\jdk1.8.0_311\bin目录下(此路径就是安装java的路径)

然后再将此文件sqljdbc42.jar复制到D:\Java\jdk1.8.0_311\jre\lib\ext和D:\Java\jre1.8.0_311\lib\ext

 然后打开高级系统设置,配置环境变量

然后如图环境变量      

CLASSPATH,在原有的基础上添加D:\Java\sqljdbc42.jar(这里是存放sqljdbc42.jar的路径,记得用" ; "来与前面的分隔。

然后找到D:\Java\jre1.8.0_311\lib\security下的java.security文件

找到下面这些并注释,并保存

 

 2、sqlserver设置

首先打开SQLserver配置管理器,选择如图下的TCP/IP,并将其启动

然后选择TCP/IP点击右键,选择属性,如图修改

 

 选择SQL Server(MSSQLSERVER)重新启动

 然后检查1433端口是否打开,在cmd里输入netstat -an|findstr 1433

 3、在eclipse中实现Java和SQLserver连接代码

 在选中的Java工程下的src,如图操作

 导入之前的sqljdbc42.jar

 然后就可以在此Java工程下写代码啦!!!

  1. /*=====================================================================
  2. File: ConnectDataSource.java
  3. Summary: This Microsoft JDBC Driver for SQL Server sample application
  4. demonstrates how to connect to a SQL Server database by
  5. using a data source object.
  6. ---------------------------------------------------------------------
  7. This file is part of the Microsoft JDBC Driver for SQL Server Code Samples.
  8. Copyright (C) Microsoft Corporation. All rights reserved.
  9. This source code is intended only as a supplement to Microsoft
  10. Development Tools and/or on-line documentation. See these other
  11. materials for detailed information regarding Microsoft code samples.
  12. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
  13. ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  14. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  15. PARTICULAR PURPOSE.
  16. =====================================================================*/
  17. import java.sql.CallableStatement;
  18. import java.sql.Connection;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
  22. public class ConnectDataSource {
  23. public static void main(String[] args) {
  24. // Create datasource.
  25. SQLServerDataSource ds = new SQLServerDataSource();
  26. ds.setUser("<user>");
  27. ds.setPassword("<password>");
  28. ds.setServerName("<server>");
  29. ds.setPortNumber(Integer.parseInt("<port>"));
  30. ds.setDatabaseName("AdventureWorks");
  31. try (Connection con = ds.getConnection();
  32. CallableStatement cstmt = con.prepareCall("{call dbo.uspGetEmployeeManagers(?)}");) {
  33. // Execute a stored procedure that returns some data.
  34. cstmt.setInt(1, 50);
  35. ResultSet rs = cstmt.executeQuery();
  36. // Iterate through the data in the result set and display it.
  37. while (rs.next()) {
  38. System.out.println("EMPLOYEE: " + rs.getString("LastName") + ", " + rs.getString("FirstName"));
  39. System.out.println("MANAGER: " + rs.getString("ManagerLastName") + ", " + rs.getString("ManagerFirstName"));
  40. System.out.println();
  41. }
  42. }
  43. // Handle any errors that may have occurred.
  44. catch (SQLException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }

测试:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/148477
推荐阅读
相关标签
  

闽ICP备14008679号