DBHelp类
package com.xunpoit.pro.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
importjava.sql.Statement;
importjava.sql.ResultSet;
importjavax.swing.JPanel;
publicclass DBHelp {
//四个属性
//驱动字符串
final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//连接字符串
final String URL="jdbc:sqlserver://localhost:1433;databasename=persource";
//用户名
final String USER="sa";
//密码
final String PASS="1309014752";
//主要加载数据库的驱动
public DBHelp(){
try {
//Class类的forName方法返回与带有给定字符串名的类或与接口相关的Class对象
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
System.out.println("加载数据驱动出错了:"+e.getMessage());
}
}
//得到数据库的连接
public Connection getCon(){
Connection con=null;//连接对象
try {
//利用驱动管理的类来简历到给定数据库URL的连接
con=DriverManager.getConnection(URL,USER,PASS);
} catch (SQLException e) {
System.out.println("得到连接出错了:"+e.getMessage());
}
return con;
}
}
PramaDAO类
package com.xunpoit.pro.db.dao;
import java.util.Vector;
publicinterface PramaDAO {
boolean addPrama(String Object ,String value);
boolean delPrama(String Object,String value);
Vector queryPrama(String Object);
}
PramaDAOImpl类
package com.xunpoit.pro.db.dao.impl;
importjava.nio.channels.ClosedByInterruptException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
importjavax.naming.spi.DirStateFactory.Result;
import com.xunpoit.pro.db.DBHelp;
import com.xunpoit.pro.db.dao.PramaDAO
publicclass PramaDAOImpl implements PramaDAO {
Connection con;
PreparedStatement ptmt;
ResultSet rs;
String sql;
@Override
publicboolean addPrama(String Object, String value) {
sql="insert into Canshu1Table values(?,?)";
try {
con=new DBHelp().getCon();
ptmt=con.prepareStatement(sql);
ptmt.setString(1,Object);
ptmt.setString(2,value);
int row=ptmt.executeUpdate();
if(row>0){
returntrue;
}
} catch (Exception e) {
System.out.println("加入系统参数操作出错了!" + e.getMessage());
} finally {
closeDb();
}
returnfalse;
}
@Override
publicboolean delPrama(String Object, String value) {
sql="delete from Canshu1Table where programe=? and value=?";
try {
con=new DBHelp().getCon();
ptmt=con.prepareStatement(sql);
ptmt.setString(1,Object);
ptmt.setString(2,value);
int row=ptmt.executeUpdate();
if(row>0){
returntrue;
}
} catch (Exception e) {
System.out.println("删除系统参数操作出错了!" + e.getMessage());
} finally {
closeDb();
}
returnfalse;
}
@Override
publicVector queryPrama(String Object) {
sql="select distinct [value] from Canshu1Table where programe=? ";
Vector v=newVector();
try {
con=new DBHelp().getCon();
ptmt=con.prepareStatement(sql);
ptmt.setString(1,Object);
rs=ptmt.executeQuery();
while(rs.next()){
v.add(rs.getString(1));
}
} catch (Exception e) {
System.out.println("查询操作出错了!" + e.getMessage());
} finally {
closeDb();
}
return v;
}
privatevoid closeDb(){
try{
if(rs!=null){
rs.close();
rs=null;
}
if(ptmt!=null){
ptmt.close();
rs=null;
}
if(con!=null){
con.close();
con=null;
}
}catch(SQLException e){
}
}
}
PramaDAOFactory类
package com.xunpoit.pro.db.dao.impl.factory;
import com.xunpoit.pro.db.dao.PramaDAO;
import com.xunpoit.pro.db.dao.impl.PramaDAOImpl;
publicclass PramaDAOFactory {
private PramaDAOFactory(){
}
publicstatic PramaDAO getIntance(){
returnnew PramaDAOImpl();
}
}
AddPersonFrame类
导入包省略
publicclassAddPersonFrameextends JFrame implements ActionListener, CaretListener {
//工具条
JToolBar toolBar;
//按钮
JButton newBut,bcBut,csBut,exitBut,addBut,delBut;
//选项卡
JTabbedPane tabPane;
//面板
JPanel cjPanel,jtPanel,grPanel,ryPanel;
//下拉框
JComboBox sexCBox,marrageCBox,pltcCBox,nationCBox,zgCBox,cjeduCBox,
zgeduCBox,zwCBox,lyCBox,workformCBox;
//标签
JLabel picLab,idLab,nameLab,jgLab,addressLab,codeLab,sexLab,
marrayLab,healthLab,nationLab,pltcLab,idcardLab,birthLab,
zgLab,dwLab,bmLab,phoneLab,telephoneLab,workLab,postLab,
cjeduLab,zgeduLab,byeduLab,gwLab,gbLab,dcLab,zwLab,zcLab,
lyLab,workformLab,agreeStartLab,agreeEndLab,recorderLab,remarkLab;
//文本框
JTextField idField,nameField,jgField,addressField,codeField,sexFiled,marrayFiled,healthField,
idcardFiled,birthField,dwField,bmField,phoneField,telephoneField,workField,postField,
byeduField,gwField,gbField,dcField,zcField,agreeStartField,agreeEndField,recorderField,remarkField;
//文件选择对话框
JFileChooser fileCh;
//
DefaultComboBoxModel model;
publicstatic JTextField info=new JTextField();
public AddPersonFrame(){
Container container=this.getContentPane();
//注册静态文本框事件
info.addCaretListener(this);
//--------------------------- 工具栏 --------------------------------
newBut=new JButton("新增职工",new ImageIcon("image/xinZeng.jpg"));
bcBut=new JButton("保存",new ImageIcon("image/Save24.gif"));
csBut=new JButton("参数设置",new ImageIcon("image/12.jpg"));
csBut.addActionListener(this);
exitBut=new JButton("退出",new ImageIcon("image/Exit24.gif"));
toolBar=new JToolBar();
toolBar.add(newBut);toolBar.add(bcBut);toolBar.add(csBut);
toolBar.add(exitBut);
//设置工具条不能移动
toolBar.setFloatable(false);
//--------------------------- 选项卡 ---------------------------------
//--------------------- 选项卡1 -----------------------
idLab=new JLabel("编号");
idField=new JTextField();
idLab.setBounds(40,10,27,20);
idField.setBounds(75,10,95,20);
nameLab=new JLabel("姓名");
nameField=new JTextField();
nameLab.setBounds(205,10,27,20);
nameField.setBounds(245,10,95,20);
jgLab=new JLabel("籍贯");
jgField=new JTextField();
jgLab.setBounds(370,10,27,20);
jgField.setBounds(406,10,95,20);
addressLab=new JLabel("住址");
addressField=new JTextField();
addressLab.setBounds(40,35,27,20);
addressField.setBounds(75,35,265,20);
codeLab=new JLabel("邮编");
codeField=new JTextField();
codeLab.setBounds(370,35,27,20);
codeField.setBounds(406,35,95,20);
sexLab=new JLabel("性别");
sexLab.setBounds(40,60,27,20);
sexCBox=newJComboBox();
sexCBox.addItem("男");sexCBox.addItem("女");
sexCBox.setBounds(75,60,45,20);
marrayLab=new JLabel("婚否");
marrayLab.setBounds(135,60,27,20);
marrageCBox=newJComboBox();
marrageCBox.addItem("是");marrageCBox.addItem("否");
marrageCBox.setBounds(168,60,45,20);
//
healthLab=new JLabel("健康状况");
healthLab.setBounds(230,60,65,20);
healthField=new JTextField();
healthField.setBounds(292,60,46,20);
nationLab=new JLabel("民族");
nationLab.setBounds(370,60,45,20);
model=new DefaultComboBoxModel(queryData("民族"));
nationCBox=new JComboBox(model);
nationCBox.setBounds(406,60,95,20);
pltcLab=new JLabel("政治面貌");
pltcLab.setBounds(15,87,55,20);
pltcCBox=new JComboBox(queryData("政治面貌"));
pltcCBox.setBounds(76,87,45,20);
idcardLab=new JLabel("身份证号");
idcardLab.setBounds(134,87,55,20);
idcardFiled=new JTextField();
idcardFiled.setBounds(191,87,146,20);
birthLab=new JLabel("出生日期");
birthLab.setBounds(348,87,55,20);
birthField=new JTextField();
birthField.setBounds(407,87,95,20);
zgLab=new JLabel("职工类型");
zgLab.setBounds(15,122,55,20);
//
zgCBox=new JComboBox(queryData("职工类型"));
zgCBox.setBounds(76,122,80,20);
dwLab=new JLabel("单位");
dwLab.setBounds(186,122,55,20);
dwField=new JTextField();
dwField.setBounds(226,122,110,20);
bmLab=new JLabel("部门");
bmLab.setBounds(370,122,55,20);
bmField=new JTextField();
bmField.setBounds(407,122,95,20);
phoneLab=new JLabel("联系电话");
phoneLab.setBounds(15,150,55,20);
phoneField=new JTextField();
phoneField.setBounds(76,150,80,20);
telephoneLab=new JLabel("移动电话");
telephoneLab.setBounds(165,150,53,20);
telephoneField=new JTextField();
telephoneField.setBounds(226,150,110,20);
workLab=new JLabel("参加工作");
workLab.setBounds(15,178,53,20);
workField=new JTextField();
workField.setBounds(76,178,80,20);
postLab=new JLabel("入本单位");
postLab.setBounds(165,178,53,20);
postField=new JTextField();
postField.setBounds(226,178,110,20);
cjeduLab=new JLabel("曾经学历");
cjeduLab.setBounds(15,207,53,20);
cjeduCBox=new JComboBox(queryData("学历"));
cjeduCBox.setBounds(76,207,80,20);
zgeduLab=new JLabel("最高学历");
zgeduLab.setBounds(165,207,53,20);
zgeduCBox=new JComboBox(queryData("学历"));
zgeduCBox.setBounds(226,207,110,20);
byeduLab=new JLabel("毕业院校");
byeduLab.setBounds(348,207,55,20);
byeduField=new JTextField();
byeduField.setBounds(407,207,95,20);
gwLab=new JLabel("岗位");
gwLab.setBounds(40,238,53,20);
gwField=new JTextField();
gwField.setBounds(76,236,80,20);
gbLab=new JLabel("岗别");
gbLab.setBounds(190,238,53,20);
gbField=new JTextField();
gbField.setBounds(226,238,110,20);
dcLab=new JLabel("档次");
dcLab.setBounds(373,238,55,20);
dcField=new JTextField();
dcField.setBounds(407,238,95,20);
zwLab=new JLabel("职务");
zwLab.setBounds(40,266,53,20);
zwCBox=new JComboBox(queryData("职务"));
zwCBox.setBounds(76,266,80,20);
zcLab=new JLabel("职称");
zcLab.setBounds(190,266,53,20);
zcField=new JTextField();
zcField.setBounds(226,266,110,20);
lyLab=new JLabel("来源");
lyLab.setBounds(373,266,55,20);
lyCBox=new JComboBox(queryData("来源"));
lyCBox.setBounds(407,266,95,20);
workformLab=new JLabel("用工形式");
workformLab.setBounds(14,297,53,20);
workformCBox=new JComboBox(queryData("用工形式"));
workformCBox.setBounds(76,297,80,20);
agreeStartLab=new JLabel("合同日期");
agreeStartLab.setBounds(165,297,53,20);
agreeStartField=new JTextField();
agreeStartField.setBounds(226,297,110,20);
agreeEndLab=new JLabel("合同结束");
agreeEndLab.setBounds(348,297,55,20);
agreeEndField=new JTextField();
agreeEndField.setBounds(407,297,95,20);
recorderLab=new JLabel("建档人");
recorderLab.setBounds(25,330,53,20);
recorderField=new JTextField();
recorderField.setBounds(76,330,80,20);
remarkLab=new JLabel("备注");
remarkLab.setBounds(190,330,53,20);
remarkField=new JTextField();
remarkField.setBounds(226,330,275,20);
//加入标签
picLab=new JLabel(new ImageIcon("image/11.png"));
picLab.setBounds(540,15,149,193);
Border border=BorderFactory.createLoweredBevelBorder();
picLab.setBorder(border);
addBut=new JButton("添加");
addBut.setBounds(542,230,140,30);
addBut.addActionListener(this);
delBut=new JButton("删除");
delBut.setBounds(542,270,140,30);
delBut.addActionListener(this);
cjPanel=new JPanel();
/*可以使用该语句来实现点击面板的一个位置则可获得点击位置的坐标
cjPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("X:"+e.getX()+"\t"+"Y:"+e.getY());
}
});
*/
cjPanel.add(idLab);cjPanel.add(idField);cjPanel.add(nameLab);cjPanel.add(nameField);
cjPanel.add(jgLab);cjPanel.add(jgField);cjPanel.add(addressLab);cjPanel.add(addressField);
cjPanel.add(codeLab);cjPanel.add(codeField);cjPanel.add(picLab);
cjPanel.add(sexLab);cjPanel.add(sexCBox);cjPanel.add(marrayLab);cjPanel.add(marrageCBox);
cjPanel.add(healthLab);cjPanel.add(healthField);cjPanel.add(nationCBox);cjPanel.add(nationLab);
cjPanel.add(pltcLab);cjPanel.add(pltcCBox);cjPanel.add(idcardLab);cjPanel.add(idcardFiled);
cjPanel.add(birthLab);cjPanel.add(birthField);cjPanel.add(zgLab);cjPanel.add(zgCBox);
cjPanel.add(dwLab);cjPanel.add(dwField);cjPanel.add(bmLab);cjPanel.add(bmField);
cjPanel.add(phoneLab);cjPanel.add(phoneField);cjPanel.add(telephoneLab);cjPanel.add(telephoneField);
cjPanel.add(workLab);cjPanel.add(workField);cjPanel.add(postLab);cjPanel.add(postField);
cjPanel.add(cjeduLab);cjPanel.add(cjeduCBox); cjPanel.add(zgeduLab);cjPanel.add(zgeduCBox);
cjPanel.add(byeduLab);cjPanel.add(byeduField);cjPanel.add(gwLab);cjPanel.add(gwField);
cjPanel.add(gbLab);cjPanel.add(gbField);cjPanel.add(dcLab);cjPanel.add(dcField);
cjPanel.add(zwLab);cjPanel.add(zwCBox);cjPanel.add(zcLab);cjPanel.add(zcField);
cjPanel.add(lyLab);cjPanel.add(lyCBox);cjPanel.add(workformLab);cjPanel.add(workformCBox);
cjPanel.add(agreeStartLab);cjPanel.add(agreeStartField);cjPanel.add(agreeEndLab);cjPanel.add(agreeEndField);
cjPanel.add(recorderLab);cjPanel.add(recorderField);cjPanel.add(remarkLab);cjPanel.add(remarkField);
cjPanel.add(addBut);cjPanel.add(delBut);
cjPanel.setBackground(Color.white);
cjPanel.setLayout(null);
//---------------------------- 选项卡2 -----------------------
jtPanel=new JPanel();
grPanel=new JPanel();
ryPanel=new JPanel();
tabPane=new JTabbedPane();
tabPane.addTab("基本信息",null,cjPanel);
tabPane.addTab("家庭成员",null,jtPanel);
tabPane.addTab("个人简历",null,grPanel);
tabPane.addTab("所获荣誉",null,ryPanel);
//--------------------------------
container.add(toolBar,new BorderLayout().NORTH);
container.add(tabPane,new BorderLayout().CENTER);
this.setSize(760,470);
this.setTitle("人事管理系统--新增人员");
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
}
publicvoid actionPerformed(ActionEvent e) {
if(e.getSource()==addBut){
fileCh=new JFileChooser();
int ch=fileCh.showOpenDialog(this);
String path="";
if(ch==JFileChooser.APPROVE_OPTION){//如果是选择了打开按钮则执行下面语句
path=fileCh.getSelectedFile().toString();
ImageIcon icon=new ImageIcon(path);
//如果图片比标签的大小要小或要大使用该语句是不会将图片放大或缩小,使用该语句可以将图片等比例进行缩放
icon.setImage(icon.getImage().getScaledInstance(149, 193,1));
picLab.setIcon(icon);
}
}elseif(e.getSource()==delBut){
picLab.setIcon(new ImageIcon(""));
}elseif(e.getSource()==csBut){
new PrameFrame();
}
}
publicVector queryData(String object){
return PramaDAOFactory.getIntance().queryPrama(object);
}
@Override
publicvoid caretUpdate(CaretEvent arg0) {
String val=info.getText().trim();
if(val.length()>=2){
model=new DefaultComboBoxModel(queryData(val));
nationCBox.setModel(model);
}
}
}
PrameFrame类
导入包省略
publicclassPrameFrameextends JFrame implements ActionListener {
JLabel prameLab,xmLab,mcLab;
Button mzbut,zzbut,zgbut,xlbut,gwbut,gbbut,dcbut,zwbut,zcbut,lybut,ygbut;
JList list;
JTextField xmtxt,mctxt;
JButton addbut,delbut;
JPanel p1,p2,p3,p4;
Vector v;
boolean flag;
public PrameFrame(){
Container container=this.getContentPane();
prameLab=new JLabel("系统参数设置");
Font font=new Font("宋体",Font.BOLD,17);
prameLab.setFont(font);
p1=new JPanel();
p1.setBounds(0,0,600,30);
p1.setBackground(new Color(100,100,200));
p1.add(prameLab);
mzbut=new Button("民族");
mzbut.addActionListener(this);
zzbut=new Button("政治面貌");
zzbut.addActionListener(this);
zgbut=new Button("职工类型");
zgbut.addActionListener(this);
xlbut=new Button("学历");
xlbut.addActionListener(this);
gwbut=new Button("岗位");
gwbut.addActionListener(this);
gbbut=new Button("岗别");
gbbut.addActionListener(this);
dcbut=new Button("档次");
dcbut.addActionListener(this);
zwbut=new Button("职务");
zwbut.addActionListener(this);
zcbut=new Button("职称");
zcbut.addActionListener(this);
lybut=new Button("来源");
lybut.addActionListener(this);
ygbut=new Button("用工形式");
ygbut.addActionListener(this);
p2=new JPanel();
p2.setBounds(-10,30,600,35);
p2.add(mzbut);p2.add(zzbut);p2.add(zgbut);p2.add(xlbut);
p2.add(gwbut);p2.add(gbbut);p2.add(dcbut);p2.add(zwbut);
p2.add(zcbut);p2.add(lybut);p2.add(ygbut);
//p2.setBackground(Color.RED);
v=PramaDAOFactory.getIntance().queryPrama("民族");
list=new JList(v);
list.setBounds(20,10,550,220);
list.addMouseListener(new MouseAdapter() {
@Override
publicvoid mouseClicked(MouseEvent e) {
mctxt.setText(list.getSelectedValue().toString());
}
});
p3=new JPanel();
p3.setBounds(0,65,660,240);
//p3.setBackground(Color.GREEN);
p3.setLayout(null);
p3.add(list);
xmLab=new JLabel("项目");
xmLab.setBounds(30,45,30,40);
xmLab.setEnabled(false);
xmtxt=new JTextField("民族");
xmtxt.setEnabled(false);
xmtxt.setBounds(70,55,110,20);
mcLab=new JLabel("名称");
mcLab.setBounds(220,45,30,40);
mctxt=new JTextField();
mctxt.setBounds(260,55,110,20);
addbut=new JButton("加入");
addbut.addActionListener(this);
addbut.setBounds(410,55,60,20);
delbut=new JButton("删除");
delbut.setBounds(490,55,60,20);
delbut.addActionListener(this);
p4=new JPanel();
p4.setBounds(0,265,600,100);
//p4.setBackground(Color.ORANGE);
p4.setLayout(null);
p4.add(xmLab);p4.add(xmtxt);
p4.add(mcLab);p4.add(mctxt);
p4.add(addbut);p4.add(delbut);
container.add(p1);
container.add(p2);
container.add(p3);
container.add(p4);
this.setSize(600,400);
this.setLocationRelativeTo(null);
container.setLayout(null);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
publicstaticvoid main(String[] args){
new PrameFrame();
}
@Override
publicvoid actionPerformed(ActionEvent e) {
String object=xmtxt.getText().trim();
String value=mctxt.getText().trim();
//使用ActionEvent类的getActionCommand方法可以获得发生事件(按钮的文本)
if(e.getSource()!=addbut&&e.getSource()!=delbut){
xmtxt.setText(e.getActionCommand());
v=getData(e.getActionCommand());
//根据一个 Vector 构造只读 ListModel,然后对此模型调用 setModel。
list.setListData(v);
}elseif(e.getSource()==addbut&&value.length()>0){
/*
for(int i=0;i<v.size();i++){
if(value.equals(v.get(i))){
flag=true;
return;
}else
flag=false;
}
if(flag==false){}
*/
Vector v=PramaDAOFactory.getIntance().queryPrama(object);
//使用集合的contains方法来看集合中是否有value这个元素
if(!v.contains(value)){
if(PramaDAOFactory.getIntance().addPrama(object,value)){
v=getData(object);
list.setListData(v);
AddPersonFrame.info.setText(object);
}
}else
JOptionPane.showMessageDialog(this, "数据库中已经有改选项了,不能重新加入");
}elseif(e.getSource()==delbut){
if(PramaDAOFactory.getIntance().delPrama(object,value)){
v=getData(object);
list.setListData(v);
AddPersonFrame.info.setText(object);
}
}
}
publicVector getData(String object){
return PramaDAOFactory.getIntance().queryPrama(object);
}
}