1. 程式人生 > 其它 >JDBC驅動連線SQLserver資料庫

JDBC驅動連線SQLserver資料庫

環境配置

軟體下載

1.下載java8Java 存檔下載 — Java SE 8 | Oracle 中國

2.下載IntelliJ IDEA https://www.jetbrains.com/idea/download/#section=windows

3.下載JDBC驅動程式 https://www.microsoft.com/zh-CN/download/details.aspx?id=11774

4.下載telnet服務端(不需要好像也可以用) http://www.goodtechsys.com/downloadstelnetnt2000.asp

軟體配置

SQLserver2019配置管理器

Microsoft SQL Server Management Studio 18配置

telnet服務

IntelliJ IDEA配置

測試程式碼

import java.sql.Connection;
import java.sql.DriverManager;

public class HelloWorld {
    public static void main(String[] args) {
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=Student";//資料庫姓名
        String userName = "sa"; //資料庫賬號
        String userPwd = "123"; //資料庫密碼
        try {
            Class.forName(driverName);
            Connection dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
            System.out.println("資料庫連線成功!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("資料庫連線失敗!");
        }
    }
}

python版本

import pymssql

connect = pymssql.connect('127.0.0.1:1433', 'sa', '123', 'Student')  # 建立連線:地址,管理員賬號,密碼,資料庫名稱
if connect:
    print("連線成功!")
else:
    print("連線失敗!")
connect.close()