JDBC的四个核心步骤可以清晰地分为以下几个部分,以下是每个步骤的详细描述:
1. 注册驱动
步骤描述 :
JDBC首先需要加载数据库驱动程序到JVM中,以便能够与数据库进行通信。这一步骤通常通过调用Class.forName(driverName)实现,其中driverName是数据库驱动程序的完全限定名。例如,对于MySQL数据库,驱动程序的完全限定名通常是"com.mysql.cj.jdbc.Driver"(MySQL 8.0及以上版本)或"com.mysql.jdbc.Driver"(早期版本)。
示例代码:
|---|-----------------------------------------------------------------|
| | try { |
| | Class.forName("com.mysql.cj.jdbc.Driver"); // MySQL 8.0及以上版本 |
| | // 或 Class.forName("com.mysql.jdbc.Driver"); // MySQL早期版本 |
| | } catch (ClassNotFoundException e) { |
| | e.printStackTrace(); |
| | } |
2. 获取连接
步骤描述 :
一旦驱动程序被加载,JDBC就可以通过DriverManager类的getConnection()方法获取与数据库的连接。这个方法需要数据库的URL、用户名和密码作为参数。
示例代码:
|---|-----------------------------------------------------------------------|
| | String url = "jdbc:mysql://localhost:3306/mydatabase"; |
| | String username = "root"; |
| | String password = "mypassword"; |
| | Connection connection = null; |
| | try { |
| | connection = DriverManager.getConnection(url, username, password); |
| | } catch (SQLException e) { |
| | e.printStackTrace(); |
| | } |
3. 创建语句
步骤描述 :
有了与数据库的连接后,就可以通过调用Connection对象的createStatement()或prepareStatement()方法来创建语句对象。createStatement()用于执行静态SQL语句,而prepareStatement()用于执行带参数的预编译SQL语句。
示例代码 (使用createStatement()):
|---|----------------------------------------------|
| | Statement statement = null; |
| | try { |
| | statement = connection.createStatement(); |
| | } catch (SQLException e) { |
| | e.printStackTrace(); |
| | } |
示例代码 (使用prepareStatement()):
|---|----------------------------------------------------------|
| | String sql = "SELECT * FROM users WHERE id = ?"; |
| | PreparedStatement preparedStatement = null; |
| | try { |
| | preparedStatement = connection.prepareStatement(sql); |
| | preparedStatement.setInt(1, 123); // 设置参数值 |
| | } catch (SQLException e) { |
| | e.printStackTrace(); |
| | } |
4. 执行语句并处理结果
步骤描述 :
使用Statement或PreparedStatement对象执行SQL语句,并根据SQL语句的类型(查询或更新)处理返回的结果。对于查询语句,可以使用executeQuery()方法执行并返回一个ResultSet对象,用于遍历查询结果;对于更新语句(如INSERT、UPDATE、DELETE),可以使用executeUpdate()方法执行并返回受影响的行数。
示例代码(执行查询):
|---|---------------------------------------------------------------|
| | ResultSet resultSet = null; |
| | try { |
| | resultSet = statement.executeQuery("SELECT * FROM users"); |
| | while (resultSet.next()) { |
| | // 处理结果集 |
| | String name = resultSet.getString("name"); |
| | int age = resultSet.getInt("age"); |
| | // ... |
| | } |
| | } catch (SQLException e) { |
| | e.printStackTrace(); |
| | } finally { |
| | // 关闭资源 |
| | if (resultSet != null) resultSet.close(); |
| | if (statement != null) statement.close(); |
| | if (connection != null) connection.close(); |
| | } |
示例代码(执行更新):
|---|----------------------------------------------------------------------------------------|
| | int rowsAffected = 0; |
| | try { |
| | rowsAffected = statement.executeUpdate("UPDATE users SET age = 30 WHERE id = 123"); |
| | } catch (SQLException e) { |
| | e.printStackTrace(); |
| | } |
在执行完数据库操作后,务必记得关闭相关的资源(如ResultSet、Statement和Connection对象),以避免资源泄漏。这通常通过调用它们的close()方法实现,并放在finally块中以确保无论是否发生异常都会被执行。