Java 数据库课程信息查询 - 使用字符界面和图形界面
Java 数据库课程信息查询 - 使用字符界面和图形界面
本教程演示如何在 Java 中使用 JDBC 连接到数据库(MySQL、Derby 或 Access),并创建名为'course'的表。使用字符界面和图形界面(JTable 和 JComboBox)查询并显示课程信息。
1. 创建'course'表
在您的数据库中创建一个名为'course'的表,包含以下字段:
course_id(课程编号) -varchar(10)course_name(课程名称) -varchar(50)course_type(课程类型) -varchar(20)credit_hours(学分数) -inttotal_hours(总学时) -inttheory_hours(理论学时) -intlab_hours(实验学时) -int
2. 编写字符界面程序
import java.sql.*;
public class CourseQuery {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 1. Load the JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// 2. Establish the connection
String url = "jdbc:mysql://localhost:3306/stuDB";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
// 3. Create a statement object
stmt = conn.createStatement();
// 4. Execute the query
String query = "SELECT * FROM course";
rs = stmt.executeQuery(query);
// 5. Display the results
System.out.println("Course ID\tCourse Name\tCourse Type\tCredit Hours\tTotal Hours\tTheory Hours\tLab Hours");
while (rs.next()) {
String course_id = rs.getString("course_id");
String course_name = rs.getString("course_name");
String course_type = rs.getString("course_type");
int credit_hours = rs.getInt("credit_hours");
int total_hours = rs.getInt("total_hours");
int theory_hours = rs.getInt("theory_hours");
int lab_hours = rs.getInt("lab_hours");
System.out.println(course_id + "\t" + course_name + "\t" + course_type + "\t" + credit_hours + "\t" + total_hours + "\t" + theory_hours + "\t" + lab_hours);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 6. Close the resources
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
3. 编写图形界面程序
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
public class CourseQueryGUI extends JFrame {
private static final long serialVersionUID = 1L;
private JTable table;
public CourseQueryGUI() {
// 1. Load the JDBC driver
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
// 2. Establish the connection
String url = "jdbc:mysql://localhost:3306/stuDB";
String user = "root";
String password = "123456";
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
// 3. Create a statement object
Statement stmt = null;
ResultSet rs = null;
DefaultTableModel model = new DefaultTableModel();
try {
stmt = conn.createStatement();
// 4. Execute the query
String query = "SELECT * FROM course";
rs = stmt.executeQuery(query);
// 5. Create a table model
model.addColumn("Course ID");
model.addColumn("Course Name");
model.addColumn("Course Type");
model.addColumn("Credit Hours");
model.addColumn("Total Hours");
model.addColumn("Theory Hours");
model.addColumn("Lab Hours");
while (rs.next()) {
String course_id = rs.getString("course_id");
String course_name = rs.getString("course_name");
String course_type = rs.getString("course_type");
int credit_hours = rs.getInt("credit_hours");
int total_hours = rs.getInt("total_hours");
int theory_hours = rs.getInt("theory_hours");
int lab_hours = rs.getInt("lab_hours");
Object[] row = {course_id, course_name, course_type, credit_hours, total_hours, theory_hours, lab_hours};
model.addRow(row);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 6. Close the resources
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 7. Create a JTable and add it to the frame
table = new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane, BorderLayout.CENTER);
// 8. Set the frame properties
setTitle("Course Query");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
new CourseQueryGUI();
}
}
注意:
- 将代码中的数据库连接信息(url、user、password)替换为您的实际连接信息。
- 在运行代码之前,确保已安装并配置了所需的数据库驱动程序(例如,MySQL 驱动程序)。
- 您可以根据需要修改代码,例如添加搜索功能,使用 JComboBox 选择课程类型等。
额外提示:
- 可以使用预编译语句来提高查询效率。
- 可以使用 PreparedStatement 对象来安全地处理用户输入,避免 SQL 注入攻击。
- 使用 try-with-resources 语句来确保资源的正确关闭。
希望本教程能够帮助您学习如何在 Java 中使用 JDBC 连接到数据库并查询数据。
原文地址: https://www.cveoy.top/t/topic/odCK 著作权归作者所有。请勿转载和采集!