基于JDBC进行数据查询操作分别实现顺序查询、条件查询、排序查询和模糊查询操作。数据库系统可选用Derby、Access、SQL Server、MySql等其中的一个建立数据库及其数据表
假设我们选用MySQL数据库,创建一个名为student的数据库,其中包括一个名为score的数据表,表中包含三个字段:id、name、score。
- 顺序查询操作
顺序查询操作即为查询所有数据,代码如下:
import java.sql.*;
public class SequentialQuery {
public static void main(String[] args) {
Connection conn = null; // 数据库连接对象
Statement stmt = null; // SQL语句执行对象
ResultSet rs = null; // 结果集对象
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "password");
// 创建SQL语句执行对象
stmt = conn.createStatement();
// 定义SQL语句
String sql = "SELECT * FROM score";
// 执行SQL语句,获取结果集
rs = stmt.executeQuery(sql);
// 遍历结果集并输出
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int score = rs.getInt("score");
System.out.println("id:" + id + ",姓名:" + name + ",分数:" + score);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
- 条件查询操作
条件查询操作即为根据指定条件查询数据,代码如下:
import java.sql.*;
public class ConditionQuery {
public static void main(String[] args) {
Connection conn = null; // 数据库连接对象
PreparedStatement pstmt = null; // SQL语句执行对象
ResultSet rs = null; // 结果集对象
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "password");
// 定义SQL语句
String sql = "SELECT * FROM score WHERE score >= ?";
// 创建SQL语句执行对象
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 60); // 设置查询条件
// 执行SQL语句,获取结果集
rs = pstmt.executeQuery();
// 遍历结果集并输出
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int score = rs.getInt("score");
System.out.println("id:" + id + ",姓名:" + name + ",分数:" + score);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
- 排序查询操作
排序查询操作即为根据指定字段对数据进行排序,代码如下:
import java.sql.*;
public class OrderQuery {
public static void main(String[] args) {
Connection conn = null; // 数据库连接对象
Statement stmt = null; // SQL语句执行对象
ResultSet rs = null; // 结果集对象
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "password");
// 创建SQL语句执行对象
stmt = conn.createStatement();
// 定义SQL语句
String sql = "SELECT * FROM score ORDER BY score DESC";
// 执行SQL语句,获取结果集
rs = stmt.executeQuery(sql);
// 遍历结果集并输出
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int score = rs.getInt("score");
System.out.println("id:" + id + ",姓名:" + name + ",分数:" + score);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
- 模糊查询操作
模糊查询操作即为根据指定字段进行模糊匹配查询,代码如下:
import java.sql.*;
public class FuzzyQuery {
public static void main(String[] args) {
Connection conn = null; // 数据库连接对象
PreparedStatement pstmt = null; // SQL语句执行对象
ResultSet rs = null; // 结果集对象
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "password");
// 定义SQL语句
String sql = "SELECT * FROM score WHERE name LIKE ?";
// 创建SQL语句执行对象
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%张%"); // 设置查询条件
// 执行SQL语句,获取结果集
rs = pstmt.executeQuery();
// 遍历结果集并输出
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int score = rs.getInt("score");
System.out.println("id:" + id + ",姓名:" + name + ",分数:" + score);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
``
原文地址: https://www.cveoy.top/t/topic/fnPw 著作权归作者所有。请勿转载和采集!