开发工具:IntelliJ IDEA 2019.2.2 x64
方法一
1、下载jar包:https://pan.baidu.com/s/1jH4vTaA
2、建立一个普通的java工程
3、将jar包导入工程
4、编写代码
package com.oceanstar;
import java.sql.*;
public class Main {
public static void main(String[] args) {
Connection dbConn = null;
Statement stmt = null;
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=hik";
String userName="sa";
String userPwd="hik12345+";
try {
Class.forName(driverName);
dbConn= DriverManager.getConnection(dbURL,userName,userPwd);
stmt = dbConn.createStatement();
ResultSet rs = stmt.executeQuery("select uname from userinfo");
while (rs.next()){
System.out.printf("username: " + rs.getString("uname"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
//8、释放资源
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (dbConn != null) {
try {
dbConn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
参考:https://blog.csdn.net/qq243920161/article/details/78971861
https://blog.csdn.net/u014572215/article/details/75578813
https://www.cnblogs.com/chenlove/p/9159227.html