JDBC(Java DataBase Connectivity)是Java语言中连接数据库进行操作的一种标准规范。下面是连接SQL Server数据库实现增删改查的全过程:
准备工作
-
安装SQL Server数据库,获取数据库的连接配置信息,包括地址、用户名、密码、端口等信息。
-
下载并安装SQL Server JDBC驱动,下载地址:https://docs.microsoft.com/en-us/sql/connect/jdbc/microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15。
连接数据库
- 加载SQL Server JDBC驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- 创建连接
Connection connection = DriverManager.getConnection(url, userName, password);
- 关闭连接(使用完连接后记得关闭)
connection.close();
数据库操作
查询操作
- 创建查询语句并执行
String sql = "select * from users";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
- 遍历查询结果集
while(resultSet.next()){
// 获取每一列的数据
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
// 根据需要进行处理
System.out.println("id: " + id + ", name: " + name + ", age: " + age);
}
- 关闭结果集和statement
resultSet.close();
statement.close();
插入操作
- 创建插入语句并执行
String sql = "insert into users(name, age) values('Tom', 18)";
Statement statement = connection.createStatement();
int result = statement.executeUpdate(sql);
- 关闭statement
statement.close();
更新操作
- 创建更新语句并执行
String sql = "update users set age = 20 where name = 'Tom'";
Statement statement = connection.createStatement();
int result = statement.executeUpdate(sql);
- 关闭statement
statement.close();
删除操作
- 创建删除语句并执行
String sql = "delete from users where id = 1";
Statement statement = connection.createStatement();
int result = statement.executeUpdate(sql);
- 关闭statement
statement.close();
示例1:查询所有用户信息
public void getAllUsers() throws SQLException, ClassNotFoundException {
// 加载SQL Server JDBC驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 创建连接
String url = "jdbc:sqlserver://localhost:1433;databaseName=test";
String userName = "sa";
String password = "123456";
Connection connection = DriverManager.getConnection(url, userName, password);
// 查询数据
String sql = "select * from users";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
// 遍历结果集
while(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("id: " + id + ", name: " + name + ", age: " + age);
}
// 关闭连接
resultSet.close();
statement.close();
connection.close();
}
示例2:添加新用户
public void addUser() throws SQLException, ClassNotFoundException {
// 加载SQL Server JDBC驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 创建连接
String url = "jdbc:sqlserver://localhost:1433;databaseName=test";
String userName = "sa";
String password = "123456";
Connection connection = DriverManager.getConnection(url, userName, password);
// 添加数据
String sql = "insert into users(name, age) values('Lucy', 20)";
Statement statement = connection.createStatement();
int result = statement.executeUpdate(sql);
// 关闭连接
statement.close();
connection.close();
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:JDBC连接SQL Server数据库实现增删改查的全过程 - Python技术站