📄 student.java.bak
字号:
}
//////查询、删除学生记录窗体类///////
class stufrm1 implements ActionListener{ //throws IOException
String id,name,sex,birthday,phone,dept;
Connection conn;
ResultSet rs;
Statement st;
public JLabel NumL,nameL,sexL,birthdayL,phoneL,deptL;
public JTextField tf,Num,nameT,sexT,birthdayT,phoneT,deptT;
public JFrame stuf;
public JPanel p1;
public JButton search,closeT,delete;
public stufrm1(){
//查询、删除学生记录窗体布局
stuf= new JFrame ("查询、删除学生记录");stuf.setSize(360,360);stuf.setResizable(false);
id = new String(); name = new String(); sex = new String();
birthday = new String();phone = new String();dept=new String();
NumL = new JLabel("学号:"); nameL = new JLabel("姓名:");
sexL = new JLabel("性别:");birthdayL = new JLabel("生日:");
phoneL = new JLabel("电话:");deptL = new JLabel("系别:");
tf=new JTextField();Num = new JTextField();nameT = new JTextField();
sexT = new JTextField();birthdayT = new JTextField();phoneT=new JTextField();
deptT=new JTextField();p1=new JPanel(); search=new JButton("查找");
search.setLocation(90,10);search.setSize(60,20);delete=new JButton("删除");
delete.setLocation(20,10);delete.setSize(60,20);tf.setLocation(190,10);
tf.setSize(100,20);p1.add(search,"Center"); p1.add(delete,"Center");p1.add(tf,"Center");
closeT=new JButton("退出"); closeT.setLocation(146,240);
closeT.setSize(80,30);p1.add(closeT,"Center");
NumL.setLocation(35,40); NumL.setSize(40,20); p1.add(NumL,"Center");
Num.setLocation(90,40); Num.setSize(200,20);Num.setEnabled(false);
p1.add(Num,"Center"); nameL.setLocation(35,70); nameL.setSize(40,20);
p1.add(nameL,"Center");nameT.setLocation(90,70);nameT.setSize(200,20);
p1.add(nameT,"Center");sexL.setLocation(35,100);sexL.setSize(40,20);
p1.add(sexL,"Center");sexT.setLocation(90,100);sexT.setSize(200,20);
p1.add(sexT,"Center");birthdayL.setLocation(35,130);birthdayL.setSize(40,20);
p1.add(birthdayL,"Center");birthdayT.setLocation(90,130);birthdayT.setSize(200,20);
p1.add(birthdayT,"Center");phoneL.setLocation(35,160);phoneL.setSize(40,20);
p1.add(phoneL,"Center");phoneT.setLocation(90,160);phoneT.setSize(200,20);
p1.add(phoneT,"Center");deptL.setLocation(35,190); deptL.setSize(40,20);
p1.add(deptL,"Center");deptT.setLocation(90,190); deptT.setSize(200,20);
p1.add(deptT,"Center"); p1.setLayout(new BorderLayout());
Container c=stuf.getContentPane(); c.add(p1);
stuf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});;
stuf.setLocation(200,200); stuf.show();
//注册按钮的事件监听器
search.addActionListener(this);
closeT.addActionListener(this);
delete.addActionListener(this);
conDB();
}
//连接数据库
public void conDB(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null,"数据库加载失败!");
}
try{
conn = DriverManager.getConnection("jdbc:odbc:studb");
st = conn.createStatement();
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,"数据库连接失败!");
}
}
// 关闭数据库
public void closeDB(){
try{
st.close(); conn.close();
}
catch(SQLException e ){
JOptionPane.showMessageDialog(null,"数据库关闭失败!");
}
}
//以下是针对数据库的各种操作
public void actionPerformed(ActionEvent e){
if(e.getSource()==search){
try{
String id=tf.getText();
// 执行查询语句
String strSQL = "select stunum,stuname,sex,birthday,phone,dept"+" from student where stunum= '"+id+"' ";
ResultSet rs = st.executeQuery(strSQL);
int count = 0;
while(rs.next()){
id = rs.getString("stunum");name = rs.getString("stuname");
sex = rs.getString("sex");birthday = rs.getString("birthday");
phone = rs.getString("phone");dept=rs.getString("dept"); ++count;
}
if(count==0)
JOptionPane.showMessageDialog(null,"对不起,没有您要查找的学生!");
else{
//在文本框中显示表中相应字段
Num.setText(id); nameT.setText(name);sexT.setText(sex);
birthdayT.setText(birthday);phoneT.setText(phone);deptT.setText(dept);
}
}
catch(SQLException ex){
Num.setText(ex.getMessage()); nameT.setText(ex.getMessage());
sexT.setText(ex.getMessage());birthdayT.setText(ex.getMessage());
phoneT.setText(ex.getMessage());deptT.setText(ex.getMessage());
}
}
if(e.getSource()==delete){
String id = Num.getText();
//执行删除语句
String strSQL = "delete stunum,stuname,sex,birthday,phone,dept"+" from student where stunum= '"+id+"' ";
try{
st.executeUpdate(strSQL);
}
catch(Exception exx){
JOptionPane.showMessageDialog(null,"521工作室通知您:出错了!");
return;
}
JOptionPane.showMessageDialog(null,"删除成功!");
//自动清空文本框中的内容
tf.setText(""); Num.setText("");nameT.setText("");sexT.setText("");
birthdayT.setText("");phoneT.setText("");deptT.setText("");
}
if(e.getSource()==closeT){
closeDB(); stuf.dispose();
}
}
}
//////插入学生记录窗体类//////////
class stufrm2 implements ActionListener{ //throws IOException
String id,name,sex,birthday,phone,dept;
Connection conn;
ResultSet rs;
Statement st;
public JLabel NumL,nameL,sexL,birthdayL,phoneL,deptL;
public JTextField Num,nameT,sexT,birthdayT,phoneT,deptT;
public JFrame stuf;
public JPanel p1;
public JButton insertT,closeT;
public stufrm2(){
//插入学生记录窗体布局
stuf= new JFrame ("插入学生记录");stuf.setSize(360,360);
id = new String(); name = new String(); sex = new String();
birthday = new String();phone = new String();dept=new String();
NumL = new JLabel("学号:"); nameL = new JLabel("姓名:");
sexL = new JLabel("性别:");birthdayL = new JLabel("生日:");
phoneL = new JLabel("电话:");deptL = new JLabel("系别:");
Num = new JTextField();nameT = new JTextField();
sexT = new JTextField();birthdayT = new JTextField();phoneT=new JTextField();
deptT=new JTextField();p1=new JPanel();
insertT=new JButton("提交");insertT.setLocation(50,240);insertT.setSize(80,30);
p1.add(insertT,"Center"); closeT=new JButton("退出"); closeT.setLocation(216,240);
closeT.setSize(80,30);p1.add(closeT,"Center");
NumL.setLocation(35,40); NumL.setSize(40,20); p1.add(NumL,"Center");
Num.setLocation(90,40); Num.setSize(200,20);
p1.add(Num,"Center"); nameL.setLocation(35,70); nameL.setSize(40,20);
p1.add(nameL,"Center");nameT.setLocation(90,70);nameT.setSize(200,20);
p1.add(nameT,"Center");sexL.setLocation(35,100);sexL.setSize(40,20);
p1.add(sexL,"Center");sexT.setLocation(90,100);sexT.setSize(200,20);
p1.add(sexT,"Center");birthdayL.setLocation(35,130);birthdayL.setSize(40,20);
p1.add(birthdayL,"Center");birthdayT.setLocation(90,130);birthdayT.setSize(200,20);
p1.add(birthdayT,"Center");phoneL.setLocation(35,160);phoneL.setSize(40,20);
p1.add(phoneL,"Center");phoneT.setLocation(90,160);phoneT.setSize(200,20);
p1.add(phoneT,"Center");deptL.setLocation(35,190); deptL.setSize(40,20);
p1.add(deptL,"Center");deptT.setLocation(90,190); deptT.setSize(200,20);
p1.add(deptT,"Center"); p1.setLayout(new BorderLayout());
Container c=stuf.getContentPane(); c.add(p1);
stuf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});;
stuf.setLocation(200,200); stuf.show();
insertT.addActionListener(this); closeT.addActionListener(this);
conDB();
}
//连接数据库
public void conDB(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null,"数据库加载失败!");
}
try{
conn = DriverManager.getConnection("jdbc:odbc:studb");
st = conn.createStatement();
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,"数据库连接失败!");
}
}
//关闭数据库
public void closeDB(){
try{
st.close(); conn.close();
}
catch(SQLException e ){
JOptionPane.showMessageDialog(null,"数据库关闭失败!");
}
}
//以下是针对数据库的各种操作
public void actionPerformed(ActionEvent e){
if(e.getSource()==insertT)//执行插入记录操作
if((Num.getText().trim()).equals("") || (nameT.getText().trim()).equals("") || (sexT.getText().trim()).equals("") || (birthdayT.getText().trim()).equals("") ||(phoneT.getText().trim()).equals("") || (deptT.getText().trim()).equals(""))
JOptionPane.showMessageDialog(null,"请输入信息再点击提交添加!");
else{
id = Num.getText();name = nameT.getText();sex = sexT.getText();
birthday = birthdayT.getText();phone = phoneT.getText();dept = deptT.getText();
String strSQL = "insert into student ( stunum,stuname,sex,birthday,phone,dept)values( '"+ id + "','" + name + "','" + sex + "','" + birthday + "','" + phone + "','" + dept + "')";
try{st.executeUpdate(strSQL);
}
catch(Exception exx){
JOptionPane.showMessageDialog(null,"数据库中已经存在您要添加的学生的学号!");
return;
}
JOptionPane.showMessageDialog(null,"恭喜您,添加成功了!");
Num.setText("");nameT.setText("");sexT.setText("");
birthdayT.setText("");phoneT.setText("");deptT.setText("");
}
if(e.getSource()==closeT){
closeDB(); stuf.dispose();
}
}
}
//////////////////////查询学生成绩窗体类///////
class markfrm1 implements ActionListener{
String id,stuname,coname,type,grade,teaname,mark;
Connection conn;
ResultSet rs;
Statement st;
public JLabel NumL,stunameL,conameL,typeL,gradeL,teanameL,markL;
public JTextField tf,Num,sName,cName,Type1,Grade1,teanameT,Chengj;
public JFrame markf;
public JPanel p1;
public JButton search,closeT;
//构造方法
public markfrm1(){ //窗体布局
markf= new JFrame ("查询学生成绩记录");markf.setSize(360,400);markf.setResizable(false);
id = new String(); stuname = new String(); coname = new String();
type = new String();grade = new String();teaname=new String();mark=new String();
NumL = new JLabel("学 号:"); stunameL = new JLabel("姓 名:");
conameL = new JLabel("课 程 名:");typeL = new JLabel("课程类型:");
gradeL = new JLabel("年 级:");teanameL = new JLabel("任课教师:");markL=new JLabel("成 绩:");
tf=new JTextField();Num = new JTextField();sName = new JTextField();
cName = new JTextField();Type1 = new JTextField();Grade1=new JTextField();
teanameT=new JTextField(); Chengj=new JTextField();p1=new JPanel(); search=new JButton("查找");
search.setLocation(90,10);search.setSize(60,20);
tf.setLocation(190,10);
tf.setSize(100,20);p1.add(search,"Center"); p1.add(tf,"Center");
closeT=new JButton("退出"); closeT.setLocation(146,280);
closeT.setSize(80,30);p1.add(closeT,"Center");
NumL.setLocation(30,40); NumL.setSize(80,20); p1.add(NumL,"Center");
Num.setLocation(130,40); Num.setSize(180,20);Num.setEnabled(false);
p1.add(Num,"Center"); stunameL.setLocation(30,70); stunameL.setSize(80,20);
p1.add(stunameL,"Center");sName.setLocation(130,70);sName.setSize(180,20);
p1.add(sName,"Center");conameL.setLocation(30,100);conameL.setSize(80,20);
p1.add(conameL,"Center");cName.setLocation(130,100);cName.setSize(180,20);
p1.add(cName,"Center");typeL.setLocation(30,130);typeL.setSize(80,20);
p1.add(typeL,"Center");Type1.setLocation(130,130);Type1.setSize(180,20);
p1.add(Type1,"Center");gradeL.setLocation(30,160);gradeL.setSize(80,20);
p1.add(gradeL,"Center");Grade1.setLocation(130,160);Grade1.setSize(180,20);
p1.add(Grade1,"Center");teanameL.setLocation(30,190);teanameL.setSize(80,20);
p1.add(teanameL,"Center");teanameT.setLocation(130,190); teanameT.setSize(180,20);
p1.add(teanameT,"Center");markL.setLocation(30,220);markL.setSize(80,20);
p1.add(markL,"Center");Chengj.setLocation(130,220); Chengj.setSize(180,20);
p1.add(Chengj,"Center");
p1.setLayout(new BorderLayout());
Container c=markf.getContentPane(); c.add(p1);
//窗体正常关闭
markf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});;
markf.setLocation(200,200); markf.show();
//注册按钮的事件监听器
search.addActionListener(this); closeT.addActionListener(this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -