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

📄 borrowbook.java

📁 图书馆系统
💻 JAVA
字号:
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Point;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.sql.*;

public class borrowBook extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel = null;

	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;

	private JLabel jLabel3 = null;

	private JComboBox jComboBoxbookname = null;

	private JTextField jTextborrowdate = null;

	private JTextField jTextcomment = null;

	private JButton jButtonReset = null;

	private JButton jButtonOK = null;

	private JButton jButtonCancel = null;
	ResultSet rs;
	DBManager db=new DBManager();  //  @jve:decl-index=0:

	private JComboBox jComboBoxstudentname = null;

	public borrowBook() throws HeadlessException {
		// TODO 自动生成构造函数存根
		super();
		initialize();
	}

	public borrowBook(GraphicsConfiguration arg0) {
		super(arg0);
		// TODO 自动生成构造函数存根
		initialize();
	}

	public borrowBook(String arg0) throws HeadlessException {
		super(arg0);
		// TODO 自动生成构造函数存根
		initialize();
	}

	public borrowBook(String arg0, GraphicsConfiguration arg1) {
		super(arg0, arg1);
		// TODO 自动生成构造函数存根
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(375, 295);
		this.setContentPane(getJContentPane());
		this.setTitle("书籍出借");
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowOpened(java.awt.event.WindowEvent e) {
				System.out.println("windowOpened()"); // TODO Auto-generated Event stub windowOpened()
				String strSQL,sql;
				sql="select Username from alluser";
				strSQL="select Bookname from Books";
				rs=db.getResult(strSQL);
				boolean isexist=false;
				try{
					isexist=rs.first();
				}
				catch(SQLException ex){
					
				}
				if(!isexist){
					JOptionPane.showMessageDialog(null, "书库中没有该书目,请确认", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
				else{
					try{
						rs.beforeFirst();
						//循环遍历rs中的数据,将其加到jcomboboxbookname中
						while(rs.next()){
							jComboBoxbookname.addItem(rs.getString("Bookname"));
						}
					}
					catch(SQLException ex){
						
					}
				}
				rs=db.getResult(sql);
				isexist=false;
				try{
					isexist=rs.first();
				}
				catch(SQLException ex){
					
				}
				if(!isexist){
					JOptionPane.showMessageDialog(null, "没有用户,请确认", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
				else{
					try{
						rs.beforeFirst();
						//循环遍历rs中的数据,将其加到jcomboboxbookname中
						while(rs.next()){
							jComboBoxstudentname.addItem(rs.getString("Username"));
						}
					}
					catch(SQLException ex){
						
					}
				}
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("备注");
			jLabel3.setLocation(new Point(40, 120));
			jLabel3.setSize(new Dimension(38, 18));
			jLabel2 = new JLabel();
			jLabel2.setText("日期");
			jLabel2.setLocation(new Point(40, 90));
			jLabel2.setSize(new Dimension(38, 18));
			jLabel1 = new JLabel();
			jLabel1.setText("书名");
			jLabel1.setLocation(new Point(40, 60));
			jLabel1.setSize(new Dimension(38, 18));
			jLabel = new JLabel();
			jLabel.setText("借阅者姓名");
			jLabel.setLocation(new Point(40, 30));
			jLabel.setSize(new Dimension(76, 18));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(jLabel3, null);
			jContentPane.add(getJComboBoxbookname(), null);
			jContentPane.add(getJTextborrowdate(), null);
			jContentPane.add(getJTextcomment(), null);
			jContentPane.add(getJButtonReset(), null);
			jContentPane.add(getJButtonOK(), null);
			jContentPane.add(getJButtonCancel(), null);
			jContentPane.add(getJComboBoxstudentname(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jComboBoxbookname	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxbookname() {
		if (jComboBoxbookname == null) {
			jComboBoxbookname = new JComboBox();
			jComboBoxbookname.setLocation(new Point(143, 60));
			jComboBoxbookname.setEditable(true);
			jComboBoxbookname.setSize(new Dimension(150, 20));
		}
		return jComboBoxbookname;
	}

	/**
	 * This method initializes jTextborrowdate	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextborrowdate() {
		if (jTextborrowdate == null) {
			jTextborrowdate = new JTextField();
			jTextborrowdate.setLocation(new Point(143, 90));
			jTextborrowdate.setSize(new Dimension(150, 20));
		}
		return jTextborrowdate;
	}

	/**
	 * This method initializes jTextcomment	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextcomment() {
		if (jTextcomment == null) {
			jTextcomment = new JTextField();
			jTextcomment.setLocation(new Point(143, 120));
			jTextcomment.setSize(new Dimension(150, 20));
		}
		return jTextcomment;
	}

	/**
	 * This method initializes jButtonReset	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonReset() {
		if (jButtonReset == null) {
			jButtonReset = new JButton();
			jButtonReset.setLocation(new Point(58, 164));
			jButtonReset.setText("清空");
			jButtonReset.setSize(new Dimension(60, 20));
			jButtonReset.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					//jTextstudentname.setText("");
					jComboBoxbookname.setSelectedItem("");
					jTextborrowdate.setText("");
					jTextcomment.setText("");
					jComboBoxstudentname.setSelectedItem("");
				}
			});
		}
		return jButtonReset;
	}

	/**
	 * This method initializes jButtonOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setLocation(new Point(128, 164));
			jButtonOK.setText("确定");
			jButtonOK.setSize(new Dimension(60, 20));
			jButtonOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					if(jComboBoxstudentname.getSelectedItem().toString().trim().equals("")){
						JOptionPane.showMessageDialog(null, "借阅者姓名不能为空", "错误", JOptionPane.ERROR_MESSAGE);
						return;
					}
					if(jComboBoxbookname.getSelectedItem().toString().trim().equals("")){
						JOptionPane.showMessageDialog(null, "书名不能为空", "错误", JOptionPane.ERROR_MESSAGE);
						return;
					}
					if(jTextborrowdate.getText().trim().equals("")){
						JOptionPane.showMessageDialog(null, "借书日期不能为空", "错误", JOptionPane.ERROR_MESSAGE);
						return;
					}
					//判断是否为可借阅用户
					String sql1;
					sql1="select * from alluser where Username='"+jComboBoxstudentname.getSelectedItem().toString().trim()+
					     "'";
					rs=db.getResult(sql1);
					boolean isexist=false;
					try{
						isexist=rs.first();
					}
					catch (SQLException ex1){						
					}
					if(isexist){
//						执行插入操作
						String sql2;
						sql2="select * from Books where Bookname='"+
						      jComboBoxbookname.getSelectedItem().toString().trim()+"'";
						rs=db.getResult(sql2);
						boolean isexist1=false;
						try{
							isexist1=rs.first();
						}
						catch (SQLException ex1){						
						}
						try{
							if(rs.getInt("Kejie")>0 && isexist1){
								String sql;
								sql="insert into BookBorrow(Studentname,Bookname,BorrowDate,Returndate,Comment) values(";
								sql=sql+"'"+jComboBoxstudentname.getSelectedItem().toString().trim()+"',";
								sql=sql+"'"+jComboBoxbookname.getSelectedItem().toString().trim()+"',";
								sql=sql+"'"+jTextborrowdate.getText().trim()+"',";
								sql=sql+"'',";
								sql=sql+"'"+jTextcomment.getText().trim()+"'";
								sql=sql+")";System.out.println(sql);
								if(db.executeSql(sql)){
									String sql4,sql3;
									sql4="select * from Books where Bookname='"+
								      jComboBoxbookname.getSelectedItem().toString().trim()+"'";
									rs=db.getResult(sql4);
									boolean isexist2=false;
									try{
										isexist2=rs.first();
									}
									catch (SQLException ex1){						
									}
									int a=rs.getInt("Kejie"),b=rs.getInt("Totaljie")+1;
									a=a-1;
									sql3="update Books set Kejie='" +a+"',Totaljie='"+
									     b+"'"+
									     " where Bookname='"+jComboBoxbookname.getSelectedItem().toString().trim()+"'";
									rs=db.getResult(sql3);
									JOptionPane.showMessageDialog(null, "添加成功", "成功", JOptionPane.INFORMATION_MESSAGE);
									buttonexit();
								}
								else{
									JOptionPane.showMessageDialog(null, "添加失败,请重新操作", "错误", JOptionPane.ERROR_MESSAGE);
									return;
								}			
							}
							else{
								JOptionPane.showMessageDialog(null, "已全部借出", "错误", JOptionPane.ERROR_MESSAGE);
								return;
							}
						}	
						catch(SQLException e3){
							
						}
									
					}
					else{
						JOptionPane.showMessageDialog(null, "该用户无记录,登记注册后才能借书", "错误", JOptionPane.ERROR_MESSAGE);
						return;
					}
					
				}
			});
		}
		return jButtonOK;
	}

	/**
	 * This method initializes jButtonCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public void buttonexit(){
		this.dispose();
	}
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setLocation(new Point(198, 164));
			jButtonCancel.setText("取消");
			jButtonCancel.setSize(new Dimension(60, 20));
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					buttonexit();
				}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jComboBoxstudentname	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxstudentname() {
		if (jComboBoxstudentname == null) {
			jComboBoxstudentname = new JComboBox();
			jComboBoxstudentname.setLocation(new Point(143, 30));
			jComboBoxstudentname.setEditable(true);
			jComboBoxstudentname.setSize(new Dimension(150, 20));
		}
		return jComboBoxstudentname;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"

⌨️ 快捷键说明

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