Java 使用 JDBC 访问数据库141


Java 中通过 JDBC(Java 数据库连接)访问数据库是一种连接和操作关系型数据库的标准 API。借助 JDBC,Java 程序可以与各种数据库进行通信,例如 MySQL、PostgreSQL、Oracle 和 Microsoft SQL Server。

连接到数据库

要连接到数据库,需要以下步骤:

加载适当的 JDBC 驱动程序。
使用驱动程序管理器创建连接对象。
使用连接对象创建语句对象,用于发送 SQL 查询和更新。

例如,要连接到 MySQL 数据库:
```java
import ;
import ;
import ;
public class JDBCExample {
public static void main(String[] args) {
// 加载 MySQL 驱动程序
try {
("");
} catch (ClassNotFoundException e) {
();
return;
}
// 创建连接对象
Connection connection = null;
try {
connection = ("jdbc:mysql://localhost:3306/my_database", "username", "password");
} catch (SQLException e) {
();
return;
}
}
}
```

查询数据

要查询数据,需要以下步骤:

使用连接对象创建语句对象。
使用语句对象执行查询。
使用结果集对象遍历查询结果。

例如,查询名为 "customers" 的表中所有记录:
```java
import ;
import ;
import ;
import ;
import ;
public class JDBCExample {
public static void main(String[] args) {
// 加载 MySQL 驱动程序
try {
("");
} catch (ClassNotFoundException e) {
();
return;
}
// 创建连接对象
Connection connection = null;
try {
connection = ("jdbc:mysql://localhost:3306/my_database", "username", "password");
} catch (SQLException e) {
();
return;
}
// 创建语句对象
Statement statement = null;
try {
statement = ();
} catch (SQLException e) {
();
return;
}
// 执行查询
ResultSet resultSet = null;
try {
resultSet = ("SELECT * FROM customers");
} catch (SQLException e) {
();
return;
}
// 遍历结果集
try {
while (()) {
// 获取特定列的值
int id = ("id");
String name = ("name");
// 处理结果
}
} catch (SQLException e) {
();
} finally {
// 关闭结果集、语句和连接
if (resultSet != null) {
try {
();
} catch (SQLException e) {
();
}
}
if (statement != null) {
try {
();
} catch (SQLException e) {
();
}
}
if (connection != null) {
try {
();
} catch (SQLException e) {
();
}
}
}
}
}
```

更新数据

要更新数据,需要以下步骤:

使用连接对象创建语句对象。
使用语句对象执行更新操作。

例如,更新 "customers" 表中特定客户的姓名:
```java
import ;
import ;
import ;
import ;
public class JDBCExample {
public static void main(String[] args) {
// 加载 MySQL 驱动程序
try {
("");
} catch (ClassNotFoundException e) {
();
return;
}
// 创建连接对象
Connection connection = null;
try {
connection = ("jdbc:mysql://localhost:3306/my_database", "username", "password");
} catch (SQLException e) {
();
return;
}
// 创建语句对象
Statement statement = null;
try {
statement = ();
} catch (SQLException e) {
();
return;
}
// 执行更新操作
try {
int rowCount = ("UPDATE customers SET name = 'John Doe' WHERE id = 1");
} catch (SQLException e) {
();
return;
} finally {
// 关闭语句和连接
if (statement != null) {
try {
();
} catch (SQLException e) {
();
}
}
if (connection != null) {
try {
();
} catch (SQLException e) {
();
}
}
}
}
}
```

事务处理

事务是一组原子操作,要么全部成功,要么全部失败。使用 JDBC,可以通过以下步骤实现事务处理:

设置连接的自动提交为 false,以开启事务。
执行 SQL 查询和更新操作。
当事务完成时,要么提交事务使更改持久化,要么回滚事务以撤销更改。

例如,使用事务更新 "customers" 表中的两个记录:
```java
import ;
import ;
import ;
import ;
public class JDBCExample {
public static void main(String[] args) {
// 加载 MySQL 驱动程序
try {
("");
} catch (ClassNotFoundException e) {
();
return;
}
// 创建连接对象
Connection connection = null;
try {
connection = ("jdbc:mysql://localhost:3306/my_database", "username", "password");
// 禁用自动提交
(false);
} catch (SQLException e) {
();
return;
}
Statement statement = null;
try {
statement = ();
// 更新客户 1 的姓名
int rowCount1 = ("UPDATE customers SET name = 'John Doe' WHERE id = 1");
// 更新客户 2 的电子邮件
int rowCount2 = ("UPDATE customers SET email = 'johndoe@' WHERE id = 2");
// 提交事务
();
} catch (SQLException e) {
();
// 回滚事务
try {
();
} catch (SQLException rollbackException) {
();
}
} finally {
// 关闭语句和连接
if (statement != null) {
try {
();
} catch (SQLException e) {
();
}
}
if (connection != null) {
try {
();
} catch (SQLException e) {
();
}
}
}
}
}
```

2024-12-05


上一篇:优化 Java 代码格式,打造可读且可维护的代码

下一篇:Java 中获取输入字符的全面指南