在stuDB数据库中建立一个course信息表其字段包括课程编号、课程名称、课程类型、学分数、总学时、理论学时、实验学时等。分别编写字符界面和图形界面的程序实现对course表中课程信息的查询和显示功能。建议在图形界面中利用JTable或JComboBox等组件来显示从数据表中查询到的信息。数据库系统可以选择Apache Derby、MySQL、Access等
1.在stuDB数据库中创建一个名为“course”的表,包括以下字段:
- course_id (课程编号) - varchar(10)
- course_name (课程名称) - varchar(50)
- course_type (课程类型) - varchar(20)
- credit_hours (学分数) - int
- total_hours (总学时) - int
- theory_hours (理论学时) - int
- lab_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();
}
原文地址: https://www.cveoy.top/t/topic/fnKE 著作权归作者所有。请勿转载和采集!