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

📄 issuebook.java

📁 采用GUI格式调用的库函数直接写的源码
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;

public class IssueBook extends JInternalFrame implements ActionListener, FocusListener {

	private JPanel pBook = new JPanel ();
	private JLabel lbBookId, lbBookName, lbBookAuthor, lbBookCategory, lbMemberId, lbMemberName;
	private JTextField txtBookId, txtBookName, txtBookAuthor, txtBookCategory, txtMemberId, txtMemberName;
	private JButton btnOk, btnCancel;

	private Statement st;			//Statement for Getting the Required Table.
	private long id = 0;			//To Hold the BookId.
	private long memberId = 0;		//To Hold the MemberId.

	//Constructor of Class.

	public IssueBook (Connection con) {

		//super (Title, Resizable, Closable, Maximizable, Iconifiable)
		super ("Issue Book", false, true, false, true);
		setSize (325, 285);

		//Setting the Form's Labels.

		lbBookId = new JLabel ("Book Id:");
		lbBookId.setForeground (Color.black);
		lbBookId.setBounds (15, 15, 100, 20);
		lbBookName = new JLabel ("Book Name:");
		lbBookName.setForeground (Color.black);
		lbBookName.setBounds (15, 45, 100, 20);
		lbBookAuthor = new JLabel ("Book Author:");
		lbBookAuthor.setForeground (Color.black);
		lbBookAuthor.setBounds (15, 75, 100, 20);
		lbBookCategory = new JLabel ("Book Category:");
		lbBookCategory.setForeground (Color.black);
		lbBookCategory.setBounds (15, 105, 100, 20);
		lbMemberId = new JLabel ("Member Id:");
		lbMemberId.setForeground (Color.black);
		lbMemberId.setBounds (15, 135, 100, 20);
		lbMemberName = new JLabel ("Member Name:");
		lbMemberName.setForeground (Color.black);
		lbMemberName.setBounds (15, 165, 100, 20);

		//Setting the Form's TextFields.

		txtBookId = new JTextField ();
		txtBookId.setHorizontalAlignment (JTextField.RIGHT);
		txtBookId.addFocusListener (this);
		txtBookId.setBounds (120, 15, 175, 25);
		txtBookName = new JTextField ();
		txtBookName.setEnabled (false);
		txtBookName.setBounds (120, 45, 175, 25);
		txtBookAuthor = new JTextField ();
		txtBookAuthor.setEnabled (false);
		txtBookAuthor.setBounds (120, 75, 175, 25);
		txtBookCategory = new JTextField ();
		txtBookCategory.setEnabled (false);
		txtBookCategory.setBounds (120, 105, 175, 25);
		txtMemberId = new JTextField ();
		txtMemberId.setHorizontalAlignment (JTextField.RIGHT);
		txtMemberId.addFocusListener (this);
		txtMemberId.setBounds (120, 135, 175, 25);
		txtMemberName = new JTextField ();
		txtMemberName.setEnabled (false);
		txtMemberName.setBounds (120, 165, 175, 25);

		//Setting the Form's Buttons.

		btnOk = new JButton ("OK");
		btnOk.setBounds (50, 205, 100, 25);
		btnOk.addActionListener (this);
		btnCancel = new JButton ("Cancel");
		btnCancel.setBounds (170, 205, 100, 25);
		btnCancel.addActionListener (this);

		//Registering the KeyListener to Restrict user to type only Numeric in Numeric Boxes.

		txtBookId.addKeyListener (new KeyAdapter () {
			public void keyTyped (KeyEvent ke) {
				char c = ke.getKeyChar ();
				if (! ((Character.isDigit (c)) || (c == KeyEvent.VK_BACK_SPACE))) {
					getToolkit().beep ();
					ke.consume ();
				}
			}
		}
		);

		txtMemberId.addKeyListener (new KeyAdapter () {
			public void keyTyped (KeyEvent ke) {
				char c = ke.getKeyChar ();
				if (! ((Character.isDigit (c)) || (c == KeyEvent.VK_BACK_SPACE))) {
					getToolkit().beep ();
					ke.consume ();
				}
			}
		}
		);

		//Adding All the Controls in Panel.

		pBook.setLayout (null);
		pBook.add (lbBookId);
		pBook.add (lbBookName);
		pBook.add (lbBookAuthor);
		pBook.add (lbBookCategory);
		pBook.add (lbMemberId);
		pBook.add (lbMemberName);
		pBook.add (txtBookId);
		pBook.add (txtBookName);
		pBook.add (txtBookAuthor);
		pBook.add (txtBookCategory);
		pBook.add (txtMemberId);
		pBook.add (txtMemberName);
		pBook.add (btnOk);
		pBook.add (btnCancel);

		//Adding Panel to Form.

		getContentPane().add (pBook, BorderLayout.CENTER);

		try {
			st = con.createStatement ();	//Creating Statement Object.
		}
		catch (SQLException sqlex) {			//If Problem then Show the User a Message.
 			JOptionPane.showMessageDialog (null, "A Problem Occurs While Loading Form.");
 			dispose ();				//Closing the Form.
	 	}

		setVisible (true);

	}

	//Action Performed By the Form's Button.

	public void actionPerformed (ActionEvent ae) {

		Object obj = ae.getSource();

		if (obj == btnOk) {		//If OK Button Pressed.

			if (txtBookId.getText().equals ("")) {
				JOptionPane.showMessageDialog (this, "Book's Id not Provided.");
				txtBookId.requestFocus ();
			}
			else if (txtMemberId.getText().equals ("")) {
				JOptionPane.showMessageDialog (this, "Member's Id not Provided.");
				txtMemberId.requestFocus ();
			}
			else {
				try {	//INSERT Query to Add Book Record in Table.
					String q = "INSERT INTO IssuedBooks (BookId, BookName, BookAuthor, Category, MemberId) " +
					"VALUES (" + id + ", '" + txtBookName.getText() + "', '" + txtBookAuthor.getText() + 
					"','" + txtBookCategory.getText() + "', " + memberId + ")";
					int result = st.executeUpdate (q);	//Running Query.
					if (result == 1) {			//If Query Successful.
						txtClear ();			//Clear All TextFields.
						JOptionPane.showMessageDialog (this, "Record has been Saved.");
					}
					else {					//If Query Failed.
						txtClear ();			//Clear All TextFields.
						JOptionPane.showMessageDialog (this, "Problem while Saving the Record.");
					}
				}
				catch (SQLException sqlex) { }
			}

		}

		if (obj == btnCancel) {		//If Cancel Button Pressed Unload the From.

			setVisible (false);
			dispose();

		}

	}

	//OverRidding the FocusListener Class Function.

	public void focusGained (FocusEvent fe) { }

	public void focusLost (FocusEvent fe) {

		Object obj = fe.getSource ();

		if (obj == txtBookId) {

			if (txtBookId.getText().equals ("")) {	//If TextField is Empty.
			}
			else {
				id = Integer.parseInt (txtBookId.getText ());	//Converting String to Numeric.
				long bookNo;					//Use for Comparing the Book's Id.
				boolean found = false;				//To Confirm the Book's Id Existance.

				try {	//SELECT Query to Retrieved the Record.
					String q = "SELECT * FROM Books WHERE BookId = " + id + "";
					ResultSet rs = st.executeQuery (q);	//Executing the Query.
					rs.next ();				//Moving towards the Record.
					bookNo = rs.getLong ("BookId");		//Storing the Record.
					if (bookNo == id) {			//If Record Found then Display Records.
						found = true;
						txtBookId.setText ("" + id);
						txtBookName.setText ("" + rs.getString ("BookName"));
						txtBookAuthor.setText ("" + rs.getString ("BookAuthor"));
						txtBookCategory.setText ("" + rs.getString ("Category"));
					}
					else {
						found = false;
					}
				}
				catch (SQLException sqlex) {
					if (found == false) {
						txtBookId.requestFocus ();
						txtBookId.setText ("");
						txtBookName.setText ("");
						txtBookAuthor.setText ("");
						txtBookCategory.setText ("");
						JOptionPane.showMessageDialog (this, "Record not Found.");
					}
				}
			}

		}
		else if (obj == txtMemberId) {

			if (txtMemberId.getText().equals ("")) {	//If TextField is Empty.
			}
			else {
				memberId = Integer.parseInt (txtMemberId.getText ());	//Converting String to Numeric.
				long memberNo;						//Use for Comparing the Member's Id.
				boolean find = false;					//To Confirm the Member's Id Existance.

				try {	//SELECT Query to Retrieved the Record.
					String q = "SELECT * FROM Members WHERE MemberId = " + memberId + "";
					ResultSet rs = st.executeQuery (q);	//Executing the Query.
					rs.next ();				//Moving towards the Record.
					memberNo = rs.getLong ("MemberId");	//Storing the Record.
					if (memberNo == memberId) {			//If Record Found then Display Records.
						find = true;
						txtMemberId.setText ("" + memberId);
						txtMemberName.setText ("" + rs.getString ("MemberName"));
					}
					else {
						find = false;
					}
				}
				catch (SQLException sqlex) {
					if (find == false) {
						txtClear ();		//Clearing the TextFields.
						JOptionPane.showMessageDialog (this, "Record not Found.");
					}
				}
			}
		}

	}

	//Function Use to Clear All the TextFields of Form.

	private void txtClear () {

		txtBookId.setText ("");
		txtBookName.setText ("");
		txtBookAuthor.setText ("");
		txtBookCategory.setText ("");
		txtMemberId.setText ("");
		txtMemberName.setText ("");
		txtBookId.requestFocus ();

	}

}	

⌨️ 快捷键说明

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