30 Java连接数据库与执行SQL语句

30 Java连接数据库与执行SQL语句

在Java中,连接数据库和执行SQL语句是非常重要的技能。下面我们将详细介绍如何在Java中操作数据库,包括连接数据库、执行查询和更新操作等。

1. 添加数据库驱动依赖

在开始之前,你需要确保项目中包含了相应的数据库驱动。如果你使用的是 Maven,可以在 pom.xml 中添加以下依赖。例如,对于 MySQL 数据库,可以使用:

1
2
3
4
5
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version> <!-- 请根据需要选择版本 -->
</dependency>

2. 加载数据库驱动

在Java中,通常使用Class.forName方法来加载数据库驱动,以下是连接 MySQL 数据库的示例代码:

1
2
3
4
5
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

3. 连接数据库

使用 DriverManager 来建立连接。以下是一个连接到 MySQL 数据库的示例:

1
2
3
4
5
6
7
8
9
10
11
12
String url = "jdbc:mysql://localhost:3306/your_database"; // 数据库URL
String user = "your_username"; // 数据库用户名
String password = "your_password"; // 数据库密码

Connection connection = null;

try {
connection = DriverManager.getConnection(url, user, password);
System.out.println("连接成功!");
} catch (SQLException e) {
e.printStackTrace();
}

4. 创建Statement对象

一旦连接成功,你可以创建 StatementPreparedStatement 对象来执行 SQL 语句。这里我们使用 Statement 对象:

1
2
3
4
5
6
7
Statement statement = null;

try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}

5. 执行SQL查询

使用 executeQuery 方法可以执行 SQL 查询,返回一个 ResultSet 对象。以下是一个执行查询的代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
String sql = "SELECT * FROM your_table";
ResultSet resultSet = null;

try {
resultSet = statement.executeQuery(sql);

while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("ID: " + id + ", Name: " + name);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

6. 执行更新操作

使用 executeUpdate 方法可以执行 INSERT、UPDATE 和 DELETE 命令。示例代码如下:

1
2
3
4
5
6
7
8
9
String insertSQL = "INSERT INTO your_table (name) VALUES ('New Name')";
int rowsAffected = 0;

try {
rowsAffected = statement.executeUpdate(insertSQL);
System.out.println("插入成功, 受影响的行数: " + rowsAffected);
} catch (SQLException e) {
e.printStackTrace();
}

7. 关闭连接

使用完数据库后,务必要关闭连接。可以使用 finally 语句块来确保连接被关闭:

1
2
3
4
5
6
7
8
9
10
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}

示例应用

整合以上代码,可以创建一个简单的数据库操作示例程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DatabaseExample {

public static void main(String[] args) {
// 数据库连接参数
String url = "jdbc:mysql://localhost:3306/your_database";
String user = "your_username";
String password = "your_password";

Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;

try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 连接数据库
connection = DriverManager.getConnection(url, user, password);
System.out.println("连接成功!");

// 创建Statement对象
statement = connection.createStatement();

// 查询数据
String sql = "SELECT * FROM your_table";
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("ID: " + id + ", Name: " + name);
}

// 插入数据
String insertSQL = "INSERT INTO your_table (name) VALUES ('New Name')";
int rowsAffected = statement.executeUpdate(insertSQL);
System.out.println("插入成功, 受影响的行数: " + rowsAffected);

} 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();
}
}
}
}

总结

通过以上介绍,你可以了解到如何在 Java 中连接数据库、执行查询和更新操作。这些是与数据库交互的基本要素,希望能够帮助你在 Java 开发中更好地使用数据库。

30 Java连接数据库与执行SQL语句

https://zglg.work/java-one/30/

作者

AI教程网

发布于

2024-08-08

更新于

2024-08-10

许可协议