0


使用Java连接SQLserver数据库

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工程下写代码啦!!!

/*=====================================================================
File:      ConnectDataSource.java
Summary: This Microsoft JDBC Driver for SQL Server sample application
         demonstrates how to connect to a SQL Server database by 
         using a data source object.
---------------------------------------------------------------------
This file is part of the Microsoft JDBC Driver for SQL Server Code Samples.
Copyright (C) Microsoft Corporation.  All rights reserved.
 
This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.
 
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
=====================================================================*/
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;

public class ConnectDataSource {

    public static void main(String[] args) {

        // Create datasource.
        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setUser("<user>");
        ds.setPassword("<password>");
        ds.setServerName("<server>");
        ds.setPortNumber(Integer.parseInt("<port>"));
        ds.setDatabaseName("AdventureWorks");

        try (Connection con = ds.getConnection();
                CallableStatement cstmt = con.prepareCall("{call dbo.uspGetEmployeeManagers(?)}");) {
            // Execute a stored procedure that returns some data.
            cstmt.setInt(1, 50);
            ResultSet rs = cstmt.executeQuery();

            // Iterate through the data in the result set and display it.
            while (rs.next()) {
                System.out.println("EMPLOYEE: " + rs.getString("LastName") + ", " + rs.getString("FirstName"));
                System.out.println("MANAGER: " + rs.getString("ManagerLastName") + ", " + rs.getString("ManagerFirstName"));
                System.out.println();
            }
        }
        // Handle any errors that may have occurred.
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

测试:


本文转载自: https://blog.csdn.net/Green_Hand_is_me/article/details/122272151
版权归原作者 积极向上的小徐 所有, 如有侵权,请联系我们删除。

“使用Java连接SQLserver数据库”的评论:

还没有评论