读者信息管理共包括两部分,读者信息添加和读者信息查询与修改,在读者信息添加里要正确输入读者的八项信息,并且读者编号唯一,不能重复。
数据库的读者如下:
添加读者过程如下:
数据库读者信息添加:
2、读者信息查询与修改:开始表格里将会列出所有的读者信息,可以根据不同的方式进行查找,有读者编号,姓名,类别,系部等,单击表格中的内容时下方的文本框将会自动填满,然后可以修改读者的信息,由于读者编号是唯一的,所以无法修改。其他的任何信息都可以修改,修改成功之后上方的表格将会刷新,显示修改后的结果。
读者信息添加源代码如下:AddReader.java
package pdsu.bbm.view;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import pdsu.bbm.dao.Dao;
import pdsu.bbm.dao.ReaderDao;
import pdsu.bbm.dao.ReaderTypeDao;
import pdsu.bbm.model.Reader;
import pdsu.bbm.model.ReaderType;
public class AddReader extends JFrame implements ActionListener{
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JLabel label_1;
private JLabel lbll;
private JLabel label_2;
private JLabel label_3;
private JTextField textField_2;
private JTextField textField_3;
private JLabel label_4;
private JRadioButton radioButton,radioButton_1;
private JTextField textField_4;
private JLabel label_5;
private JTextField textField_5;
JComboBox comboBox_1;
JPanel centerpPanel;
JButton button1,button2;
Image icon = new ImageIcon("image/icon.png").getImage();
public static void main(String[] args) {
new AddReader();
}
public AddReader() {
setTitle("添加读者信息");
setSize(555, 333);
setIconImage(icon);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
centerpPanel=new JPanel();
centerpPanel.setLayout(null);
JLabel label1 = new JLabel("编 号:");
label1.setFont(new Font("宋体", Font.PLAIN, 20));
label1.setBounds(50, 26, 116, 42);
centerpPanel.add(label1);
textField = new JTextField();
textField.setBounds(122, 35, 120, 30);
centerpPanel.add(textField);
textField.setColumns(10);
JLabel label = new JLabel("姓 名:");
label.setFont(new Font("宋体", Font.PLAIN, 20));
label.setBounds(292, 30, 116, 35);
centerpPanel.add(label);
textField_1 = new JTextField();
textField_1.setBounds(362, 35, 120, 30);
centerpPanel.add(textField_1);
textField_1.setColumns(10);
label_1 = new JLabel("类 别:");
label_1.setFont(new Font("宋体", Font.PLAIN, 20));
label_1.setBounds(50, 78, 154, 50);
centerpPanel.add(label_1);
lbll = new JLabel("性 别:");
lbll.setFont(new Font("宋体", Font.PLAIN, 20));
lbll.setBounds(292, 78, 70, 50);
centerpPanel.add(lbll);
label_2 = new JLabel("年 龄:");
label_2.setFont(new Font("宋体", Font.PLAIN, 20));
label_2.setBounds(50, 130, 154, 50);
centerpPanel.add(label_2);
label_3 = new JLabel("电 话:");
label_3.setFont(new Font("宋体", Font.PLAIN, 20));
label_3.setBounds(292, 130, 137, 50);
centerpPanel.add(label_3);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(122, 138, 120, 30);
centerpPanel.add(textField_2);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(362, 138, 120, 30);
centerpPanel.add(textField_3);
label_4 = new JLabel("所在系:");
label_4.setFont(new Font("宋体", Font.PLAIN, 20));
label_4.setBounds(50, 178, 131, 50);
centerpPanel.add(label_4);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(122, 190, 120, 30);
centerpPanel.add(textField_4);
label_5 = new JLabel("注册日期:");
label_5.setFont(new Font("宋体", Font.PLAIN, 20));
label_5.setBounds(271, 178, 137, 50);
centerpPanel.add(label_5);
textField_5 = new JTextField();
textField_5.setColumns(10);
textField_5.setBounds(362, 190, 120, 30);
centerpPanel.add(textField_5);
List<ReaderType> rs = ReaderTypeDao.selectReaderType();
Iterator<ReaderType> iterator = rs.iterator();
String[] AllTypeName = new String[rs.size()];
int i = 0;
while (iterator.hasNext()) {
String typename = iterator.next().getTypename();
AllTypeName[i] = typename;
i++;
}
comboBox_1 = new JComboBox<String>(AllTypeName);
comboBox_1.setFont(new Font("宋体", Font.PLAIN, 16));
comboBox_1.setBounds(122, 84, 120, 30);
comboBox_1.addActionListener(this);
centerpPanel.add(comboBox_1);
JPanel panel = new JPanel();
panel.setBounds(362, 85, 120, 30);
centerpPanel.add(panel);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
getContentPane().add(centerpPanel,BorderLayout.CENTER);
radioButton = new JRadioButton("男");
radioButton.setFont(new Font("宋体", Font.PLAIN, 16));
panel.add(radioButton);
radioButton_1 = new JRadioButton("女");
radioButton_1.setFont(new Font("宋体", Font.PLAIN, 16));
panel.add(radioButton_1);
ButtonGroup group=new ButtonGroup();
group.add(radioButton);
group.add(radioButton_1);
JPanel panel2=new JPanel();
panel2.setLayout(new FlowLayout());
button1=new JButton("添加");
button1.setFont(new Font("宋体", Font.PLAIN, 20));
button1.addActionListener(this);
button2=new JButton("关闭");
button2.setFont(new Font("宋体", Font.PLAIN, 20));
button2.addActionListener(this);
panel2.add(button1);
panel2.add(button2);
add(panel2,BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Reader reader=new Reader();
if(e.getSource()==button2){
dispose();
new MainWindow();
}
if(e.getSource()==button1){
String bianhao=textField.getText().trim();
String name =textField_1.getText().trim();
String typename=comboBox_1.getSelectedItem()+"";
String date=textField_5.getText().trim();
String sex;
if(radioButton.isSelected()){
sex="男";
}else sex="女";
String phone=textField_3.getText().trim();
String dept=textField_4.getText().trim();
String age=textField_2.getText();
Date regdate=null;
try {
regdate=Dao.getStringToDate(date);
} catch (Exception e1) {
e1.printStackTrace();
}
if(regdate==null) return;
int m=0;
reader.setReaderid(bianhao);
reader.setName(name);
reader.setTypename(typename);
List<ReaderType> list=ReaderTypeDao.selectReaderType(typename);
Iterator<ReaderType> iterator=list.iterator();
while (iterator.hasNext()) {
m=iterator.next().getId();
}
reader.setType(m);
reader.setSex(sex);
reader.setPhone(phone);
reader.setDept(dept);
reader.setRegDate(regdate);
if(bianhao.equals("")||name.equals("")||sex.equals("")||age.equals("")||phone.equals("")||dept.equals("")||date.equals("")){
JOptionPane.showMessageDialog(this,"请输入正确的信息");
return;
}
else if(ReaderDao.selectReaderById2(bianhao)!=null){
JOptionPane.showMessageDialog(this,"该编号已存在");
System.out.println(ReaderDao.insertReader(reader));
return;
}
else if(!age.equals("")){
try {
reader.setAge(Integer.parseInt(age));
} catch (NumberFormatException e1) {
e1.printStackTrace();
}
if(ReaderDao.insertReader(reader)!=0){
JOptionPane.showMessageDialog(this,"添加成功");
}
else {
JOptionPane.showMessageDialog(this,"添加失败");
}
}
}
}
}
读者信息查询与修改源代码如下:ReaderSelectAndUpdate.java
package pdsu.bbm.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import pdsu.bbm.dao.Dao;
import pdsu.bbm.dao.ReaderDao;
import pdsu.bbm.dao.ReaderTypeDao;
import pdsu.bbm.model.Reader;
import pdsu.bbm.model.ReaderType;
public class ReaderSelectAndUpdate extends JFrame implements ActionListener,MouseListener {
// 上部组件
private JPanel selectJP;
// 上部组建(北)
private JPanel select_conditionJP;
private JButton selectJB;
private JComboBox conditionJCB;
private JTextField select_conditionJTF;
// 上部组件(中)
private JTable jtable;
private String[] title = { "编号", "类型", "姓名", "年龄", "性别", "电话", "系部", "注册日期" };
private JPanel select_resultJP;
private JScrollPane jscrollPane;
// 中部组件
private JPanel updateJP, sexJP;
private JRadioButton JRB1, JRB2;
private ButtonGroup buttonGroup = new ButtonGroup();
private JLabel IDJL, typeJL, readerNameJL, sexJL, phoneJL, deptJL, ageJL,
regJL;
private JTextField IDJTF, readerNameJTF, phoneJTF, deptJTF, ageJTF, regJTF;
private JComboBox readertypeJCB;
// 底部组件
private JPanel buttonJP;
private JButton updateJB, closeJB;
Image icon = new ImageIcon("image/icon.png").getImage();
public ReaderSelectAndUpdate() {
setBounds(200, 200, 555, 500);
setLocationRelativeTo(null);
setTitle("读者信息查询与修改");
setIconImage(icon);
// 读者信息查询面板设计
selectJP = new JPanel();
selectJP.setLayout(new BorderLayout());
// 查询条件面板
// 查询条件下拉列表框
select_conditionJP = new JPanel();
conditionJCB = new JComboBox();
String[] array = { "读者编号", "姓名", "类型", "系部","所有"};
for (int i = 0; i < array.length; i++) {
conditionJCB.addItem(array[i]);
conditionJCB.setFont(new Font("宋体", Font.PLAIN, 16));
}
conditionJCB.addActionListener(this);
select_conditionJP.add(conditionJCB);
// 查询条件文本框
select_conditionJTF = new JTextField();
select_conditionJTF.setColumns(20);
select_conditionJP.add(select_conditionJTF);
// 查询条件按钮
selectJB = new JButton("查询");
selectJB.setFont(new Font("宋体", Font.PLAIN, 20));
selectJB.addActionListener(this);
select_conditionJP.add(selectJB);
selectJP.add(select_conditionJP, BorderLayout.NORTH);
// 查询结果面板
select_resultJP = new JPanel();
jscrollPane = new JScrollPane();
jscrollPane.setPreferredSize(new Dimension(400, 200));
// 查询读者信息并生成初始表格
// Object[][] data = getSelect(ReaderDao.selectReader());
List<Reader> list = ReaderDao.selectReader();
jtable = new JTable(getSelect(list), title);
jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jtable.addMouseListener(this);
jscrollPane.setViewportView(jtable);
select_resultJP.add(jscrollPane);
selectJP.add(select_resultJP, BorderLayout.CENTER);
// 读者信息修改面板设计
updateJP = new JPanel();
updateJP.setBorder(new EmptyBorder(5, 10, 5, 10));
GridLayout gridLayout = new GridLayout(4, 4);
gridLayout.setVgap(10);
gridLayout.setHgap(10);
updateJP.setLayout(gridLayout);
IDJL = new JLabel("编 号:");
IDJL.setFont(new Font("宋体", Font.PLAIN, 20));
IDJL.setHorizontalAlignment(SwingConstants.CENTER);
updateJP.add(IDJL);
IDJTF = new JTextField();
IDJTF.setEditable(false);
updateJP.add(IDJTF);
readerNameJL = new JLabel("姓 名:");
readerNameJL.setFont(new Font("宋体", Font.PLAIN, 20));
readerNameJL.setHorizontalAlignment(SwingConstants.CENTER);
updateJP.add(readerNameJL);
readerNameJTF = new JTextField();
updateJP.add(readerNameJTF);
typeJL = new JLabel("类 别:");
typeJL.setFont(new Font("宋体", Font.PLAIN, 20));
typeJL.setHorizontalAlignment(SwingConstants.CENTER);
updateJP.add(typeJL);
// 下拉列表
List<ReaderType> rs = ReaderTypeDao.selectReaderType();
Iterator<ReaderType> iterator = rs.iterator();
String[] AllTypeName = new String[rs.size()];
int i = 0;
while (iterator.hasNext()) {
String typename = iterator.next().getTypename();
AllTypeName[i] = typename;
i++;
}
readertypeJCB = new JComboBox(AllTypeName);
readertypeJCB.setFont(new Font("宋体", Font.PLAIN, 20));
// Iterator<ReaderType> it =
// ReaderTypeDao.selectReaderType().iterator();
// while(it.hasNext()){z
// ReaderType rt = it.next();
// readertypeJCB.addItem(rt.getName());
// }
updateJP.add(readertypeJCB);
sexJL = new JLabel("性 别:");
sexJL.setFont(new Font("宋体", Font.PLAIN, 20));
sexJL.setHorizontalAlignment(SwingConstants.CENTER);
updateJP.add(sexJL);
sexJP = new JPanel();
final FlowLayout flowLayout = new FlowLayout();
flowLayout.setHgap(0);
flowLayout.setVgap(0);
sexJP.setLayout(flowLayout);
JRB1 = new JRadioButton();
sexJP.add(JRB1);
JRB1.setSelected(true);
buttonGroup.add(JRB1);
JRB1.setText("男");
JRB1.setFont(new Font("宋体", Font.PLAIN, 20));
JRB2 = new JRadioButton();
sexJP.add(JRB2);
buttonGroup.add(JRB2);
JRB2.setText("女");
JRB2.setFont(new Font("宋体", Font.PLAIN, 20));
updateJP.add(sexJP);
ageJL = new JLabel("年 龄:");
ageJL.setFont(new Font("宋体", Font.PLAIN, 20));
ageJL.setHorizontalAlignment(SwingConstants.CENTER);
updateJP.add(ageJL);
ageJTF = new JTextField(50);
updateJP.add(ageJTF);
phoneJL = new JLabel("电 话:");
phoneJL.setFont(new Font("宋体", Font.PLAIN, 20));
phoneJL.setHorizontalAlignment(SwingConstants.CENTER);
updateJP.add(phoneJL);
phoneJTF = new JTextField();
updateJP.add(phoneJTF);
deptJL = new JLabel("所在部门:");
deptJL.setFont(new Font("宋体", Font.PLAIN, 20));
deptJL.setHorizontalAlignment(SwingConstants.CENTER);
updateJP.add(deptJL);
deptJTF = new JTextField();
updateJP.add(deptJTF);
regJL = new JLabel("注册日期:");
regJL.setFont(new Font("宋体", Font.PLAIN, 20));
regJL.setHorizontalAlignment(SwingConstants.CENTER);
updateJP.add(regJL);
regJTF = new JTextField();
updateJP.add(regJTF);
// 登录取消按钮面板设计
buttonJP = new JPanel();// 修改按钮面板
updateJB = new JButton("修改");
updateJB.addActionListener(this);
updateJB.setFont(new Font("宋体", Font.PLAIN, 20));
closeJB = new JButton("关闭");
closeJB.addActionListener(this);
closeJB.setFont(new Font("宋体", Font.PLAIN, 20));
buttonJP.add(updateJB);
buttonJP.add(closeJB);
this.add(selectJP, BorderLayout.NORTH);
this.add(updateJP, BorderLayout.CENTER);
this.add(buttonJP, BorderLayout.SOUTH);
this.setVisible(true);// 设置窗体显示,否则不显示。
setResizable(false);// 取消最大化
}
public static void main(String[] args) {
new ReaderSelectAndUpdate();
}
private Object[][] getSelect(List<Reader> list) {
Object[][] data = new Object[list.size()][title.length];
for (int i = 0; i < list.size(); i++) {
Reader reader = list.get(i);
data[i][0] = reader.getReaderid().trim();
data[i][1] = reader.getTypename();
data[i][2] = reader.getName().trim();
data[i][3] = reader.getAge();
data[i][4] = reader.getSex().trim();
data[i][5] = reader.getPhone().trim();
data[i][6] = reader.getDept().trim();
data[i][7] = reader.getRegDate();
}
return data;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == selectJB) {
String type = (String) conditionJCB.getSelectedItem();
String find = select_conditionJTF.getText();
if (type.equals("读者编号")) {
if (select_conditionJTF.getText().equals(""))
JOptionPane.showMessageDialog(this, "请输入查询条件");
List<Reader> list = ReaderDao.selectReaderById(find);
jtable = new JTable(getSelect(list), title);
jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jscrollPane.setViewportView(jtable);
jtable.addMouseListener(this);
}
else if (type.equals("姓名")) {
if (select_conditionJTF.getText().equals(""))
JOptionPane.showMessageDialog(this, "请输入查询条件");
List<Reader> list = ReaderDao.selectReaderByName(find);
jtable = new JTable(getSelect(list), title);
jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jscrollPane.setViewportView(jtable);
jtable.addMouseListener(this);
}
else if (type.equals("类型")) {
if (select_conditionJTF.getText().equals(""))
JOptionPane.showMessageDialog(this, "请输入查询条件");
List<Reader> list = ReaderDao.selectReaderByType(find);
jtable = new JTable(getSelect(list), title);
jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jscrollPane.setViewportView(jtable);
jtable.addMouseListener(this);
}
else if (type.equals("系部")) {
if (select_conditionJTF.getText().equals(""))
JOptionPane.showMessageDialog(this, "请输入查询条件");
List<Reader> list = ReaderDao.selectReaderByDept(find);
jtable = new JTable(getSelect(list), title);
jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jscrollPane.setViewportView(jtable);
jtable.addMouseListener(this);
}
else if (type.equals("所有")) {
List<Reader> list = ReaderDao.selectReader();
jtable = new JTable(getSelect(list), title);
jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jscrollPane.setViewportView(jtable);
jtable.addMouseListener(this);
}
}
if (e.getSource() == updateJB) {
String id = IDJTF.getText();
String name = readerNameJTF.getText();
String typename = (String) readertypeJCB.getSelectedItem();
String sex;
if (JRB1.isSelected()) {
sex = "男";
} else
sex = "女";
String ageStr = ageJTF.getText().trim();
int age=0;
try {
age = Integer.parseInt(ageStr);
} catch (NumberFormatException e1) {
e1.printStackTrace();
}
String phone = phoneJTF.getText();
String dept = deptJTF.getText();
String regDateStr = regJTF.getText();
Date regDate = null;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date dt=new Date();
try {
regDate=sdf.parse(regDateStr);
} catch (ParseException a) {
JOptionPane.showMessageDialog(this, "请输入正确的日期格式!格式如2016-01-01");
regJTF.setText("");
}
if (regDate == null)
return;
int m = 0;
// 封装对象
Reader reader = new Reader();
reader.setReaderid(id);
reader.setName(name);
reader.setTypename(typename);
reader.setAge(age);
reader.setRegDate(regDate);
List<ReaderType> list = ReaderTypeDao.selectReaderType(typename);
Iterator<ReaderType> iterator = list.iterator();
while (iterator.hasNext()) {
m = iterator.next().getId();
}
reader.setType(m);
reader.setSex(sex);
reader.setPhone(phone);
reader.setDept(dept);
int result = ReaderDao.updateReader(reader);
if (result!=0) {
JOptionPane.showMessageDialog(this, "修改成功!");
} else {
JOptionPane.showMessageDialog(this, "修改失败!");
}
List<Reader> list1 = ReaderDao.selectReader();
Object[][] data = getSelect(list1);
jtable = new JTable(data, title);
jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jscrollPane.setViewportView(jtable);
jtable.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
if (e.getSource() == jtable) {
int row = jtable.getSelectedRow();
String id = (String) jtable.getValueAt(row, 0);
String typename = jtable.getValueAt(row, 1) + "";
String name = (String) jtable.getValueAt(row, 2);
String age = jtable.getValueAt(row, 3) + "";
String sex = ((String) jtable.getValueAt(row, 4))
.trim();
String phone = (String) jtable.getValueAt(row, 5);
String dept = (String) jtable.getValueAt(row, 6);
Date regDate = (Date) jtable.getValueAt(row, 7);
String regdatestr = Dao.getDateToString(regDate);
IDJTF.setText(id);
readerNameJTF.setText(name);
phoneJTF.setText(phone);
ageJTF.setText(age);
deptJTF.setText(dept);
regJTF.setText(regdatestr);
readertypeJCB.setSelectedItem(typename);
if (sex.equals("男")) {
JRB1.setSelected(true);
} else
JRB2.setSelected(true);
}
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseClicked(MouseEvent arg0) {
}
});
}
if(e.getSource()==closeJB){
dispose();
new MainWindow();
}
}
public void mouseClicked(MouseEvent e) {
if (e.getSource() == jtable) {
int row = jtable.getSelectedRow();
String id = (String) jtable.getValueAt(row, 0);
String typename = jtable.getValueAt(row, 1) + "";
String name = (String) jtable.getValueAt(row, 2);
String age = jtable.getValueAt(row, 3) + "";
String sex = ((String) jtable.getValueAt(row, 4)).trim();
String phone = (String) jtable.getValueAt(row, 5);
String dept = (String) jtable.getValueAt(row, 6);
Date regDate = (Date) jtable.getValueAt(row, 7);
String regdatestr = Dao.getDateToString(regDate);
IDJTF.setText(id);
readerNameJTF.setText(name);
phoneJTF.setText(phone);
ageJTF.setText(age);
deptJTF.setText(dept);
regJTF.setText(regdatestr);
readertypeJCB.setSelectedItem(typename);
if (sex.equals("男")) {
JRB1.setSelected(true);
} else
JRB2.setSelected(true);
IDJTF.setEditable(false);
}
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
}