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方法栈日志的艺术:从错误定位到性能优化的深度指南
https://www.shuihudhg.cn/133725.html
PHP 获取本机端口的全面指南:实践与技巧
https://www.shuihudhg.cn/133724.html
Python内置函数:从核心原理到高级应用,精通Python编程的基石
https://www.shuihudhg.cn/133723.html
Java Stream转数组:从基础到高级,掌握高性能数据转换的艺术
https://www.shuihudhg.cn/133722.html
深入解析:基于Java数组构建简易ATM机系统,从原理到代码实践
https://www.shuihudhg.cn/133721.html
热门文章
Java中数组赋值的全面指南
https://www.shuihudhg.cn/207.html
JavaScript 与 Java:二者有何异同?
https://www.shuihudhg.cn/6764.html
判断 Java 字符串中是否包含特定子字符串
https://www.shuihudhg.cn/3551.html
Java 字符串的切割:分而治之
https://www.shuihudhg.cn/6220.html
Java 输入代码:全面指南
https://www.shuihudhg.cn/1064.html