java 连接 sqlserver 2000

20190327 09:30最新更新:

同事竟然发现有Maven官方源的3.0版本也能运行,所以跳过 1、2 步骤,直接用Maven引入依赖就行

<dependency>
    <groupId>com.microsoft</groupId>
    <artifactId>sqljdbc4</artifactId>
    <version>3.0</version>
</dependency>

-------------------------------------------------------------------------------

业务需要连接远程sqlserver 2000 数据库 并获取数据

1、下载 SQL Server JDBC Driver

https://www.microsoft.com/en-us/download/details.aspx?id=2505

下载后是个htm文件,用浏览器打开后点击

I Accept the above agreement and I want to download the UNIX version (UNIX 版本)

或者

I Accept the above agreement and I want to download the Microsoft Windows version (Windows 版本)

即会下载对应版本。

2、将解压后的jar文件引入到自己项目中(Maven)

两个jar对应的JRE如下

Maven 引入代码

<dependency>
    <groupId>com.microsoft.jdbc.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>2.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/jar/sqljdbc4.jar</systemPath>
</dependency>

groupId,artifactId,version 可根据自己喜好编写

3、java示例代码

import java.*;
public class Connect{
     private java.sql.Connection  con = null;
     //官方代码有microsoft,但加了会报“No suitable driver found for jdbc:microsoft:sqlserver://...",各位根据自己项目加减
     //private final String url = "jdbc:microsoft:sqlserver://";
     private final String url = "jdbc:sqlserver://";
     private final String serverName= "localhost";
     private final String portNumber = "1433";
     private final String databaseName= "pubs";
     private final String userName = "user";
     private final String password = "password";
     // Informs the driver to use server a side-cursor, 
     // which permits more than one active statement 
     // on a connection.
     private final String selectMethod = "cursor"; 
     
     // Constructor
     public Connect(){}
     
     private String getConnectionUrl(){
          return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
     }
     
     private java.sql.Connection getConnection(){
          try{
               Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
               con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
               if(con!=null) System.out.println("Connection Successful!");
          }catch(Exception e){
               e.printStackTrace();
               System.out.println("Error Trace in getConnection() : " + e.getMessage());
         }
          return con;
      }

     /*
          Display the driver properties, database details 
     */ 

     public void displayDbProperties(){
          java.sql.DatabaseMetaData dm = null;
          java.sql.ResultSet rs = null;
          try{
               con= this.getConnection();
               if(con!=null){
                    dm = con.getMetaData();
                    System.out.println("Driver Information");
                    System.out.println("\tDriver Name: "+ dm.getDriverName());
                    System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
                    System.out.println("\nDatabase Information ");
                    System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
                    System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
                    System.out.println("Avalilable Catalogs ");
                    rs = dm.getCatalogs();
                    while(rs.next()){
                         System.out.println("\tcatalog: "+ rs.getString(1));
                    } 
                    rs.close();
                    rs = null;
                    closeConnection();
               }else System.out.println("Error: No active Connection");
          }catch(Exception e){
               e.printStackTrace();
          }
          dm=null;
     }     
     
     private void closeConnection(){
          try{
               if(con!=null)
                    con.close();
               con=null;
          }catch(Exception e){
               e.printStackTrace();
          }
     }
     public static void main(String[] args) throws Exception
       {
          Connect myDbTest = new Connect();
          myDbTest.displayDbProperties();
       }
}

如果成功会显示如下信息:

Connection Successful!
Driver Information
        Driver Name: SQLServer
        Driver Version: 2.2.0022

Database Information
        Database Name: Microsoft SQL Server
        Database Version: Microsoft SQL Server  2000 - 8.00.384 (Intel X86)
        May 23 2001 00:02:52
        Copyright (c) 1988-2000 Microsoft Corporation
        Desktop Engine on Windows NT 5.1 (Build 2600: )

Avalilable Catalogs
        catalog: master
        catalog: msdb
        catalog: pubs
        catalog: tempdb

参考文献:

https://support.microsoft.com/en-au/help/313100/how-to-get-started-with-microsoft-jdbc

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值