基于JDBC进行数据查询操作,分别实现顺序查询、条件查询、排序查询和模糊查询操作。数据库系统可选用Derby、Access、SQL Server、MySql等其中的一个建立数据库及其数据表。
时间: 2023-12-22 22:05:38 浏览: 39
好的,我可以为您提供一个基于MySQL的示例代码。首先,您需要安装MySQL数据库并创建一个名为“test”的数据库。在该数据库中,创建一个名为“users”的数据表,其中包含“id”、“name”和“age”三个字段。
接下来,您可以使用以下代码进行四种不同类型的查询操作:
1. 顺序查询
```java
import java.sql.*;
public class SequentialQuery {
public static void main(String[] args) {
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
// 创建Statement对象
Statement stmt = conn.createStatement();
// 执行查询
String sql = "SELECT * FROM users";
ResultSet rs = stmt.executeQuery(sql);
// 遍历结果集
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Age: " + rs.getInt("age"));
}
// 关闭连接
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
2. 条件查询
```java
import java.sql.*;
public class ConditionalQuery {
public static void main(String[] args) {
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
// 创建PreparedStatement对象
String sql = "SELECT * FROM users WHERE age > ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 20);
// 执行查询
ResultSet rs = pstmt.executeQuery();
// 遍历结果集
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Age: " + rs.getInt("age"));
}
// 关闭连接
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
3. 排序查询
```java
import java.sql.*;
public class SortingQuery {
public static void main(String[] args) {
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
// 创建Statement对象
Statement stmt = conn.createStatement();
// 执行查询
String sql = "SELECT * FROM users ORDER BY age DESC";
ResultSet rs = stmt.executeQuery(sql);
// 遍历结果集
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Age: " + rs.getInt("age"));
}
// 关闭连接
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
4. 模糊查询
```java
import java.sql.*;
public class FuzzyQuery {
public static void main(String[] args) {
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
// 创建PreparedStatement对象
String sql = "SELECT * FROM users WHERE name LIKE ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%Tom%");
// 执行查询
ResultSet rs = pstmt.executeQuery();
// 遍历结果集
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Age: " + rs.getInt("age"));
}
// 关闭连接
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
希望这些示例代码可以帮助您进行基于JDBC的数据查询操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)