📄 bm.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
class bm extends Frame implements ActionListener
{ private condb condep=null;
private ResultSet rs=null;
private String sql=null;
private Label dephao=new Label("部门号");
private Label chodepnm=new Label("部门号列表");
private TextField haotx=new TextField();
private Label depname=new Label("部门名称");
private TextField nametx=new TextField();
private Choice chodep=new Choice();
private Label depone=new Label("部门电话");
private TextField ponetx=new TextField();
private Button btnadd=new Button("增加");
private Button btndel=new Button("删除");
private Button btnmod=new Button("修改");
private Button btnexit=new Button("退出");
public bm()
{ super("部门信息");//调用父类方法
setSize(400,250);
setBackground(Color.LIGHT_GRAY);
setLocationRelativeTo(this.getParent());
setResizable(false);
setLayout(null);
dephao.setBounds(80,30,50,20);
haotx.setBounds(160,30,170,20);
depname.setBounds(80,70,50,20);
nametx.setBounds(160,70,170,20);
depone.setBounds(80,110,50,20);
ponetx.setBounds(160,120,170,20);
chodepnm.setBounds(80,160,50,20);
chodep.setBounds(160,160,170,20);
btnadd.setBounds(60,200,50,20);
btndel.setBounds(140,200,50,20);
btnmod.setBounds(220,200,50,20);
btnexit.setBounds(300,200,50,20);
add(dephao);
add(haotx);
add(depname);
add(nametx);
add(depone);
add(ponetx);
add(chodepnm);
add(chodep);
add(btnadd);
add(btndel);
add(btnmod);
add(btnexit);
initchodep();
setVisible(true);
btnadd.addActionListener(this);
btndel.addActionListener(this);
btnmod.addActionListener(this);
btnexit.addActionListener(this);
chodep.addItemListener(
new ItemListener()
{public void itemStateChanged(ItemEvent e)
{haotx.setText(chodep.getSelectedItem());}
} );
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{dispose();}
}
);
}
public void initchodep()
{ condep=new condb();
sql="select * from 部门";
try{rs=condep.getrs(condep.con,sql);
while(rs.next())
{chodep.addItem(rs.getString("部门号"));
}
condep.dbclose(condep.con,rs);
condep=null;
rs=null;
}
catch(Exception ee){}
}
public void actionPerformed(ActionEvent e)
{ if(e.getActionCommand().equals("退出"))
this.dispose();
if(e.getActionCommand().equals("增加"))
{ condep=new condb();
sql="select * from 部门 where 部门号='"+haotx.getText()+"'";
try{ rs=condep.getrs(condep.con,sql);
if(rs.next())
{ JOptionPane jop=new JOptionPane();
jop.showConfirmDialog(null,"该用户已经存在!","警告!",-1);
condep.dbclose(condep.con,rs);
condep=null;
rs=null;
}
else { sql="insert into 部门 values('"+haotx.getText()+"','"+
nametx.getText()+"','"+ponetx.getText()+"')";
condep=new condb();
try{ condep.moddb(condep.con,sql);
condep.dbclose(condep.con,null);
}
catch(Exception ue){}
chodep.addItem(haotx.getText());
haotx.setText("");
condep=null;
}
}catch(Exception be){}
}
if(e.getActionCommand().equals("修改"))
{ condep=new condb();
sql="update 部门 set 部门名称='"+nametx.getText()+"',电话='"+
ponetx.getText()+"'where 部门号='"+haotx.getText()+"'";
try{ condep.moddb(condep.con,sql);
condep.dbclose(condep.con,null);
}
catch(Exception ee){}
chodep.remove(chodep.getSelectedIndex());
chodep.add(haotx.getText());
haotx.setText("");
condep=null;
}
if(e.getActionCommand().equals("删除"))
{ condep=new condb();
sql="delete from 部门 where 部门号='"+haotx.getText()+"'";
try{ condep.moddb(condep.con,sql);
condep.dbclose(condep.con,null);
}
catch(Exception ee){}
chodep.remove(chodep.getSelectedIndex());
haotx.setText("");
condep=null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -