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

📄 addorchangebookframe.java

📁 图书管理系统:用户管理
💻 JAVA
字号:
//添加和修改图书窗口
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.URL;
import java.sql.*;
public class AddOrChangeBookFrame extends JFrame implements ActionListener{
	static final long serialVersionUID=80;
	JTextField booksn,bookname,publish,author,price,number,type;
	JLabel bookIDlabel,bookNamelabel,bookPublishlabel,authorlabel,pricelabel,typelabel,numberlabel;
	JButton addbutton,cancelbutton,clearbutton;
	Container container;
	Connection con;
	PreparedStatement sql;
	Statement statesql;
	Dimension screenSize;
	Toolkit tool;
	Image myimage;
	boolean select;
	JTable table;
	int id;
	Object result[][];
	Object temp[][];
	AddOrChangeBookFrame(String title,Connection con,boolean select,Object[][] result,JTable table){
		super(title);
		this.con=con;
		String queue="INSERT INTO Book_Info VALUES (?,?,?,?,?,?,?,?,?)";
		try{
			sql=con.prepareStatement(queue);
		}
        catch(SQLException e3){
        	JOptionPane.showMessageDialog(this,"计算机已与服务器断开");
        }
		this.result=result;
		this.select=select;
	    this.table=table;
		tool=getToolkit();
		screenSize=tool.getScreenSize();
		URL url = getClass().getResource("/images/book.GIF");
		if(url!=null){
			myimage=tool.getImage(url);
			setIconImage(myimage);
		}
		setResizable(false);
		setSize(290,240);
		booksn=new JTextField(20);
		bookname=new JTextField(20);
		publish=new JTextField(20);
		author=new JTextField(8);
		price=new JTextField(8);
		price.setText("单位(元)");
		number=new JTextField(8);
		number.setText("单位(个)");
		type=new JTextField(8);
		if(!select){
			addbutton=new JButton("添加");
		}
		else{
			id=table.getSelectedRow();
			addbutton=new JButton("修改");
			temp=new Object[result.length][result[0].length];
			temp=(Object[][])result.clone();
			booksn.setText("不填即是不修改");
			bookname.setText("填null可删除此图书的这项信息");
		}
		cancelbutton=new JButton("取消");
		clearbutton=new JButton("清除");
		addbutton.addActionListener(this);
		cancelbutton.addActionListener(this);
		clearbutton.addActionListener(this);
		bookIDlabel=new JLabel("书号");
		bookNamelabel=new JLabel("书名");
		bookPublishlabel=new JLabel("出版");
		authorlabel=new JLabel("作者");
		pricelabel=new JLabel("价格");
		typelabel=new JLabel("类型");
		numberlabel=new JLabel("数量");
		container=getContentPane();
		container.setLayout(null);
		container.add(bookIDlabel);
		bookIDlabel.setBounds(10,10,30,30);
		container.add(booksn);
		booksn.setBounds(40,14,240,22);
		container.add(bookNamelabel);
		bookNamelabel.setBounds(10,40,30,30);
		container.add(bookname);
		bookname.setBounds(40,44,240,22);
		container.add(bookPublishlabel);
		bookPublishlabel.setBounds(10,70,30,30);
		container.add(publish);
		publish.setBounds(40,74,240,22);
		container.add(authorlabel);
		authorlabel.setBounds(10,100,30,30);
		container.add(author);
		author.setBounds(40,104,100,22);
		container.add(typelabel);
		typelabel.setBounds(145,100,30,30);
		container.add(type);
		type.setBounds(175,104,105,22);
		container.add(pricelabel);
		pricelabel.setBounds(10,130,30,30);
		container.add(price);
		price.setBounds(40,134,100,22);
		container.add(numberlabel);
		numberlabel.setBounds(145,130,30,30);
		container.add(number);
		number.setBounds(175,134,105,22);
		container.add(addbutton);
		addbutton.setBounds(25,170,60,30);
		container.add(clearbutton);
		clearbutton.setBounds(115,170,60,30);
		container.add(cancelbutton);
		cancelbutton.setBounds(205,170,60,30);
		container.setBackground(Color.ORANGE);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e1){
				if(sql!=null){
					try{
						sql.close();
					}
					catch(SQLException e3){
						System.out.println("计算机已与服务器断开");
					}
				}
				if(statesql!=null){
					try{
						statesql.close();
					}
					catch(SQLException e2){
						System.out.println("计算机已与服务器断开");
					}
				}
				dispose();
			}
		});
		setLocation((screenSize.width-getSize().width)/2,(screenSize.height-getSize().height)/2);
		setVisible(true);
		validate();
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==addbutton){
			String sn=booksn.getText().trim();
			String name=bookname.getText().trim();
			String publi=publish.getText().trim();
			String auth=author.getText().trim();
			String booktype=type.getText().trim();
			if(!select){
				double money=0;
				int booknumber=0;
				String bookprice=price.getText().trim();
				if(bookprice.length()!=0){
					try{
						money=Double.parseDouble(bookprice);
					}
					catch(IllegalArgumentException e1){
						JOptionPane.showMessageDialog(this,"价格框要输入数字","警告对话框",JOptionPane.WARNING_MESSAGE);
						return;
					}
				}
				try{
					booknumber=Integer.parseInt(number.getText());
				}
				catch(IllegalArgumentException e2){
					JOptionPane.showMessageDialog(this,"数量框要输入整数","警告对话框",JOptionPane.WARNING_MESSAGE);
					return;
				}
	            if(sn.length()>10||name.length()>10||publi.length()>20||auth.length()>10||booktype.length()>10){
	            	JOptionPane.showMessageDialog(this,"书号,书名,作者,类型不能超过10,出版社不能超过20","警告对话框",JOptionPane.WARNING_MESSAGE);
	            	return;
	            }
				if(sn.length()!=0&name.length()!=0&booktype.length()!=0){
					try{
						sql.setString(1,sn);
						sql.setString(2,name);
						sql.setString(3,publi);
                        sql.setString(4,auth);
						sql.setString(5,booktype);
						sql.setDouble(6,money);
						sql.setInt(7,booknumber);
						sql.setInt(8,0);
						sql.setInt(9,booknumber);
						int n=sql.executeUpdate();
						if(n==1){
							JOptionPane.showMessageDialog(this,"添加成功");
						}
						else{
							JOptionPane.showMessageDialog(this,"添加失败");
						}
						sql.clearParameters();
					}
					catch(SQLException e2){
						JOptionPane.showMessageDialog(this,"已经存在此书","警告对话框",JOptionPane.WARNING_MESSAGE);
					}
				}
				else{
					JOptionPane.showMessageDialog(this,"书号,书名,类型不能为空","警告对话框",JOptionPane.WARNING_MESSAGE);
				}
			}
			else{
				if(sn.length()>10||name.length()>10||publi.length()>20||auth.length()>10||booktype.length()>10){
	            	JOptionPane.showMessageDialog(this,"书号,书名,作者,类型不能超过10,出版社不能超过20","警告对话框",JOptionPane.WARNING_MESSAGE);
	            	return;
	            }
				try{
					statesql=con.createStatement();
				}
				catch(SQLException e2){
					JOptionPane.showMessageDialog(this,"计算机已与服务器断开");
					return;
				}
				String bookprice=price.getText().trim();
				String booknumber=number.getText().trim();
				int n=0;
				double d;
				String changebook="UPDATE Book_Info SET ";
				if(sn.length()==0&!booktype.equals("null")&!name.equals("null")){
					if(booknumber.length()!=0){
						try{
							n=Integer.parseInt(booknumber);
							if(n==0){
								JOptionPane.showMessageDialog(this,"图书数量不能为0","警告对话框",JOptionPane.WARNING_MESSAGE);
								return;
							}
						}
						catch(IllegalArgumentException e1){
							JOptionPane.showMessageDialog(this,"请在数量框输入数字","警告对话框",JOptionPane.WARNING_MESSAGE);
							return;
						}
						int b=Integer.parseInt(result[id][7].toString());
						if(n>=b){
								changebook=changebook+"BookNumber = "+n+",";
								int y=n-b;
								changebook=changebook+"BookLastNumber = "+y+",";
								result[id][6]=new Integer(n);
								result[id][8]=new Integer(y);
						}
						else{
							JOptionPane.showMessageDialog(this,"总数量不能小于借出数量","警告对话框",JOptionPane.WARNING_MESSAGE);
							return;
						}
					}
					if(bookprice.length()!=0&!bookprice.equals("null")){
						try{
							d=Double.parseDouble(bookprice);
							if(d==0.0){
								JOptionPane.showMessageDialog(this,"价格不能为0","警告对话框",JOptionPane.WARNING_MESSAGE);
								return;
							}
						}
						catch(IllegalArgumentException e3){
							JOptionPane.showMessageDialog(this,"价格框要输入数字","警告对话框",JOptionPane.WARNING_MESSAGE);
							return;
						}
						changebook=changebook+"BookPrice = "+d+",";
						result[id][5]=new Double(d);
					}
					if(bookprice.equals("null")){
						changebook=changebook+"BookPrice = "+0.0+",";
						result[id][5]=new Double(0.0);
					}
					if(booktype.length()!=0){
						changebook=changebook+"BookType = "+"'"+booktype+"',";
						result[id][4]=booktype;
					}
					if(name.length()!=0){
						changebook=changebook+"BookName = "+"'"+name+"',";
						result[id][1]=name;
					}
					if(publi.length()!=0&!publi.equals("null")){
						changebook=changebook+"BookPublish = "+"'"+publi+"',";
						result[id][2]=publi;
					}
					if(publi.equals("null")){
						changebook=changebook+"BookPublish = "+null+",";
						result[id][2]=null;
					}
					if(auth.length()!=0&!auth.equals("null")){
						changebook=changebook+"BookAuthor = "+"'"+auth+"',";
						result[id][3]=auth;
					}
					if(auth.equals("null")){
						changebook=changebook+"BookAuthor = "+null+",";
						result[id][3]=null;
					}
					changebook=changebook.substring(0,changebook.length()-1);
					if(changebook.length()!=20){
						changebook=changebook+" WHERE BookID = "+"'"+result[id][0]+"'";
						try{
							int p=statesql.executeUpdate(changebook);
							if(p==1){
								JOptionPane.showMessageDialog(this,"修改成功");
							}
							else{
								JOptionPane.showMessageDialog(this,"修改失败");
								result=temp;
								return;
							}
							table.repaint();
						}
						catch(SQLException e2){
							JOptionPane.showMessageDialog(this,"计算机已与服务器断开");
						}
					}	
				}
				else{
					JOptionPane.showMessageDialog(this,"书号不能修改,书名,类型不能为空!","警告对话框",JOptionPane.WARNING_MESSAGE);
				}
			}
		}
		else if(e.getSource()==cancelbutton){
			if(sql!=null){
				try{
					sql.close();
				}
				catch(SQLException e3){
					System.out.println("计算机已与服务器断开");
				}
			}
			if(statesql!=null){
				try{
					statesql.close();
				}
				catch(SQLException e2){
					System.out.println("计算机已与服务器断开");
				}
			}
			dispose();	
		}
		else{
			booksn.setText(null);
			bookname.setText(null);
			publish.setText(null);
			author.setText(null);
			type.setText(null);
			price.setText(null);
			number.setText(null);
		}
	}
}

⌨️ 快捷键说明

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