⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readertypemanage.java

📁 简单的书店管理系统,是课设期间教师指导完成的,希望会对大家有帮助!
💻 JAVA
字号:
package book;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
//读者类型
public class ReaderTypeManage extends Mb {
	private JLabel icon,number, name,number1;

	private JTextField tnumber, tname,tnumber1;

	private ButtonIcon add, del, xiugai, close, save, quxiao;

	private JScrollPane scrool;

	private ArrayList List = new ArrayList();

	private Table table;

	private PreparedStatement pstmt = null;

	private NowEditableModel dtm;

	ReaderTypeManage(String s, int i, int j) throws SQLException {
		super(s, i, j);
		con.setLayout(null);
		shezhi();
		add();
	}

	private void shezhi() throws SQLException {
		MainDAO dao = new MainDAO();
		dtm = (NowEditableModel) dao.getTableModel("select * from duzheleibie");
		table = new Table(dtm);
		scrool = new JScrollPane(table);
		scrool.setBounds(15, 60, 365, 200);
		table.setWidth();

		ListSelectionModel rowSM = table.getSelectionModel();
		rowSM.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if (e.getValueIsAdjusting())
					return;
				ListSelectionModel lsm = (ListSelectionModel) e.getSource();
				if (lsm.isSelectionEmpty()) {
					System.out.println("No rows are selected.");
				} else {
					int selectedRow = lsm.getMinSelectionIndex();
					List = table.getSelectRow(lsm.getMinSelectionIndex(), table
							.getColumnCount());
					if (!(List.size() == 0)) {
						setSelect();
					}
					System.out.println("Row " + selectedRow
							+ " is now selected.");
				}
			}

		});

		setLabel();
		setTextField();
		setButton();
	}

	private void setButton() {
		xiugai = new ButtonIcon("修改");
		xiugai.setBounds(15, 310, 70, 25);
		xiugai.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				try {
					if (xiuJudge()&&judge()) {
						MainDAO dao;
						dao = new MainDAO();
						String str = "update duzheleibie set 读者类别=?,借书天数=?,借书数量=? where 类别编号=?";
						pstmt = dao.getPreparedStatement(str);
						pstmt.setString(1, tnumber.getText());
						pstmt.setInt(2, Integer.parseInt(tname.getText()));
						pstmt.setInt(3, Integer.parseInt(tnumber1.getText()));
						pstmt.setInt(4, Integer.parseInt(List.get(0).toString()));
						pstmt.executeUpdate();
						pstmt.close();
						dao.close();
						table.Renovate("select * from duzheleibie");	
						JOptionPane.showMessageDialog(table, "修改成功!!!!!!!!");
						}
				} catch (SQLException e) {
					e.printStackTrace();
				} 
			}
		});

		add = new ButtonIcon("添加");
		add.setBounds(115, 310, 70, 25);
		add.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				tnumber.setText("");
				tname.setText("");
				tnumber1.setText("");
				xiugai.setEnabled(false);
				del.setVisible(false);
				save.setVisible(true);
				add.setEnabled(false);
				quxiao.setVisible(true);
				close.setVisible(false);
				tnumber.setEnabled(true);

			}

		});

		save = new ButtonIcon("保存");
		save.setVisible(false);
		save.setBounds(215, 310, 70, 25);
		save.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				try {
					MainDAO dao = new MainDAO();
					if (addJudge() && judge()) {
						String str = "insert into duzheleibie(读者类别,借书天数,借书数量) values(?,?,?)";
						pstmt = dao.getPreparedStatement(str);
						pstmt.setString(1, tnumber.getText());
						pstmt.setInt(2, Integer.parseInt(tname.getText()));
						pstmt.setInt(3, Integer.parseInt(tnumber1.getText()));
						pstmt.executeUpdate();
						pstmt.close();
						dao.close();
						table.Renovate("select * from duzheleibie");
						xiugai.setEnabled(true);
						add.setEnabled(true);
						del.setVisible(true);
						save.setVisible(false);
						quxiao.setVisible(false);
						close.setVisible(true);
						JOptionPane.showMessageDialog(table, "添加新类别成功!!!!!!!!");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}

			}
		});

		del = new ButtonIcon("删除");
		del.setBounds(215, 310, 70, 25);
		del.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				if (List.get(0).toString().equals("")) {
					JOptionPane.showMessageDialog(table, "没选中任何用户!!!!");
				} else {
					int n = JOptionPane.showConfirmDialog(table,
							"确认删除此条记录??删除后将不可以恢复!!", "退出",
							JOptionPane.YES_NO_OPTION);
					if (n == 0) {
						MainDAO dao;
						try {
							dao = new MainDAO();
							String str;
							str = "delete from duzheleibie where 类别编号="
									+ List.get(0).toString();
							dao.del(str);
							table.Renovate("select * from duzheleibie");
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
					}
				}
			}
		});

		close = new ButtonIcon("关闭");
		close.setBounds(315, 310, 70, 25);
		close.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				close();
			}

		});

		quxiao = new ButtonIcon("取消");
		quxiao.setBounds(315, 310, 70, 25);
		quxiao.setVisible(false);
		quxiao.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				xiugai.setEnabled(true);
				del.setVisible(true);
				save.setVisible(false);
				add.setEnabled(true);
				quxiao.setVisible(false);
				close.setVisible(true);
				table.getSelectionModel().setSelectionInterval(0, 0);
			}
		});

	}

	private void setTextField() {
		tnumber = new JTextField();
		tnumber.setBounds(60, 270, 80, 18);
		tname = new JTextField();
		tname.setBounds(210, 270, 50, 18);
		tnumber1=new JTextField();
		tnumber1.setBounds(330,270,50,18);
	}

	private void setLabel() {
		
		icon=new JLabel(new ImageIcon("./data/image/heard/duzheleixing.jpg"));
		icon.setBounds(0,5,400,50);
		
		number = new JLabel("类别名:");
		number.setBounds(15, 270, 80, 18);
		name = new JLabel("天数上限:");
		name.setBounds(150, 270, 80, 18);
		number1=new JLabel("数量上限:");
		number1.setBounds(265,270,80,18);
	}

	private void setSelect() {
		tnumber.setText(List.get(1).toString());
		tname.setText(List.get(2).toString());
		tnumber1.setText(List.get(3).toString());
	}

	private boolean judge() {
		boolean boo = true;
		
		if (tnumber.getText().equals("") || tname.getText().equals("")||tnumber1.getText().equals("")) {
			JOptionPane.showMessageDialog(table, "数据不能为空!!");
			boo = false;
		}
		return boo;
	}

	
	
	private boolean xiuJudge() throws SQLException {
		boolean boo = true;
		MainDAO dao = new MainDAO();
		char b[] = tname.getText().toCharArray();
		for (int i = 0; i < b.length; i++) {
			if (!(Character.isDigit(b[i]))) {
				boo = false;
			} 
		 }
		 
		
		
		char a[] = tnumber1.getText().toCharArray();
		for (int i = 0; i < a.length; i++) {
			if (!(Character.isDigit(a[i]))) {
				boo = false;
			} 
		 }
		
		 if(!boo){
			 JOptionPane.showMessageDialog(table, "天数上限和数量上限只能是数字!!");
		 }
		
		if(boo){
			if(!tnumber.getText().equals(List.get(1).toString())){
				if (dao.Judge("select 读者类别 from duzheleibie", tnumber
						.getText(), "读者类别")) {
					boo = false;
					JOptionPane.showMessageDialog(table, "此类别名已存在,请重新输入!!");
				}
			}
		}
		
	
		return boo;
	}
	
	
	
	
	private boolean addJudge() throws SQLException {
		boolean boo = true;
		MainDAO dao = new MainDAO();
		char b[] = tname.getText().toCharArray();
		for (int i = 0; i < b.length; i++) {
			if (!(Character.isDigit(b[i]))) {
				boo = false;
			} 
		 }
		
		
		char a[] = tnumber1.getText().toCharArray();
		for (int i = 0; i < a.length; i++) {
			if (!(Character.isDigit(a[i]))) {
				boo = false;
			} 
		 }
				
		if(!boo){
			 JOptionPane.showMessageDialog(table, "天数上限和数量上限只能是数字!!");
		 }
				
				if(boo){
					if (dao.Judge("select 读者类别 from duzheleibie", tnumber
							.getText(), "读者类别")) {
						boo = false;
						JOptionPane.showMessageDialog(table, "此类别名已存在,请重新输入!!");
					}
				}
				
		return boo;
	}

	private void add() {
		con.add(icon);
		
		con.add(scrool);
		con.add(number);
		con.add(tnumber);
		con.add(name);
		con.add(tname);
		con.add(tnumber1);
		con.add(number1);

		con.add(xiugai);
		con.add(add);
		con.add(save);
		con.add(del);
		con.add(close);
		con.add(quxiao);

		con.validate();
		table.getSelectionModel().setSelectionInterval(0, 0);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -