所需jar包:
https://www.microsoft.com/zh-CN/download/details.aspx?id=11774
package controller;
import java.sql.Connection;
import java.sql.DriverManager;
public class Test {
public static void main(String[] srg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
String dbURL = "jdbc:sqlserver://数据库地址:1433;DatabaseName=数据库名字"; // 连接服务器和数据库sample
String userName = "用户名"; // 默认用户名
String userPwd = "密码"; // 密码
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful! "); // 如果连接成功控制台输出
// Connection
// Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
}