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

📄 returnbook.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 returnBook 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 JTextField jTextreturndate = null;

	private JTextField jTextcomment = null;

	private JComboBox jComboBoxbookname = null;

	private JButton jButtonReset = null;

	private JButton jButtonOK = null;

	private JButton jButtonCancel = null;
	ResultSet rs;
	private DBManager db=new DBManager();

	private JComboBox jComboBoxstudentname = null;
	

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

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

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

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

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(423, 304);
		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;
				strSQL="select Bookname from Books";
				String sql="select Studentname from BookBorrow";
				rs=db.getResult(sql);
				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()){
							jComboBoxstudentname.addItem(rs.getString("Studentname"));
						}
					}
					catch(SQLException ex){
						
					}
				}
				rs=db.getResult(strSQL);
			    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){
						
					}
				}
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("备注");
			jLabel3.setLocation(new Point(45, 120));
			jLabel3.setSize(new Dimension(38, 18));
			jLabel2 = new JLabel();
			jLabel2.setText("日期");
			jLabel2.setLocation(new Point(45, 90));
			jLabel2.setSize(new Dimension(38, 18));
			jLabel1 = new JLabel();
			jLabel1.setText("书名");
			jLabel1.setLocation(new Point(45, 60));
			jLabel1.setSize(new Dimension(38, 18));
			jLabel = new JLabel();
			jLabel.setText("还书者姓名");
			jLabel.setLocation(new Point(45, 30));
			jLabel.setSize(new Dimension(91, 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(getJTextreturndate(), null);
			jContentPane.add(getJTextcomment(), null);
			jContentPane.add(getJComboBoxbookname(), null);
			jContentPane.add(getJButtonReset(), null);
			jContentPane.add(getJButtonOK(), null);
			jContentPane.add(getJButtonCancel(), null);
			jContentPane.add(getJComboBoxstudentname(), null);
		}
		return jContentPane;
	}

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

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

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

	/**
	 * This method initializes jButtonReset	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonReset() {
		if (jButtonReset == null) {
			jButtonReset = new JButton();
			jButtonReset.setLocation(new Point(60, 165));
			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()
					jComboBoxstudentname.setSelectedItem("");
					jComboBoxbookname.setSelectedItem("");
					jTextreturndate.setText("");
					jTextcomment.setText("");
				}
			});
		}
		return jButtonReset;
	}

	/**
	 * This method initializes jButtonOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setLocation(new Point(140, 165));
			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()
					String strSQL;
					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(jTextreturndate.getText().trim().equals("")){
						JOptionPane.showMessageDialog(null, "还书日期不能为空", "错误", JOptionPane.ERROR_MESSAGE);
						return;
					}
					//检验是否此人借的书
					strSQL="select * from BookBorrow where Studentname='"+
					jComboBoxstudentname.getSelectedItem().toString().trim()+"'";
					strSQL=strSQL+" and Bookname='"+jComboBoxbookname.getSelectedItem().toString().trim()+"'";
					rs=db.getResult(strSQL);
					boolean isexist=false;
					try{
						isexist=rs.first();
					}
					catch(SQLException ex1){
						
					}
					//若不存在,提示用户重新输入
					if(!isexist){
						JOptionPane.showMessageDialog(null, jComboBoxstudentname.getSelectedItem().toString().trim()+
								"同学并未借得<<"+jComboBoxbookname.getSelectedItem().toString().trim()+
								">>请确认", "错误", JOptionPane.ERROR_MESSAGE);
						return;
					}
					//生成更新sql语句
					strSQL="update BookBorrow set Returndate='"+jTextreturndate.getText().trim()+"',";
					strSQL=strSQL+"Comment='"+jTextcomment.getText().trim()+"'";
					strSQL=strSQL+" where Studentname='"+jComboBoxstudentname.getSelectedItem().toString().trim()+"'";
					strSQL=strSQL+" and Bookname='"+jComboBoxbookname.getSelectedItem().toString().trim()+"'";
					System.out.println(strSQL);
					String sql4,sql3;
					sql4="select * from Books where Bookname='"+
				      jComboBoxbookname.getSelectedItem().toString().trim()+"'";
					//由数据库执行
					rs=db.getResult(sql4);
					boolean isexist2=false;
					int a=1,b=0;
					try{
						isexist2=rs.first();
						a=rs.getInt("Kejie")+1;
						b=rs.getInt("Jiechu");						
					}
					catch (SQLException ex1){						
					}		
					if(db.executeSql(strSQL) && b>0){
						sql3="update Books set Kejie='" +a+"'"+
						     " 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;
					}
				}
			});
		}
		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(220, 165));
			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(160, 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 + -