大家好,又见面了,我是你们的朋友全栈君。
文章目录
系统概述
查询学生的个人基本信息,查询课程表、选课情况,查询课程的成绩信息。其中课程表及选课信息和成绩信息无法改动,个人基本信息可以添加或者删除。
这是一个比较简单的管理系统,具备简单的添删查功能,适合学习用。
首先设计好数据库,软件为Microsoft SQL server
- 表名:成绩信息,课程信息,学生基本信息
- 列名,字段属性:
学生基本信息:学号varchar(8) notnull,姓名char(10)notnull,性别varchar(2)notnull,出生年月varchar(20),籍贯varchar(20),班级varchar(20),专业varchar(20),系varchar(20)
课程信息:课程号varchar(8)notnull,课程名varchar(20)notnull,学分 tinyint,学时 tinyint
成绩信息:学号 varchar(8)notnull,课程号 char(8)notnull,成绩 tinyint,已获学分tinyint - 主键:学生基本信息:学号
课程信息:课程号
成绩信息:学号,课程号
设置好后,随便往数据库里输入两个人的信息
设计类:
主界面类:MainFrame
学生信息界面类:StuInfo
课程信息界面类:ClassInfo
成绩信息界面类:ScoreInfo
添加学生信息方法类:Add
添加学生信息界面类:AddFrame
删除学生信息方法类:Delete
删除学生信息界面类:DeleteFrame
代码段
MainFrame.java
package StuManager;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MainFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
setTitle("\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(600, 300, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=学生管理系统数据库";
String userName="sa";
String pwd="w12621058";
String tab1="学生基本信息";
String tab2="课程信息";
String tab3="成绩信息";
JButton btnNewButton = new JButton("\u5B66\u751F\u4FE1\u606F\u67E5\u8BE2");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new StuInfo(driver,url,userName,pwd,tab1);
}
});
btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
btnNewButton.setBounds(125, 98, 171, 27);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u8BFE\u7A0B\u67E5\u8BE2");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ClassInfo(driver,url,userName,pwd,tab2);
}
});
btnNewButton_1.setFont(new Font("微软雅黑", Font.BOLD, 15));
btnNewButton_1.setBounds(151, 149, 113, 27);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("\u5B66\u5206\u67E5\u8BE2");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ScoreInfo(driver,url,userName,pwd,tab3);
}
});
btnNewButton_2.setFont(new Font("微软雅黑", Font.BOLD, 15));
btnNewButton_2.setBounds(151, 199, 113, 27);
contentPane.add(btnNewButton_2);
JLabel label = new JLabel("\u6B22\u8FCE\u6765\u5230\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF(\u6559\u5E08\u6A21\u5F0F)");
label.setFont(new Font("微软雅黑", Font.BOLD, 25));
label.setBounds(35, 40, 383, 43);
contentPane.add(label);
JMenuBar menuBar = new JMenuBar();
menuBar.setToolTipText("");
menuBar.setBounds(0, 0, 432, 27);
contentPane.add(menuBar);
JMenu mnNewMenu = new JMenu("\u4FE1\u606F\u66F4\u65B0");
menuBar.add(mnNewMenu);
JMenuItem mntmNewMenuItem = new JMenuItem("\u5B66\u751F\u4FE1\u606F\u5F55\u5165");
mntmNewMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddFrame af1=new AddFrame(driver,url,userName,pwd);
af1.setVisible(true);
}
});
mnNewMenu.add(mntmNewMenuItem);
JMenuItem menuItem = new JMenuItem("\u5B66\u751F\u4FE1\u606F\u5220\u9664");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DeleteFrame df1=new DeleteFrame(driver,url,userName,pwd);
df1.setVisible(true);
}
});
mnNewMenu.add(menuItem);
}
}
StuInfo.java
package StuManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
class StuInfo extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JTable table=new JTable();
private Connection conn;
public StuInfo(String drv,String url,String usr,String pwd,String tb)
{
this.setBounds(500,300,800,200);
this.setTitle("学生基本信息");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try{
Class.forName(drv);
this.conn=DriverManager.getConnection(url,usr,pwd);
table=query(tb);
} catch(Exception e){e.printStackTrace();}
this.getContentPane().add(new JScrollPane(table));
this.setVisible(true);
try{conn.close();}catch(Exception e){e.printStackTrace();}
}
public JTable query(String table) throws SQLException{
DefaultTableModel tbmode=new DefaultTableModel();
String sql="SELECT * FROM "+table+";";
try{
Statement Stmt=conn.createStatement();
ResultSet rs= Stmt.executeQuery(sql);
ResultSetMetaData meta=rs.getMetaData();
int colcount=meta.getColumnCount();
for(int i=1;i<=colcount;i++)
tbmode.addColumn(meta.getColumnName(i));
Object[]col=new Object[colcount];
while(rs.next()){
for(int j=1;j<=col.length;j++)
col[j-1]=rs.getString(j);
tbmode.addRow(col);
}
rs.close();
Stmt.close();
}catch(Exception e){e.printStackTrace();}
return new JTable(tbmode);
}
}
ClassInfo.java
package StuManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
class ClassInfo extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JTable table=new JTable();
private Connection conn;
public ClassInfo(String drv,String url,String usr,String pwd,String tb)
{
this.setBounds(500,300,800,200);
this.setTitle("课程信息");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try{
Class.forName(drv);
this.conn=DriverManager.getConnection(url,usr,pwd);
table=query(tb);
} catch(Exception e){e.printStackTrace();}
this.getContentPane().add(new JScrollPane(table));
this.setVisible(true);
try{conn.close();}catch(Exception e){e.printStackTrace();}
}
public JTable query(String table) throws SQLException{
DefaultTableModel tbmode=new DefaultTableModel();
String sql="SELECT * FROM "+table+";";
try{
Statement Stmt=conn.createStatement();
ResultSet rs= Stmt.executeQuery(sql);
ResultSetMetaData meta=rs.getMetaData();
int colcount=meta.getColumnCount();
for(int i=1;i<=colcount;i++)
tbmode.addColumn(meta.getColumnName(i));
Object[]col=new Object[colcount];
while(rs.next()){
for(int j=1;j<=col.length;j++)
col[j-1]=rs.getString(j);
tbmode.addRow(col);
}
rs.close();
Stmt.close();
}catch(Exception e){e.printStackTrace();}
return new JTable(tbmode);
}
}
ScoreInfo.java
package StuManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
class ScoreInfo extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JTable table=new JTable();
private Connection conn;
public ScoreInfo(String drv,String url,String usr,String pwd,String tb)
{
this.setBounds(500,300,800,200);
this.setTitle("成绩信息");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try{
Class.forName(drv);
this.conn=DriverManager.getConnection(url,usr,pwd);
table=query(tb);
} catch(Exception e){e.printStackTrace();}
this.getContentPane().add(new JScrollPane(table));
this.setVisible(true);
try{conn.close();}catch(Exception e){e.printStackTrace();}
}
public JTable query(String table) throws SQLException{
DefaultTableModel tbmode=new DefaultTableModel();
String sql="SELECT * FROM "+table+";";
try{
Statement Stmt=conn.createStatement();
ResultSet rs= Stmt.executeQuery(sql);
ResultSetMetaData meta=rs.getMetaData();
int colcount=meta.getColumnCount();
for(int i=1;i<=colcount;i++)
tbmode.addColumn(meta.getColumnName(i));
Object[]col=new Object[colcount];
while(rs.next()){
for(int j=1;j<=col.length;j++)
col[j-1]=rs.getString(j);
tbmode.addRow(col);
}
rs.close();
Stmt.close();
}catch(Exception e){e.printStackTrace();}
return new JTable(tbmode);
}
}
实际上这三个Info类除了连接的表不同,获取表的函数完全一样
Add.java
package StuManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class Add{
private Connection conn;
public Add(String a,String b,String c,String d,String i,String f,String g,String h,String drv,String url,String usr,String pwd){
try{
Class.forName(drv);
conn=DriverManager.getConnection(url,usr,pwd);
String sql="insert into 学生基本信息(学号,姓名,性别,出生年月,籍贯,班级,专业,系) VALUES('"+a+"','"+b+"','"+d+"','"+c+"','"+i+"','"+f+"','"+g+"','"+h+"')";
java.sql.Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
conn.close();
JOptionPane.showMessageDialog(null, "添加成功!");
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "添加失败!");
e.printStackTrace();
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, "添加失败!");
e.printStackTrace();
}
}
}
Delete.java
package StuManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class Delete {
private Connection conn;
public Delete(String drv,String url,String usr,String pwd,String tb){
try{
Class.forName(drv);
conn=DriverManager.getConnection(url,usr,pwd);
String sql1="delete from 学生基本信息 where 学号='"+tb+"'";
java.sql.Statement stmt1=conn.createStatement();
stmt1.executeUpdate(sql1);
String sql2="delete from 成绩信息 where 学号='"+tb+"'";
Statement stmt2 = conn.createStatement();
stmt2.executeUpdate(sql2);
conn.close();
JOptionPane.showMessageDialog(null, "删除成功!");
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "删除失败!");
e.printStackTrace();
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, "删除失败!");
e.printStackTrace();
}
}
}
AddFrame.java
package StuManager;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class AddFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
JButton btnNewButton;
/**
* Create the frame.
*/
public AddFrame(String drv,String url,String usr,String pwd) {
setTitle("\u6DFB\u52A0\u5B66\u751F\u4FE1\u606F");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(500, 200, 450, 528);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel label = new JLabel("\u5B66\u53F7\uFF08\u5FC5\u586B\uFF09");
label.setBounds(69, 40, 90, 20);
label.setFont(new Font("微软雅黑", Font.BOLD, 15));
JLabel label_1 = new JLabel("\u59D3\u540D\uFF08\u5FC5\u586B\uFF09");
label_1.setBounds(69, 82, 90, 20);
label_1.setFont(new Font("微软雅黑", Font.BOLD, 15));
JLabel label_2 = new JLabel("\u6027\u522B\uFF08\u5FC5\u586B\uFF09");
label_2.setBounds(69, 124, 90, 20);
label_2.setFont(new Font("微软雅黑", Font.BOLD, 15));
JLabel label_3 = new JLabel("\u51FA\u751F\u5E74\u6708");
label_3.setBounds(69, 166, 60, 20);
label_3.setFont(new Font("微软雅黑", Font.BOLD, 15));
JLabel label_4 = new JLabel("\u7C4D\u8D2F");
label_4.setBounds(69, 208, 30, 20);
label_4.setFont(new Font("微软雅黑", Font.BOLD, 15));
JLabel label_5 = new JLabel("\u73ED\u7EA7");
label_5.setBounds(69, 250, 30, 20);
label_5.setFont(new Font("微软雅黑", Font.BOLD, 15));
JLabel label_6 = new JLabel("\u4E13\u4E1A");
label_6.setBounds(69, 292, 30, 20);
label_6.setFont(new Font("微软雅黑", Font.BOLD, 15));
JLabel label_7 = new JLabel("\u7CFB");
label_7.setBounds(69, 334, 15, 20);
label_7.setFont(new Font("微软雅黑", Font.BOLD, 15));
btnNewButton = new JButton("\u6DFB\u52A0");
btnNewButton.setBounds(175, 418, 74, 29);
btnNewButton.setForeground(Color.BLACK);
btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
textField = new JTextField();
textField.setBounds(219, 39, 153, 24);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(219, 81, 153, 24);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(219, 123, 153, 24);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(219, 165, 153, 24);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(219, 207, 153, 24);
textField_4.setColumns(10);
textField_5 = new JTextField();
textField_5.setBounds(219, 249, 153, 24);
textField_5.setColumns(10);
textField_6 = new JTextField();
textField_6.setBounds(219, 291, 153, 24);
textField_6.setColumns(10);
textField_7 = new JTextField();
textField_7.setBounds(219, 333, 153, 24);
textField_7.setColumns(10);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnNewButton){
String a=textField.getText();
String b=textField_1.getText();
String c=textField_2.getText();
String d=textField_3.getText();
String i=textField_4.getText();
String f=textField_5.getText();
String g=textField_6.getText();
String h=textField_7.getText();
new Add(a,b,d,c,i,f,g,h,drv,url,usr,pwd);
}
}
});
contentPane.setLayout(null);
contentPane.add(label);
contentPane.add(label_1);
contentPane.add(label_2);
contentPane.add(label_4);
contentPane.add(label_3);
contentPane.add(label_5);
contentPane.add(label_6);
contentPane.add(label_7);
contentPane.add(textField_7);
contentPane.add(textField_6);
contentPane.add(textField_5);
contentPane.add(textField_4);
contentPane.add(textField_3);
contentPane.add(textField);
contentPane.add(textField_1);
contentPane.add(textField_2);
contentPane.add(btnNewButton);
}
}
DeleteFrame.java
package StuManager;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class DeleteFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
/**
* Create the frame.
*/
public DeleteFrame(String drv,String url,String usr,String pwd) {
setTitle("\u5220\u9664\u4FE1\u606F");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(500, 200, 451, 122);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
textField = new JTextField();
textField.setBounds(127, 28, 135, 24);
textField.setColumns(10);
JButton button = new JButton("\u5220\u9664");
button.setBounds(293, 26, 72, 27);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String a=textField.getText();
new Delete(drv,url,usr,pwd,a);
}
});
contentPane.setLayout(null);
button.setForeground(Color.RED);
button.setFont(new Font("微软雅黑", Font.BOLD, 15));
contentPane.add(button);
contentPane.add(textField);
JLabel label = new JLabel("\u5B66\u53F7\uFF1A");
label.setFont(new Font("宋体", Font.BOLD, 15));
label.setBounds(63, 31, 48, 18);
contentPane.add(label);
}
}
运行
主界面
学生信息查询表
添加信息
删除信息
课程信息查询
成绩信息查询
相关问题会持续更新~
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/158047.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...