在做修改功能的时候选择数据列修改出现的页面就是以上错误的页面身份证号和手机号对不上,哪位大佬能够帮忙改一下。下个星期就要交这个
```java
主界面
package guanliyuan;
import denglu.User;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Admin extends JFrame {
private JPanel contentPane, panel,buttonPane;
private boolean btn_1 = false;
CardLayout cardLayout = new CardLayout();
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Admin frame = new Admin();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Admin() {
setResizable(false);
setTitle("管理员主界面");//设置窗口标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(20, 30, 1200, 800);
setLocationRelativeTo(null);
contentPane = new JPanel(); //容器面板
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setBackground(Color.pink);
panel = new JPanel(); //内容显示面板
panel.setBounds(200, 200, 1000, 560);
contentPane.add(panel);
panel.setLayout(cardLayout);
panel.setBackground(Color.black);
buttonPane = new JPanel(); //菜单显示面板
buttonPane.setLayout(new FlowLayout());
buttonPane.setPreferredSize(new Dimension(200,680));
buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 5));
buttonPane.setBounds(0, 120, 200, 730);
buttonPane.setBackground(Color.white);
contentPane.add(buttonPane);
addbtn(buttonPane);
setBak();
User s = new User();
panel.add(s, "s");
}
public void addbtn (JPanel buttonPane) {
buttonPane.removeAll();
Font f=new Font("楷体",Font.BOLD,18);
JButton b = new JButton("用户管理");
b.setFont(f);
Icon yonghu = new ImageIcon("C://Users//anyone//wqlidea//yp//tubiao/用户管理.png");
b.setIcon(yonghu);
this.add(b);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btn_1=false;
cardLayout.show(panel, "s");
addbtn(buttonPane);
}
});
b.setPreferredSize(new Dimension(190, 40));
buttonPane.add(b);
JButton c = new JButton("信息管理");
c.setFont(f);
Icon jiben = new ImageIcon("C://Users//anyone//wqlidea//yp//tubiao/信息管理1.png");
c.setIcon(jiben);
this.add(c);
c.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == c && e.getButton() == MouseEvent.BUTTON1) {
if (e.getClickCount() % 2 == 0) {
btn_1 = false;
addbtn(buttonPane);
} else {
btn_1 = true;
addbtn(buttonPane);
}
}
}
});
c.setPreferredSize(new Dimension(190, 40));
buttonPane.add(c);
if (btn_1) {
JButton c1 = new JButton("员工信息管理");
c1.setBackground(Color.white);
c1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//事件内容
//加员工信息面板
yuangong d = new yuangong();
panel.add(d, "d");
cardLayout.show(panel, "d");
}
});
c1.setPreferredSize(new Dimension(190, 40));
Icon yuangong = new ImageIcon("C://Users//anyone//wqlidea//yp//tubiao/员工信息.png");
c1.setIcon(yuangong);
buttonPane.add(c1);
}
buttonPane.updateUI(); //刷新控件
}
public void setBak () {
// ((JPanel) this.getContentPane()).setOpaque(false);
// ImageIcon img = new ImageIcon("src/image/1.jpg"); //添加图片
// JLabel background = new JLabel();
// this.getLayeredPane().add(background);
// background.setBounds(0, 0, 1000, 100);
//背景图片上显示内容
JLabel l=new JLabel("占位");
this.getLayeredPane().add(l);
l.setBounds(300,120,50,20);
ImageIcon img1 = new ImageIcon("src/image/2.jpg"); //添加图片
JLabel background1 = new JLabel(img1);
this.getLayeredPane().add(background1);
background1.setBounds(200, 120, 1000, 80);
}
}
添加的方法
package guanliyuan;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class yuanggong_add extends JFrame {
JLabel UID,UN,UP,UR,sex,age,exl;
JTextField uid,un,tel,esfz;
JComboBox ur,nianling,xb;
JButton add,exit;
ButtonGroup group;
int i=18;
private JPanel contentPane;
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
yuanggong_add frame = new yuanggong_add();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public yuanggong_add(){
init();
this.setTitle("添加员工信息");
this.setBounds(350,300,680,400);
this.setVisible(true);
this.setResizable(false);
this.setLayout(null);
this.setLocationRelativeTo(null);
}
void init() {
Font font = new Font("宋体", Font.BOLD, 15);
contentPane = new JPanel();
group = new ButtonGroup();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
UID = new JLabel("员工编号:");
UN = new JLabel("员工姓名:");
UP = new JLabel("电 话:");
UR = new JLabel("身份证号:");
sex = new JLabel("性 别:");
age = new JLabel("年 龄:");
exl = new JLabel("学 历:");
uid = new JTextField();
un = new JTextField();
tel = new JTextField();
esfz = new JTextField();
ur = new JComboBox<>();
nianling = new JComboBox<>();
xb=new JComboBox<>();
for( i=18;i<=60;++i){
nianling.addItem(i);
}
UID.setFont(font);
UN.setFont(font);
UP.setFont(font);
UR.setFont(font);
sex.setFont(font);
xb.setFont(font);
age.setFont(font);
exl.setFont(font);
nianling.setFont(font);
ur.addItem("小学");
ur.addItem("初中");
ur.addItem("高中");
ur.addItem("本科");
xb.addItem("男");
xb.addItem("女");
contentPane.add(UID);
contentPane.add(uid);
contentPane.add(UN);
contentPane.add(un);
contentPane.add(UP);
contentPane.add(tel);
contentPane.add(UR);
contentPane.add(ur);
contentPane.add(sex);
contentPane.add(age);
contentPane.add(exl);
contentPane.add(esfz);
contentPane.add(nianling);
contentPane.add(xb);
UID.setBounds(40, 20, 80, 30);
uid.setBounds(130, 20, 150, 30);
UN.setBounds(350, 20, 80, 30);
un.setBounds(430, 20, 150, 30);
UP.setBounds(40, 130, 80, 30);
tel.setBounds(130, 130, 150, 30);
UR.setBounds(40, 180, 80, 30);
ur.setBounds(430, 130, 80, 30);
sex.setBounds(40, 80, 80, 30);
age.setBounds(350, 80, 80, 30);
exl.setBounds(350, 130, 80, 30);
esfz.setBounds(130, 180, 150, 30);
nianling.setBounds(430,80,80,30);
xb.setBounds(130,80,80,30);
add = new JButton("添加");
add.setFont(font);
contentPane.add(add);
add.setBounds(40, 250, 80, 30);
exit = new JButton("取消");
exit.setFont(font);
contentPane.add(exit);
add.setBounds(300, 250, 80, 30);
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
setVisible(false);
}
});
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
String eid=uid.getText();
String ename=un.getText();
String sex=xb.getSelectedItem().toString();
String age=nianling.getSelectedItem().toString();
String t=tel.getText();
String xl=ur.getSelectedItem().toString();
String sfz=esfz.getText();
if(eid.length()==0) {
JOptionPane.showMessageDialog(null, "员工编号不能为空!");
return;
}
if(ename.length()==0){
JOptionPane.showMessageDialog(null, "员工姓名不能为空!");
return;
}
if(ename.length()==0){
JOptionPane.showMessageDialog(null, "用户名不能为空!");
return;
}
if(t.length()==0){
JOptionPane.showMessageDialog(null,"电话不能为空!");
return;
}
if(sfz.length()==0){
JOptionPane.showMessageDialog(null,"身份证不能为空!");
return;
}
if(!t.matches("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$")){
JOptionPane.showMessageDialog(null,"请输入正确的手机号!");
return;
}
if(!sfz.matches("(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
"(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)")){
JOptionPane.showMessageDialog(null,"请输入正确的身份证号!");
return;
}
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=yaoDB";
String userName="sa";
String userPwd="123456wql";
Connection dbConn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
PreparedStatement pst = null;
pst=dbConn.prepareStatement("insert into emp values(?,?,?,?,?,?,?)");
pst.setString(1,eid);
pst.setString(2,ename);
pst.setString(3,sex);
pst.setString(4,age);
pst.setString(5,t);
pst.setString(6,xl);
pst.setString(7,sfz);
pst.addBatch();
pst.executeBatch();
dbConn.close();
JOptionPane.showMessageDialog(null, "添加成功!");
dispose();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
});
}
}
```java
package guanliyuan;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class yuanggong_modify extends JFrame {
JLabel UID,UN,UP,UR,sex,age,xl;
JTextField uid,un,tl,sfz;
JComboBox ur,nianling,xb;
JButton add,exit;
ButtonGroup group;
int i=18;
private JPanel contentPane;
yuanxinxi yuan;
public yuanggong_modify(yuanxinxi yuan){
this.yuan=yuan;
init(yuan);
this.setTitle("修改员工信息");
this.setBounds(350,300,680,400);
this.setVisible(true);
this.setResizable(false);
this.setLayout(null);
this.setLocationRelativeTo(null);
}
void init(yuanxinxi yuan) {
Font font = new Font("宋体", Font.BOLD, 15);
contentPane = new JPanel();
group = new ButtonGroup();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
UID = new JLabel("员工编号:");
UN = new JLabel("员工姓名:");
UP = new JLabel("电 话:");
UR = new JLabel("身份证号:");
sex = new JLabel("性 别:");
age = new JLabel("年 龄:");
xl = new JLabel("学 历:");
uid = new JTextField(yuan.getEid());
un = new JTextField(yuan.getEname());
tl = new JTextField(yuan.getTel());
sfz = new JTextField(yuan.getEsfz());
ur = new JComboBox<>();
nianling = new JComboBox<>();
xb=new JComboBox<>();
for( i=18;i<=60;++i){
nianling.addItem(i);
}
UID.setFont(font);
UN.setFont(font);
UP.setFont(font);
UR.setFont(font);
sex.setFont(font);
xb.setFont(font);
age.setFont(font);
xl.setFont(font);
nianling.setFont(font);
ur.addItem("小学");
ur.addItem("初中");
ur.addItem("高中");
ur.addItem("本科");
xb.addItem("男");
xb.addItem("女");
contentPane.add(UID);
contentPane.add(uid);
contentPane.add(UN);
contentPane.add(un);
contentPane.add(UP);
contentPane.add(tl);
contentPane.add(UR);
contentPane.add(ur);
contentPane.add(sex);
contentPane.add(age);
contentPane.add(xl);
contentPane.add(sfz);
contentPane.add(nianling);
contentPane.add(xb);
UID.setBounds(40, 20, 80, 30);
uid.setBounds(130, 20, 150, 30);
UN.setBounds(350, 20, 80, 30);
un.setBounds(430, 20, 150, 30);
UP.setBounds(40, 130, 80, 30);
tl.setBounds(130, 130, 150, 30);
UR.setBounds(40, 180, 80, 30);
ur.setBounds(430, 130, 80, 30);
sex.setBounds(40, 80, 80, 30);
age.setBounds(350, 80, 80, 30);
xl.setBounds(350, 130, 80, 30);
sfz.setBounds(130, 180, 150, 30);
nianling.setBounds(430,80,80,30);
xb.setBounds(130,80,80,30);
add = new JButton("修改");
add.setFont(font);
contentPane.add(add);
add.setBounds(40, 250, 80, 30);
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
yuanxinxi deng1 = new yuanxinxi(un.getText(),nianling.getSelectedItem().toString(),tl.getText(),ur.getSelectedItem().toString(),uid.getText(),xb.getSelectedItem().toString(),sfz.getText());
System.out.println(deng1);
yuanggongdao mo = new yuanggongdao();
if(mo.modify(deng1.ename,deng1.age,deng1.tel,deng1.exl,deng1.eid, deng1.sex,deng1.esfz)){
JOptionPane.showMessageDialog(null,"修改成功!",""
,JOptionPane.YES_NO_OPTION);
}
}
});
}
}
package guanliyuan;
import denglu.basedao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
public class yuanggongdao extends basedao {
Statement simt = null;
ResultSet res = null;
//查询
public Vector<Vector<String>>selectALL(){
Vector<Vector<String>> vData=new Vector<Vector<String>>();
String sql="select*from emp";
try{
conn=getConn();
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while (rs.next()){
Vector<String> v=new Vector<String>();
v.add(rs.getString("eid"));
v.add(rs.getString("ename"));
v.add(rs.getString("sex"));
v.add(rs.getString("age"));
v.add(rs.getString("tel"));
v.add(rs.getString("exl"));
v.add(rs.getString("esfz"));
vData.add(v);
}
}catch (SQLException e){
e.printStackTrace();
}
return vData;
}
//带名字的查询
public Vector<Vector<String>>selectname(String ename){
Vector<Vector<String>> vData=new Vector<Vector<String>>();
String sql="select*from emp where ename like ? ";
try{
conn=getConn();
ps=conn.prepareStatement(sql);
ps.setObject(1,"%"+ename+"%");
rs=ps.executeQuery();
while (rs.next()){
Vector<String>v=new Vector<String>();
v.add(rs.getString("eid"));
v.add(rs.getString("ename"));
v.add(rs.getString("sex"));
v.add(rs.getString("age"));
v.add(rs.getString("tel"));
v.add(rs.getString("exl"));
v.add(rs.getString("esfz"));
vData.add(v);
}
}catch (SQLException e){
e.printStackTrace();
}
return vData;
}
//带账号查询
public Vector<Vector<String>>selectname1(String eid){
Vector<Vector<String>> vData=new Vector<Vector<String>>();
String sql;
if(eid.equals("")) {
sql = " select * from emp ";
}else{
sql = " select * from emp where eid= "+eid;
}
try{
conn=getConn();
ps=conn.prepareStatement(sql);
// ps.setObject(1,eid);
rs=ps.executeQuery();
while (rs.next()){
Vector<String>v=new Vector<String>();
v.add(rs.getString("eid"));
v.add(rs.getString("ename"));
v.add(rs.getString("sex"));
v.add(rs.getString("age"));
v.add(rs.getString("tel"));
v.add(rs.getString("exl"));
v.add(rs.getString("esfz"));
vData.add(v);
}
}catch (SQLException e){
e.printStackTrace();
}
return vData;
}
//删除
public int delete(int eid){
int result=0;
String sql="delete from emp where eid=?";
try{
conn=getConn();
ps=conn.prepareStatement(sql);
ps.setObject(1,eid);
result=ps.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}
close();
return result;
}
//修改
public boolean modify(String ename,String age, String tel,String exl,String eid,String sex,String esfz){
//int result=0;
String sql="update emp set ename=?,age=?,tel=?,exl=? where eid=? and sex=? and esfz=? ";
try{
conn=getConn();
ps=conn.prepareStatement(sql);
ps.setObject(1,ename);
ps.setObject(2,age);
ps.setObject(3,tel);
ps.setObject(4,exl);
ps.setObject(5,eid);
ps.setObject(6,sex);
ps.setObject(7,esfz);
ps.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}
close();
return true;
}
}
package guanliyuan;
import denglu.denglu;
import denglu.dengludao;
import denglu.user_add;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class yuangong extends JPanel {
yuanggongdao ed = new yuanggongdao();
DefaultTableModel model;
JTable table;
JTextField textField2;
JButton query, add, modify, delete, exit;
JComboBox JC=new JComboBox();
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
yuangong frame = new yuangong();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public yuangong() {
setLayout(null);
this.setBounds(350, 300, 660, 500);
this.setVisible(true);
JC.addItem("员工编号");
JC.addItem("员工姓名");
JC.setBounds(280, 20, 90, 20);
textField2 = new JTextField();
textField2.setBounds(380, 20, 90, 20);
add(JC);
add(textField2);
query = new JButton("查询");
query.setBounds(500, 20, 80, 20);
add = new JButton("添加");
modify = new JButton("修改");
delete = new JButton("删除");
exit = new JButton("退出");
add(query);
add(add);
add(modify);
add(delete);
add(exit);
Vector<Vector<String>> vData=ed.selectALL();
getModel(vData);
table = new JTable(model);
JScrollPane jsp = new JScrollPane(table);
jsp.setBounds(0, 50, 1000, 560);
add(jsp);
add.setBounds(20, 400, 60, 20);
modify.setBounds(120, 400, 60, 20);
delete.setBounds(220, 400, 60, 20);
exit.setBounds(550, 400, 60, 20);
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
new yuanggong_add();
}
});
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
System.exit(0);
}
});
query.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(JC.getSelectedItem().toString().equals("员工姓名")){
String ename = textField2.getText();
getModel(ed.selectname(ename));
}
if(JC.getSelectedItem().toString().equals("员工编号")){
String eid=textField2.getText();
getModel(ed.selectname1(eid));
}
}
});
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int num=table.getSelectedRow();
if(num<0){
JOptionPane.showMessageDialog(null,"请选择删除数据行!");
}else{
int r=JOptionPane.showConfirmDialog(null,"确定选择删除数据嘛?");
if(r==0){
int eid= Integer.parseInt(model.getValueAt(num, 0).toString());
if (ed.delete(eid)>0){
JOptionPane.showMessageDialog(null,"删除成功!");
model.removeRow(num);
}
}
}
}
});
modify.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int num=table.getSelectedRow();
if(num<0){
JOptionPane.showMessageDialog(null,"请选择修改数据行!");
}else{
int r=JOptionPane.showConfirmDialog(null,"确定选择此数据数据嘛?");
if(r==0){
String eid= model.getValueAt(num, 0).toString();
String ename = model.getValueAt(num, 1).toString();
String sex = model.getValueAt(num, 2).toString();
String tel = model.getValueAt(num, 3).toString();
String exl = model.getValueAt(num, 4).toString();
String esfz = model.getValueAt(num, 5).toString();
String age = model.getValueAt(num, 6).toString();
yuanxinxi deng = new yuanxinxi(ename,age,tel,exl,eid,sex,esfz);
yuanggong_modify um = new yuanggong_modify(deng);
System.out.println(deng);
}
}
}
});
}
private void getModel (Vector < Vector < String >> vData) {
Vector<String> vHead = new Vector<String>();
vHead.add("员工编号");
vHead.add("员工姓名");
vHead.add("性别");
vHead.add("年龄");
vHead.add("电话");
vHead.add("学历");
vHead.add("身份证号");
if (model == null) {
model = new DefaultTableModel(vData, vHead);
} else {
model.setDataVector(vData, vHead);
}
}
}