在现代企业应用中,数据库是数据存储和管理的重要组成部分。Java作为一种广泛使用的编程语言,提供了多种方式与数据库进行交互。本文将介绍 JDBC(Java Database Connectivity),它是Java语言与数据库之间的桥梁,帮助开发者通过Java程序与数据库进行连接、查询、插入、更新和删除数据。
JDBC(Java数据库连接)是Java提供的一套API,用于从Java程序中访问和操作关系型数据库。通过JDBC,Java程序能够执行SQL语句、管理数据库连接,并处理结果集。JDBC是Java平台的标准一部分,支持多种数据库管理系统(DBMS),如MySQL、Oracle、PostgreSQL、SQL Server等。
Statement: 用于执行简单的SQL查询。PreparedStatement: 用于执行预编译的SQL查询,避免SQL注入问题,并提高性能。CallableStatement: 用于执行存储过程。在使用JDBC时,需要确保以下几个步骤:
Class.forName()来动态加载。DriverManager.getConnection()方法建立与数据库的连接。Statement或PreparedStatement对象执行SQL语句。ResultSet对象读取查询的结果数据。首先,我们需要添加数据库的JDBC驱动包。如果使用的是MySQL,可以下载并添加 MySQL JDBC 驱动(mysql-connector-java)。
然后,创建一个简单的Java程序来连接数据库。
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
// 数据库连接信息
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "root";
// 连接对象
Connection connection = null;
try {
// 1. 加载数据库驱动(对于MySQL数据库)
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 获取数据库连接
connection = DriverManager.getConnection(url, user, password);
System.out.println("连接成功!");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}这段代码展示了如何加载MySQL驱动、建立数据库连接并进行简单的连接验证。
接下来,我们通过JDBC查询数据库,获取并输出查询结果。
public class JDBCQueryExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "root";
String query = "SELECT * FROM users"; // 假设有一个名为users的表
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// 1. 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 获取数据库连接
connection = DriverManager.getConnection(url, user, password);
// 3. 创建Statement对象
statement = connection.createStatement();
// 4. 执行SQL查询
resultSet = statement.executeQuery(query);
// 5. 处理结果集
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String email = resultSet.getString("email");
System.out.println("ID: " + id + ", Name: " + name + ", Email: " + email);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (resultSet != null) resultSet.close();
if (statement != null) statement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}此代码通过executeQuery()方法执行一个SELECT语句,返回一个ResultSet对象,并通过next()方法遍历结果。
使用PreparedStatement进行数据插入,可以提高性能并避免SQL注入。
public class JDBCInsertExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "root";
String insertQuery = "INSERT INTO users (name, email) VALUES (?, ?)";
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
// 1. 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 获取数据库连接
connection = DriverManager.getConnection(url, user, password);
// 3. 创建PreparedStatement对象
preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setString(1, "John Doe");
preparedStatement.setString(2, "john.doe@example.com");
// 4. 执行更新操作(插入数据)
int rowsAffected = preparedStatement.executeUpdate();
System.out.println("插入了 " + rowsAffected + " 行数据");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}PreparedStatement允许我们使用占位符?,在插入数据时动态绑定参数,避免SQL注入问题。
通过PreparedStatement更新数据:
public class JDBCUpdateExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "root";
String updateQuery = "UPDATE users SET email = ? WHERE name = ?";
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
// 1. 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 获取数据库连接
connection = DriverManager.getConnection(url, user, password);
// 3. 创建PreparedStatement对象
preparedStatement = connection.prepareStatement(updateQuery);
preparedStatement.setString(1, "new.email@example.com");
preparedStatement.setString(2, "John Doe");
// 4. 执行更新操作
int rowsAffected = preparedStatement.executeUpdate();
System.out.println("更新了 " + rowsAffected + " 行数据");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}通过PreparedStatement删除数据:
public class JDBCDeleteExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "root";
String deleteQuery = "DELETE FROM users WHERE name = ?";
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
// 1. 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 获取数据库连接
connection = DriverManager.getConnection(url, user, password);
// 3. 创建PreparedStatement对象
preparedStatement = connection.prepareStatement(deleteQuery);
preparedStatement.setString(1, "John Doe");
// 4. 执行删除操作
int rowsAffected = preparedStatement.executeUpdate();
System.out.println("删除了 " + rowsAffected + " 行数据");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}具体来说,setString(int parameterIndex, String x) 方法用于将指定位置的占位符(?)替换成指定的字符串值。
PreparedStatement 的工作原理PreparedStatement 是一个接口,用于执行预编译的 SQL 查询。在 SQL 查询中,通常使用 ? 来表示占位符,表示参数将在运行时被动态传入,而不是在 SQL 查询字符串中直接硬编码。
例如,假设你要执行一个插入操作,SQL 语句可能是这样的:
INSERT INTO users (name, email) VALUES (?, ?);这个 SQL 语句有两个 ? 占位符,表示你将动态地插入两个值(例如,name 和 email)。
setString(int parameterIndex, String x) 详解parameterIndex:这个参数表示要替换的占位符的位置(从1开始),也就是你想要设置哪个位置的参数。x:这是你想要设置的值,类型必须与占位符的类型匹配。在这种情况下,我们使用 setString,意味着占位符将会被一个字符串值替换。preparedStatement.setString(1, "John Doe");
1:这是第一个 ? 占位符的位置(从1开始计数)。表示要将 ? 的位置替换为 John Doe。"John Doe":这是你要插入的实际值(字符串 "John Doe")。这个值会替代 SQL 语句中的第一个占位符 ?。等价于:
INSERT INTO users (name, email) VALUES ('John Doe', ?);
preparedStatement.setString(2, "john.doe@example.com");2:这是第二个 ? 占位符的位置,表示要将 SQL 中的第二个 ? 替换为 john.doe@example.com。"john.doe@example.com":这是你要插入的实际值(字符串 "john.doe@example.com")。这个值会替代 SQL 语句中的第二个占位符 ?。等价于:
INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com');
将这两行代码与 SQL 语句结合,最终执行的 SQL 语句就是:
INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com');
这两行代码的作用是:
"John Doe" 作为第一个参数(name)传递给 SQL 查询。"john.doe@example.com" 作为第二个参数(email)传递给 SQL 查询。使用 PreparedStatement 设置参数有两个主要优点:
本文展示了如何在Java中使用JDBC进行简单的数据库操作,包括连接数据库、查询、插入、更新和删除等基本操作。JDBC为Java应用程序提供了强大的数据库交互功能,但也需要注意资源管理(如连接的关闭)以及SQL注入等安全问题。希望本文能够帮助大家快速上手并实现Java与数据库的交互。