📄 dataentry.java
字号:
//作者:沈阳
//时间:2008年11月7日
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
/*---------------------------------------DataEntry-------------------------------------*/
//数据库操作不成功时,把操作改得更友好!
class DataEntry extends JFrame implements ActionListener{
int userPriority;
JPanel jpTop1 = new JPanel();
JPanel jpTop2 = new JPanel();
JPanel jpTop3 = new JPanel();
JPanel jpTop = new JPanel();
JPanel jpLeft1 = new JPanel();
JPanel jpLeft2 = new JPanel();
JPanel jpRight1 = new JPanel();
JPanel jpRight2 = new JPanel();
JPanel jpLeft = new JPanel();
JPanel jpRight = new JPanel();
JPanel jpCenter = new JPanel();
JButton jbuttonLeft1 = new JButton("新 增 学 生");
JButton jbuttonLeft2 = new JButton("新 增 住 址");
JButton jbuttonRight1 = new JButton("新 增 教 师 ");
JButton jbuttonRight2 = new JButton("返 回 上 级");
DataEntry(int priority){
userPriority = priority;
Container container = this.getContentPane();
container.setLayout(new BorderLayout());
jpCenter.setLayout(new GridLayout(1,2));
jpLeft.setLayout(new GridLayout(2,1));
jpRight.setLayout(new GridLayout(2,1));
jpTop.setLayout(new GridLayout(3,1));
jbuttonLeft1.setPreferredSize(new Dimension(145,45));
jbuttonLeft2.setPreferredSize(new Dimension(145,45));
jbuttonRight1.setPreferredSize(new Dimension(145,45));
jbuttonRight2.setPreferredSize(new Dimension(145,45));
jbuttonLeft1.setFont(new java.awt.Font("Dialog",1,18));
jbuttonLeft2.setFont(new java.awt.Font("Dialog",1,18));
jbuttonRight1.setFont(new java.awt.Font("Dialog",1,18));
jbuttonRight2.setFont(new java.awt.Font("Dialog",1,18));
jpTop.add(jpTop1);
jpTop.add(jpTop2);
jpTop.add(jpTop3);
jpLeft1.add(jbuttonLeft1);
jpLeft2.add(jbuttonLeft2);
jpRight1.add(jbuttonRight1);
jpRight2.add(jbuttonRight2);
jpLeft.add(jpLeft1);
jpLeft.add(jpLeft2);
jpRight.add(jpRight1);
jpRight.add(jpRight2);
jpCenter.add(jpLeft);
jpCenter.add(jpRight);
jbuttonLeft1.addActionListener(this);
jbuttonLeft2.addActionListener(this);
jbuttonRight1.addActionListener(this);
jbuttonRight2.addActionListener(this);
container.add("North",jpTop);
container.add("Center",jpCenter);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width - 450) / 2,(screen.height - 500) / 2);
setTitle("数据录入");
setResizable(false);
setSize(470,280);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jbuttonLeft1){
dispose();
new EntryStudent();
}
else if(e.getSource() == jbuttonLeft2){
dispose();
new EntryAddress();
}
else if(e.getSource() == jbuttonRight1){
dispose();
new EntryMaster();
}
else if(e.getSource() == jbuttonRight2){
dispose();
new MainWindow(userPriority);
}
}// end of actionPerformed
/*---------------------------------------EntryStudent--------------------------------------------*/
class EntryStudent extends JFrame implements ActionListener{
String title[] = {"学 号","姓 名","性 别","年 龄",
"专业方向","所在系名","所在学院"};
JTextField txtNumber = new JTextField(15);
JTextField txtName = new JTextField(15);
JTextField txtAge = new JTextField(15);
JTextField txtAcademy = new JTextField(15);
ButtonGroup group=new ButtonGroup();
JRadioButton rabSexM = new JRadioButton("男",true);
JRadioButton rabSexF = new JRadioButton("女",false);
JComboBox cobSpeciality = new JComboBox();
JComboBox cobDepartment = new JComboBox();
JButton save = new JButton("保 存");
JButton exit = new JButton("退 出");
EntryStudent(){
Container container = this.getContentPane();
container.setLayout(new BorderLayout());
cobSpeciality.addItem("计算机网络与信息系统");
cobSpeciality.addItem("实时软件工程");
cobSpeciality.addItem("嵌入式系统");
cobSpeciality.addItem("人工智能");
cobSpeciality.addItem("计算机视觉");
cobSpeciality.addItem("多媒体技术");
cobSpeciality.addItem("数据库信息处理");
cobSpeciality.addItem("计算机理论学");
cobSpeciality.setSelectedIndex(0);
cobDepartment.addItem("软件工程");
cobDepartment.addItem("计算机应用科学与技术");
cobDepartment.addItem("信息技术科学");
cobDepartment.setSelectedIndex(0);
group.add(rabSexM);
group.add(rabSexF);
JPanel p[] = new JPanel[7];
for(int i = 0;i < 7;i++){
p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT));
}
for(int i = 0;i < 7;i++){
p[i].add(new JLabel(title[i]));
}
p[0].add(txtNumber);
p[1].add(txtName);
p[2].add(rabSexM);
p[2].add(rabSexF);
p[3].add(txtAge);
p[4].add(cobSpeciality);
p[5].add(cobDepartment);
p[6].add(txtAcademy);
JPanel jpCenter = new JPanel();
jpCenter.setLayout(new GridLayout(7,1));
for(int i = 0;i < 7;i++){
jpCenter.add(p[i]);
}
JPanel jpTop = new JPanel();
JPanel jpBottom1 = new JPanel();
JPanel jpBottom2 = new JPanel();
JPanel jpBottom = new JPanel();
jpBottom1 = new JPanel(new FlowLayout(FlowLayout.CENTER,50,5));
jpBottom1.add(save);
jpBottom1.add(exit);
jpBottom.setLayout(new GridLayout(2,1));
jpBottom.add(jpBottom1);
jpBottom.add(jpBottom2);
txtAcademy.addActionListener(this);
save.addActionListener(this);
exit.addActionListener(this);
container.add("North",jpTop);
container.add("Center",jpCenter);
container.add("South",jpBottom);
setSize(360,400);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width - 370) / 2,(screen.height - 480) / 2);
setTitle("学生数据录入");
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == save || e.getSource() == txtAcademy){
Statement stm = null;
ResultSet rs = null;
String stuNumber;
String stuName;
String stuSex;
int stuAge;
String stuSpeciality;
String stuDepartment;
String stuAcademy;
ConnectServer.mainLink(); //建立和数据库的连接。
try{
stm = ConnectServer.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stm.executeQuery("select * from student");
while(rs.next()){ // for testing
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4)+" "+rs.getString(5)+" "+rs.getString(6));
}
stuNumber = txtNumber.getText();
stuName = txtName.getText();
boolean judge1 = false;
judge1 = rabSexM.isSelected();
if(judge1 == true){
stuSex = "男";
}
else{
stuSex = "女";
}
stuAge = Integer.parseInt(txtAge.getText());
stuSpeciality = (String)cobSpeciality.getSelectedItem();
stuDepartment = (String)cobDepartment.getSelectedItem();
stuAcademy = txtAcademy.getText();
System.out.println(" "+stuNumber+" "+stuName+" "+stuAge+" "+stuSpeciality+
" "+stuDepartment+" "+stuAcademy); // for testing
String sql;
sql = "insert into student(stu_number,stu_name,stu_sex,stu_age,speciality,department,academy)";
sql = sql+" values('"+stuNumber+"','"+stuName+"','"+stuSex+"','"+stuAge;
sql = sql+"','"+stuSpeciality+"','"+stuDepartment+"','"+stuAcademy+"')";
stm.executeUpdate(sql); //执行SQL语句
JOptionPane.showMessageDialog(null,"增添信息已成功!");
txtNumber.setText("");
txtName.setText("");
rabSexM.setSelected(true);
txtAge.setText("");
cobSpeciality.setSelectedIndex(0);
cobDepartment.setSelectedIndex(0);
txtAcademy.setText("");
txtNumber.requestFocus();
stm.close();
ConnectServer.con.close();
}
catch(NumberFormatException ee){
JOptionPane.showMessageDialog(null,"年龄必须为正整数!");
txtAge.setText(""); //to clear the text.
txtAge.requestFocus();
txtAge.setSelectionStart(0);
txtAge.setSelectionEnd(txtAge.getText().length());
}
catch(Exception ee){
JOptionPane.showMessageDialog(null,"数据库操作失败!");
System.exit(0);
}
}
else if(e.getSource() == exit){
dispose();
new DataEntry(userPriority);
}
}// end of actionPerformed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -