一、实验目的
1 设计数据库;
2 概念分析;
3 登录功能实现。
二、 实验内容
1 登录Login.java
package 学生成绩管理系统登录界面;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class Login {
private JFrame frame;
private JTextField textField;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login window = new Login();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Login() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF-\u767B\u5F55");
frame.setResizable(false);
frame.setBounds(400, 200, 463, 431);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new CompoundBorder(new EmptyBorder(3, 0, 0, 0), null));
panel.setBounds(15, 0, 442, 332);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel label = new JLabel("\u7B80\u5355\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
label.setFont(new Font("宋体", Font.BOLD, 25));
label.setBounds(108, 71, 246, 68);
panel.add(label);
JLabel label_1 = new JLabel("\u8EAB \u4EFD\uFF1A");
label_1.setFont(new Font("宋体", Font.BOLD, 20));
label_1.setBounds(53, 152, 85, 29);
panel.add(label_1);
JRadioButton Teacher = new JRadioButton("\u6559\u5E08 ");
Teacher.setSelected(true);
buttonGroup.add(Teacher);
Teacher.setFont(new Font("宋体", Font.BOLD, 20));
Teacher.setBounds(149, 150, 102, 33);
panel.add(Teacher);
JRadioButton Student = new JRadioButton("\u5B66\u751F");
buttonGroup.add(Student);
Student.setFont(new Font("宋体", Font.BOLD, 20));
Student.setBounds(265, 150, 111, 33);
panel.add(Student);
JLabel label_1_1 = new JLabel("\u7528\u6237\u540D\uFF1A");
label_1_1.setFont(new Font("宋体", Font.BOLD, 20));
label_1_1.setBounds(53, 196, 85, 29);
panel.add(label_1_1);
JLabel label_1_2 = new JLabel("\u5BC6 \u7801\uFF1A");
label_1_2.setFont(new Font("宋体", Font.BOLD, 20));
label_1_2.setBounds(53, 240, 85, 29);
panel.add(label_1_2);
textField = new JTextField();
textField.setBounds(149, 197, 181, 28);
panel.add(textField);
textField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(149, 241, 181, 28);
panel.add(passwordField);
JButton button = new JButton("\u767B\u5F55");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(Teacher.isSelected()){
if(textField.getText().equals("admin")&&passwordField.getText().equals("123")){
TeacherMain login=new TeacherMain();
login.setVisible(true);
frame.setVisible(false);
}
}
if(Student.isSelected()){
if(textField.getText().equals("admin")&&passwordField.getText().equals("123")){
StudentMain login=new StudentMain();
login.setVisible(true);
frame.setVisible(false);
}
}
}
});
button.setFont(new Font("宋体", Font.BOLD, 20));
button.setBounds(216, 347, 79, 29);
frame.getContentPane().add(button);
JButton button_1 = new JButton("\u53D6\u6D88");
button_1.setFont(new Font("宋体", Font.BOLD, 20));
button_1.setBounds(310, 347, 79, 29);
frame.getContentPane().add(button_1);
}
}
2 教师端 TeacherMain.java
package 学生成绩管理系统登录界面;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class TeacherMain extends JFrame {
Connection conn = null;
Statement stmt = null;
String DB_URL = "jdbc:mysql://localhost:3306/student";
String user = "root";
String pass = "123456";
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TeacherMain frame = new TeacherMain();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TeacherMain() {
setResizable(false);
setTitle("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF-\u6559\u5E08\u7AEF");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(400, 200, 514, 385);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("\u7CFB\u7EDF\u7BA1\u7406");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("\u9000\u51FA");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
menu.add(menuItem);
JMenu menu_1 = new JMenu("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406");
menuBar.add(menu_1);
JMenuItem menuItem_1 = new JMenuItem("\u6DFB\u52A0\u5B66\u751F\u4FE1\u606F");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
AddStu stu=new AddStu();
stu.setVisible(true);
}
});
menu_1.add(menuItem_1);
JMenuItem menuItem_4 = new JMenuItem("\u67E5\u8BE2\u5B66\u751F\u4FE1\u606F");
menuItem_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
QueryStu stu=new QueryStu();
stu.setVisible(true);
}
});
menu_1.add(menuItem_4);
JMenuItem menuItem_2 = new JMenuItem("\u4FEE\u6539\u5B66\u751F\u4FE1\u606F");
menu_1.add(menuItem_2);
JMenuItem menuItem_3 = new JMenuItem("\u5220\u9664\u5B66\u751F\u4FE1\u606F");
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DelStu stu=new DelStu();
stu.setVisible(true);
}
});
menu_1.add(menuItem_3);
JMenu menu_2 = new JMenu("\u5B66\u751F\u6210\u7EE9\u7BA1\u7406");
menuBar.add(menu_2);
JMenuItem menuItem_5 = new JMenuItem("\u6DFB\u52A0\u5B66\u751F\u6210\u7EE9");
menu_2.add(menuItem_5);
JMenuItem menuItem_6 = new JMenuItem("\u67E5\u8BE2\u5B66\u751F\u6210\u7EE9");
menu_2.add(menuItem_6);
JMenuItem menuItem_7 = new JMenuItem("\u4FEE\u6539\u5B66\u751F\u6210\u7EE9");
menu_2.add(menuItem_7);
JMenu menu_3 = new JMenu("\u5E2E\u52A9");
menuBar.add(menu_3);
JMenuItem menuItem_8 = new JMenuItem("\u5173\u4E8E\u672C\u8F6F\u4EF6");
menu_3.add(menuItem_8);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 498, 326);
contentPane.add(panel);
}
}
3 添加学生信息 AddStu.java
package 学生成绩管理系统登录界面;
import java.io.*;
import java.sql.*;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class AddStu extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField NO;
private JTextField Name;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField Age;
private JTextField BirDate;
private JTextField ClassA;
private JTextField Speci;
private JTextField Mzu;
private JPasswordField PassWord;
protected String Spec1;
protected String Minzu1;
protected String Password1;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
AddStu dialog = new AddStu();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public AddStu() {
setResizable(false);
setTitle("\u6DFB\u52A0\u5B66\u751F\u4FE1\u606F");
setBounds(100, 100, 538, 611);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JLabel label = new JLabel("\u8F93\u5165\u5B66\u751F\u57FA\u672C\u4FE1\u606F");
label.setBounds(170, 12, 234, 59);
label.setFont(new Font("宋体", Font.BOLD, 25));
label.setForeground(Color.RED);
contentPanel.add(label);
JLabel label_1 = new JLabel("\u5B66\u53F7\uFF1A");
label_1.setBounds(111, 61, 89, 48);
label_1.setFont(new Font("宋体", Font.BOLD, 22));
contentPanel.add(label_1);
NO = new JTextField();
NO.setBounds(241, 73, 163, 27);
contentPanel.add(NO);
NO.setColumns(10);
JLabel label_1_1 = new JLabel("\u59D3\u540D\uFF1A");
label_1_1.setBounds(111, 107, 89, 48);
label_1_1.setFont(new Font("宋体", Font.BOLD, 22));
contentPanel.add(label_1_1);
Name = new JTextField();
Name.setBounds(241, 115, 163, 27);
Name.setColumns(10);
contentPanel.add(Name);
JLabel label_1_1_1 = new JLabel("\u5E74\u9F84\uFF1A");
label_1_1_1.setBounds(111, 204, 89, 48);
label_1_1_1.setFont(new Font("宋体", Font.BOLD, 22));
contentPanel.add(label_1_1_1);
JLabel label_1_1_2 = new JLabel("\u6027\u522B\uFF1A");
label_1_1_2.setBounds(111, 153, 89, 48);
label_1_1_2.setFont(new Font("宋体", Font.BOLD, 22));
contentPanel.add(label_1_1_2);
JRadioButton Nan = new JRadioButton("\u7537");
Nan.setBounds(251, 160, 61, 29);
buttonGroup.add(Nan);
Nan.setSelected(true);
contentPanel.add(Nan);
JRadioButton Nv = new JRadioButton("\u5973");
Nv.setBounds(337, 160, 61, 29);
buttonGroup.add(Nv);
contentPanel.add(Nv);
Age = new JTextField();
Age.setBounds(242, 212, 163, 27);
Age.setColumns(10);
contentPanel.add(Age);
JLabel label_1_1_1_1 = new JLabel("\u51FA\u751F\u65E5\u671F\uFF1A");
label_1_1_1_1.setBounds(111, 250, 116, 48);
label_1_1_1_1.setFont(new Font("宋体", Font.BOLD, 22));
contentPanel.add(label_1_1_1_1);
BirDate = new JTextField();
BirDate.setBounds(242, 262, 163, 27);
BirDate.setColumns(10);
contentPanel.add(BirDate);
JLabel label_1_1_1_2 = new JLabel("\u73ED\u7EA7\uFF1A");
label_1_1_1_2.setBounds(111, 293, 89, 48);
label_1_1_1_2.setFont(new Font("宋体", Font.BOLD, 22));
contentPanel.add(label_1_1_1_2);
ClassA = new JTextField();
ClassA.setBounds(242, 301, 163, 27);
ClassA.setColumns(10);
contentPanel.add(ClassA);
JLabel label_1_1_1_2_1 = new JLabel("\u4E13\u4E1A\uFF1A");
label_1_1_1_2_1.setBounds(111, 343, 89, 48);
label_1_1_1_2_1.setFont(new Font("宋体", Font.BOLD, 22));
contentPanel.add(label_1_1_1_2_1);
Speci = new JTextField();
Speci.setBounds(242, 351, 163, 27);
Speci.setColumns(10);
contentPanel.add(Speci);
JLabel label_1_1_1_2_2 = new JLabel("\u6C11\u65CF\uFF1A");
label_1_1_1_2_2.setBounds(111, 396, 89, 48);
label_1_1_1_2_2.setFont(new Font("宋体", Font.BOLD, 22));
contentPanel.add(label_1_1_1_2_2);
Mzu = new JTextField();
Mzu.setBounds(242, 404, 163, 27);
Mzu.setColumns(10);
contentPanel.add(Mzu);
JLabel label_1_1_1_2_2_1 = new JLabel("\u767B\u5F55\u5BC6\u7801\uFF1A");
label_1_1_1_2_2_1.setBounds(111, 448, 116, 48);
label_1_1_1_2_2_1.setFont(new Font("宋体", Font.BOLD, 22));
contentPanel.add(label_1_1_1_2_2_1);
PassWord = new JPasswordField();
PassWord.setBounds(241, 460, 163, 27);
contentPanel.add(PassWord);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("\u786E\u5B9A");
okButton.addActionListener(new ActionListener() {
private String ClassA1;
public void actionPerformed(ActionEvent arg0) {
String NO1=NO.getText();
String Name1=Name.getText();
String Age1=Age.getText();
String BirDate1=BirDate.getText();
String Speci1=Speci.getText();
String Mzu1=Mzu.getText();
String Class1=ClassA.getText();
String PassWord1=PassWord.getText();
String Sex="";
if(Nan.isSelected())Sex="男";
else Sex="女";
System.out.println(NO1);
System.out.println(Name1);
System.out.println(Age1);
System.out.println(BirDate1);
System.out.println(Sex);
System.out.println(Class1);
System.out.println(Speci1);
System.out.println(Mzu1);
System.out.println(PassWord1);
Connection conn = null;
Statement stmt = null;
String DB_URL = "jdbc:mysql://localhost:3306/学生成绩管理系统";
String user = "root";
String pass = "123456";
try {
// (1)注册 JDBC 驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// (2)创建连接
conn = DriverManager.getConnection(DB_URL, user, pass);
// (3)实例化Statement对象,执行SQL语句
stmt = conn.createStatement();
String sql = "insert into 学籍信息表(学号,姓名,年龄,出生日期,性别,班级,专业,民族,登录密码) VALUES ("+"'"+NO1+"',"+"'"+Name1+"',"+"'"+Age1+"',"+"'"+BirDate1+"',"+"'"+Sex+"',"+"'"+Class1+"',"+"'"+Speci1+"',"+"'"+Mzu1+"',"+"'"+PassWord1+"'"+")";
int hasResultSet = stmt.executeUpdate(sql);
ResultSet rs = stmt.executeQuery("SELECT * FROM 学籍信息表");
while (rs.next()) {
// (5)通过字段检索,进行查询操作
// 输出数据
System.out.print(" " + rs.getString("学号"));
System.out.print(" " + rs.getString("姓名"));
System.out.print(" " + rs.getString("年龄"));
System.out.print(" " + rs.getString("出生日期"));
System.out.print(" " + rs.getString("性别"));
System.out.print(" " + rs.getString("班级"));
System.out.print(" " + rs.getString("专业"));
System.out.print(" " + rs.getString("民族"));
System.out.print("\n");
}
// (6)完成后关闭
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
System.out.print(e);
}
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("\u53D6\u6D88");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}
4 查询信息 QueryStu.java
package 学生成绩管理系统登录界面;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.event.ActionEvent;
import javax.swing.ListSelectionModel;
import javax.swing.JTextArea;
public class QueryStu extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField textField;
JTextArea txtContent;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
QueryStu dialog = new QueryStu();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public QueryStu() {
setResizable(false);
setTitle("\u5B66\u751F\u4FE1\u606F\u67E5\u8BE2");
setBounds(100, 100, 774, 503);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JLabel label = new JLabel("\u8F93\u5165\u5B66\u53F7\uFF1A");
label.setFont(new Font("宋体", Font.BOLD, 25));
label.setBounds(86, 34, 132, 48);
contentPanel.add(label);
textField = new JTextField();
textField.setBounds(245, 41, 186, 39);
contentPanel.add(textField);
textField.setColumns(10);
{
JButton okButton = new JButton("\u67E5\u8BE2");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Connection conn = null;
Statement stmt = null;
String DB_URL = "jdbc:mysql://localhost:3306/学生成绩管理系统";
String user = "root";
String pass = "123456";
try {
// (1)注册 JDBC 驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// (2)创建连接
conn = DriverManager.getConnection(DB_URL, user, pass);
// (3)实例化Statement对象,执行SQL语句
stmt = conn.createStatement();
String tmp=textField.getText();
String sql="";
if(tmp=="") sql="SELECT * FROM 学籍信息表";
else
sql="SELECT * FROM 学籍信息表 where 学号 like "+"'"+tmp+"%'";
boolean hasResultSet =stmt.execute(sql);
ResultSet rs = stmt.getResultSet();//(4)
while (rs.next()) {
// (5)通过字段检索,进行查询操作
// 输出数据
String temp=" " + rs.getString("学号")+
" " + rs.getString("姓名")+
" " + rs.getString("年龄")+
" " + rs.getString("出生日期")+
" " + rs.getString("性别")+
" " + rs.getString("班级")+
" " + rs.getString("专业")+
" " + rs.getString("民族")+"\n";
txtContent.append(temp);
}
// (6)完成后关闭
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
System.out.print(e);
}}
});
okButton.setBounds(483, 46, 84, 36);
contentPanel.add(okButton);
okButton.setActionCommand("OK");
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("\u53D6\u6D88");
cancelButton.setBounds(582, 46, 87, 36);
contentPanel.add(cancelButton);
cancelButton.setActionCommand("Cancel");
}
txtContent = new JTextArea();
txtContent.setBounds(60, 97, 672, 341);
contentPanel.add(txtContent);
JTable table = new JTable();
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setBounds(102, 348, 499, -181);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
}
}
}
5 删除信息DelStu.java
package 学生成绩管理系统登录界面;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.event.ActionEvent;
public class DelStu extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField textField;
private JTable table_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
DelStu dialog = new DelStu();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public DelStu() {
setResizable(false);
setTitle("\u5B66\u751F\u4FE1\u606F\u5220\u9664");
setBounds(100, 100, 774, 503);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JLabel label = new JLabel("\u8F93\u5165\u5B66\u53F7\uFF1A");
label.setFont(new Font("宋体", Font.BOLD, 25));
label.setBounds(86, 34, 132, 48);
contentPanel.add(label);
textField = new JTextField();
textField.setBounds(245, 41, 186, 39);
contentPanel.add(textField);
textField.setColumns(10);
{
JButton okButton = new JButton("\u5220\u9664");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Connection conn = null;
Statement stmt = null;
String DB_URL = "jdbc:mysql://localhost:3306/学生信息管理系统";
String user = "root";
String pass = "12345";
try {
// (1)注册 JDBC 驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// (2)创建连接
conn = DriverManager.getConnection(DB_URL, user, pass);
// (3)实例化Statement对象,执行SQL语句
stmt = conn.createStatement();
String tmp=textField.getText().trim();
String sql = "delete 学籍信息表 where ID="+"'"+tmp+"'";
int hasResultSet =stmt.executeUpdate(sql);
ResultSet rs = stmt.getResultSet();//(4)
while (rs.next()) {
// (5)通过字段检索,进行查询操作
// 输出数据
System.out.print(" " + rs.getString("学号"));
System.out.print(" " + rs.getString("姓名"));
System.out.print(" " + rs.getString("年龄"));
System.out.print(" " + rs.getString("出生日期"));
System.out.print(" " + rs.getString("性别"));
System.out.print(" " + rs.getString("班级"));
System.out.print(" " + rs.getString("专业"));
System.out.print(" " + rs.getString("民族"));
System.out.print(" " + rs.getString("登录密码"));
System.out.print("\n");
}
// (6)完成后关闭
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
System.out.print(e);
}}
});
okButton.setBounds(483, 46, 84, 36);
contentPanel.add(okButton);
okButton.setActionCommand("OK");
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("\u53D6\u6D88");
cancelButton.setBounds(582, 46, 87, 36);
contentPanel.add(cancelButton);
cancelButton.setActionCommand("Cancel");
}
JTable table = new JTable();
table.setBounds(102, 348, 499, -129);
contentPanel.add(table);
table_1 = new JTable();
table_1.setBounds(0, 0, 1, 1);
contentPanel.add(table_1);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
}
}
}
6 查询全部QueryAll.java
package 学生成绩管理系统登录界面;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class QueryAll {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
String DB_URL = "jdbc:mysql://localhost:3306/学生信息管理系统";
String user = "root";
String pass = "12345";
try {
// (1)注册 JDBC 驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// (2)创建连接
conn = DriverManager.getConnection(DB_URL, user, pass);
// (3)实例化Statement对象,执行SQL语句
stmt = conn.createStatement();
String sql = "SELECT * FROM 学籍信息表";
ResultSet rs = stmt.executeQuery(sql);//(4)
System.out.print("学号 " +"姓 名 " + "年龄 "+"出生日期 "+"性别 "+"班级 "+"专业 "+"民族"+"登录密码 \n");
while (rs.next()) {
// (5)通过字段检索,进行查询操作
// 输出数据
System.out.print(" "+rs.getInt("学号"));
System.out.print(" "+rs.getString("姓名"));
System.out.print(" "+rs.getString("年龄"));
System.out.print(" "+rs.getInt("出生日期"));
System.out.print(" "+rs.getString("性别"));
System.out.print(" "+rs.getString("班级"));
System.out.print(" "+rs.getString("专业"));
System.out.print(" "+rs.getString("民族"));
System.out.print(" "+rs.getString("登录密码"));
System.out.print("\n");
}
// (6)完成后关闭
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
System.out.print(e);
}
}
}