5、選擇一個項目右鍵單擊目錄窗口中的Test, 選擇Build Path --> Configure Build Path..., 添加擴展jar文件,即把sqljdbc.jar添加到其中
import java.sql.*;
public class Test {
public static void main(String[] srg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加載JDBC驅(qū)動
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=information";
String userName = "sa";
String userPwd = "123";
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!");
} catch (Exception e) {
e.printStackTrace();
}
}
}