📄 scoresystem.java
字号:
import java.util.*;
import java.sql.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class ScoreSystem extends JFrame
{
private Container content = getContentPane(); //获取内容窗格
private JMenuBar menuBar = new JMenuBar(); //创建菜单栏
private JMenu Setting = new JMenu("操作"); //创建菜单
private JMenu SelectScourse = new JMenu("选择课程");
private JMenu Other = new JMenu("其它");
private JMenuItem Record = new JMenuItem("录入");//创建JMenu菜单项
private JMenuItem Find = new JMenuItem("查询");
private JMenuItem Rework = new JMenuItem("修改");
private JMenuItem Del = new JMenuItem("删除");
private JMenuItem Reset = new JMenuItem("刷新");
private JMenuItem BC = new JMenuItem("C语言"); //创建课程选择菜单
private JMenuItem BJava = new JMenuItem("Java");
private JMenuItem BMath = new JMenuItem("数学");
public static String Scourse ="C";
public static final int RECORD=001; //定义常量,用来区分不同的菜单项
public static final int FIND=002;
public static final int REWORK=003;
public static final int DEL=004;
public static final int RESET=005;
public static final int ABOUT=006;
public static final int EXIT=007;
public static final int BRECORD=101;
public static final int BFIND=102;
public static final int BREWORK=103;
public static final int BDEL=104;
public static final int CBC=105;
public static final int CBJAVA=106;
public static final int CBMATH=107;
private JMenuItem About = new JMenuItem("关于软件...");
private JMenuItem Exit = new JMenuItem("退出!!!");
private JButton BRecord = new JButton("录入");//创建按钮项
private JButton BFind = new JButton("查询");
private JButton BRework = new JButton("修改");
private JButton BDel = new JButton("删除");
JTextField TextNO = new JTextField(8); //定义输入文本框
JTextField TextName = new JTextField(8);
JTextField TextScore = new JTextField(3);
JTextField TextFind = new JTextField(8);
JLabel LaNO = new JLabel("学号"); // 定义标签
JLabel LaName = new JLabel("姓名");
JLabel LaScore = new JLabel("分数");
JLabel State = new JLabel("good");
String[] data = {"学号","姓名"}; //定义查找用的ComboBox
JComboBox FindList = new JComboBox(data);
private JPanel PanelRecord=new JPanel(); //定义录入,查找,更改,删除板块
private JPanel PanelFind=new JPanel();
private JPanel PanelRework=new JPanel();
private JPanel PanelDel=new JPanel();
JPanel BigPanel=new JPanel();
JPanel BigPanel2=new JPanel();
String title[]={"学号","姓名","成绩"};
public static String name,no,score;
int row;
JTable table; //声明table
JScrollPane scroll; //声明滚动条
Vector vector = new Vector(); //创建向量对象
AbstractTableModel tm = new AbstractTableModel() //创建模板
{
public int getColumnCount() //取得表格的列数
{
return title.length;
}
public int getRowCount() //取得表格的行数
{
return vector.size();
}
public Object getValueAt(int row,int column)//取得单元格中的属性
{
if(!vector.isEmpty())
{
return ((Vector)vector.elementAt(row)).elementAt(column);
}
else
{
return null;
}
}
public void setValueAt(Object value,int row,int column)//数据模型不可编辑,改方法设为空
{
}
public String getColumnName(int column) //取得表格名
{
return title[column];
}
public boolean isCellEditable(int row,int column)
{
return false;
}
};
public ScoreSystem(String title)
{
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭窗口
Setting.add(Record); //设置"操作"下拉菜单
Setting.add(Find);
Setting.add(Rework);
Setting.add(Del);
Setting.add(Reset);
SelectScourse.add(BC); //设置课程选择下拉菜单
SelectScourse.add(BJava);
SelectScourse.add(BMath);
Other.add(About); //设置"其它"下拉菜单
Other.add(Exit);
menuBar.add(SelectScourse);
menuBar.add(Setting); //设置下拉菜单
menuBar.add(Other);
setJMenuBar(menuBar); //设置菜单拦
Record.addActionListener(new FileActionListener(RECORD));
Find.addActionListener(new FileActionListener(FIND));
Rework.addActionListener(new FileActionListener(REWORK));
Del.addActionListener(new FileActionListener(DEL));
BRecord.addActionListener(new FileActionListener(BRECORD));
BFind.addActionListener(new FileActionListener(BFIND));
BRework.addActionListener(new FileActionListener(BREWORK));
BDel.addActionListener(new FileActionListener(BDEL));
Reset.addActionListener(new FileActionListener(RESET));
BC.addActionListener(new FileActionListener(CBC));
BJava.addActionListener(new FileActionListener(CBJAVA));
BMath.addActionListener(new FileActionListener(CBMATH));
setPanelRecord(); //设置各个板块
setPanelFind();
setPanelRework();
setPanelDel();
BigPanel.add(PanelRecord); //把各个板块加入到容器中
BigPanel.add(PanelFind);
BigPanel.add(PanelRework);
BigPanel.add(PanelDel);
content.add(BigPanel,BorderLayout.NORTH);
table=new JTable(tm); //生成自己的数据模型
table.setToolTipText("Display,goodboy");//设置帮助提示
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(java.awt.event.MouseEvent e)
{
System.out.println("row numbers is :"+table.getRowCount());//获取表格的总行数
//获取鼠标点击的行的位置(及行数)
Point mousepoint;
mousepoint =e.getPoint();
System.out.println(table.rowAtPoint(mousepoint)+1);
System.out.println("Mouse OK!");
row=0;
row=table.rowAtPoint(mousepoint);
if(row<0) row=0;
Object row_no=table.getValueAt(row,0);
Object row_name=table.getValueAt(row,1);
Object row_score=table.getValueAt(row,2);
TextNO.setText(row_no.toString());
TextName.setText(row_name.toString());
if(row_score != null){TextScore.setText(row_score.toString());}
else{TextScore.setText("0");}
no = row_no.toString();
name = row_name.toString();
score = row_score.toString();
row++;
System.out.println("选择了第"+row+"行");
State.setText("第"+row+"行");
}
});
table.setAutoResizeMode(table.AUTO_RESIZE_SUBSEQUENT_COLUMNS);//设置表格调整尺寸模式
//table.setCellSelectionEnabled(false); //设置单元格选择方式
table.setShowHorizontalLines(true); //设置单元格之间的分割线 显示
table.setShowVerticalLines(true);
scroll = new JScrollPane(table); //给table加上滚动条
scroll.setPreferredSize(new Dimension(530,200));
content.add(scroll);
ReSetTable();
content.add(State,BorderLayout.SOUTH);
}
public void LinkAccess() //数据库连接
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("连接数据库成功");
}
catch(ClassNotFoundException ce)
{
System.out.println("SQLException:"+ce.getMessage());
System.out.println("连接数据库不成功");
}
}
public void ReSetTable() //刷新Table
{
LinkAccess();
try{
Connection con = DriverManager.getConnection("jdbc:odbc:Student");
Statement stmt = con.createStatement();
ResultSet rSet=stmt.executeQuery("select * from "+Scourse+" order by ID");//将查询得到的结果集给rs
vector.removeAllElements(); //初始化向量对象
tm.fireTableStructureChanged(); //更新表格内容
while(rSet.next())
{
Vector rec_vector = new Vector();
rec_vector.addElement(rSet.getString(1));
rec_vector.addElement(rSet.getString(2));
rec_vector.addElement(String.valueOf(rSet.getString(3)));
vector.addElement(rec_vector);
}
tm.fireTableStructureChanged();
System.out.println("刷新"+Scourse+"课程的数据成功!");
stmt.close();
con.close();
}
catch(SQLException ex)
{
System.out.println("\nERROR:-----SQLEXCEPTION------\n");
while(ex!=null)
{
System.out.println("Message:"+ ex.getMessage());
System.out.println("SQLState:"+ ex.getSQLState());
System.out.println("ErrorCode:"+ ex.getErrorCode());
ex = ex.getNextException();
}
}
}
public void RecordAccess() //录入Table
{
LinkAccess();
try
{
Connection con = DriverManager.getConnection("jdbc:odbc:Student");
Statement stmt = con.createStatement();
String sql="insert into "+Scourse+"(ID,Name,Score) values ('"+TextNO.getText()+"','"+TextName.getText()+"',"+Integer.parseInt(TextScore.getText())+");";
stmt.executeUpdate(sql);
TextNO.setText("");
TextName.setText("");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -