📄 addrecord.java
字号:
package cn.com.studentsystem.kaoqin;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import cn.com.studentsystem.exception.NumberOutException;
import cn.com.studentsystem.log.Log;
import cn.com.util.DBConnection;
import cn.com.util.studentsystemcommon.JDatePicker;
public class AddRecord {
JFrame jf;
JDatePicker date_text;
JTextField be_text;
JTextField now_text;
JTextField late_text;
JTextField expalin_text;
JButton add_button;
JButton cancel_button;
JOptionPane jop = new JOptionPane() ;
public AddRecord(){
init();
}
public void init(){
jf = new JFrame("考勤记录");
jf.setSize(420,480);
jf.setLocationRelativeTo(null);
BorderLayout border = new BorderLayout();
FlowLayout flow = new FlowLayout(FlowLayout.CENTER,50,50);
GridLayout grid = new GridLayout(5,1,10,10);
ImageIcon i = new ImageIcon("image//title.png");
jf.setIconImage(i.getImage());
JSplitPane slipt_pane = new JSplitPane();
JPanel up_pane = new JPanel();
JPanel left_pane = new JPanel();
JPanel right_pane = new JPanel();
JPanel all_pane = new JPanel();
up_pane.setBorder(BorderFactory.createTitledBorder("时间日期"));
left_pane.setBorder(BorderFactory.createTitledBorder("人数统计"));
right_pane.setBorder(BorderFactory.createTitledBorder("迟到缘由"));
left_pane.setLayout(flow);
right_pane.setLayout(flow);
all_pane.setLayout(border);
JLabel date_label = new JLabel("时间日期");
date_text = new JDatePicker();
JLabel be_label = new JLabel("应到人数");
be_text = new JTextField(10);
JLabel now_label = new JLabel("实到人数");
now_text = new JTextField(10);
JLabel late_person = new JLabel("迟到者");
late_text = new JTextField(10);
JLabel explain = new JLabel("迟到缘由");
expalin_text = new JTextField(10);
add_button = new JButton("添加");
cancel_button = new JButton("取消");
up_pane.add(date_label);
up_pane.add(date_text);
left_pane.add(be_label);
left_pane.add(be_text);
left_pane.add(late_person);
left_pane.add(late_text);
left_pane.add(add_button);
right_pane.add(now_label);
right_pane.add(now_text);
right_pane.add(explain);
right_pane.add(expalin_text);
right_pane.add(cancel_button);
slipt_pane.setLeftComponent(left_pane);
slipt_pane.setRightComponent(right_pane);
slipt_pane.setAutoscrolls(true);
slipt_pane.setDividerSize(2);
slipt_pane.setDividerLocation(200);
all_pane.add(up_pane,"North");
all_pane.add(slipt_pane,"Center");
jf.add(all_pane);
class KaoqinAction implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try{
if(arg0.getActionCommand().equals("添加")){
File file = new File("logdiary.txt");
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter(file,true),true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.log("AddRecord的添加按钮", pw, "将数据添加到数据库和JTable中");
if(Integer.parseInt(be_text.getText().toString())<Integer.parseInt(now_text.getText().toString())){
try {
throw new NumberOutException(Integer.parseInt(be_text.getText().toString()),Integer.parseInt(now_text.getText().toString()),
"应到人数小于实到人数了");
}
catch (NumberOutException e) {
jop.showMessageDialog(null, "输入错误,应到人数小于实到人数了");
}
}else {Connection con = DBConnection.getConnectionOracle();
try {
PreparedStatement ps = con.prepareStatement("insert into attendance values(?,?,?,?,?)");
ps.setString(1,date_text.getSelectedItem().toString() );
ps.setInt(2, Integer.parseInt(be_text.getText().toString()));
ps.setInt(3, Integer.parseInt(now_text.getText().toString()));
ps.setString(4,late_text.getText().toString() );
ps.setString(5,expalin_text.getText().toString() );
ps.executeUpdate();
Object[] obj = {date_text.getSelectedItem().toString(),be_text.getText().toString(),
now_text.getText().toString(),late_text.getText().toString(),
expalin_text.getText().toString()};
int rows = KaoQin.table_model.getRowCount();
for(int i = 0;i<rows;i++){
KaoQin.table_model.removeRow(i);
}
KaoQin.table_model.addRow(obj);
jop.showMessageDialog(null, "添加记录成功","记录提示", jop.INFORMATION_MESSAGE);
jf.dispose();
} catch (SQLException e) {
// TODO Auto-generated catch block
jop.showMessageDialog(null, "该记录已存在", "记录提示", jop.INFORMATION_MESSAGE);
}catch(NumberFormatException e){
jop.showMessageDialog(null, "你输出的不是数字,请输入数字");
}
}
}else if(arg0.getActionCommand().equals("取消")){
File file = new File("logdiary.txt");
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter(file,true),true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.log("AddRecord的取消按钮", pw, "取消将数据添加到数据库和JTable中的操作");
jf.dispose();
}
} catch (NumberFormatException e1) {
jop.showMessageDialog(null, "你输出的不是数字,请输入数字");
}
}
}
KaoqinAction kaoqin = new KaoqinAction();
add_button.addActionListener(kaoqin);
cancel_button.addActionListener(kaoqin);
jf.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -