java+sql server 数据库实现英才大学图书管理系统 第一部分 连接数据库及界面实现

1.英才大学图书馆管理系统
功能点:用户登录、注册、密码修改,新增、修改、删除图书信息,实现图书外借与归还,查询图书信息(精确查找、模糊查找)

这个东西是数据库的一次实验
因为我常年不用java,这也算是一个java大型复健作业,不过由于对面向对象的思想,还有java的很多东西掌握不够牢固,在写的时候应该有很多莫名其妙的语法orz
另外建议swpu的朋友们还是仅供参考,找我要这份代码的人已经相当多了
下载地址:https://download.csdn.net/download/u011612364/13985581
百度网盘链接:https://pan.baidu.com/s/15kH3g5ifDuV4kssP4rL19g
提取码:np4u

一 数据库连接测试

首先是关于数据库的连接,老师也没有讲过,自己试了老久才连上,还是有很多坑的。
这里先给出一份测试连接的代码

import java.sql.*;
public class MySQLDemo {
	public static void main(String[] args) {  
	       System.out.println("数据库原理及应用"); 
	     String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=MyDb";  //你要连的数据库
	      // Declare the JDBC objects.  
	      Connection con = null;  
	      Statement stmt = null;  
	      ResultSet rs = null;  
	      System.out.println("正在连接数据库和检索,请等待...");
	      try {  
	         // Establish the connection.  
	         //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
	         con = DriverManager.getConnection(connectionUrl,"sa","123456");  //账号和密码,注意权限是否开了
	         // Create and execute an SQL statement that returns some data.  
	         String SQL = "select * from IpTable ";  //测试,改成你的表看是否成功
	         stmt = con.createStatement();  
	         rs = stmt.executeQuery(SQL);  
  
	         while (rs.next()) {  
	            System.out.println(rs.getString(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3) + "\t" + rs.getString(4));  
	         }  
	      }   
	      catch (Exception e) {  
	         e.printStackTrace();  
	      }  
	      finally {  
	         if (rs != null) try { rs.close(); } catch(Exception e) {}  
	         if (stmt != null) try { stmt.close(); } catch(Exception e) {}  
	         if (con != null) try { con.close(); } catch(Exception e) {}  
	      }  
	   }  
}

这一步的重点:
当然你想要连接的上数据库,你的连接账号权限设置肯定是不能忘记的。数据库初次下载后默认的是只有Windows身份验证模式,要将设置改一下。
在这里插入图片描述

然后还要在微软官网上下载对应的驱动包,导入eclipse,链接我也给一下https://docs.microsoft.com/zh-cn/sql/connect/jdbc/using-the-jdbc-driver?view=sql-server-2017
导入后的配置如图所示
在这里插入图片描述

二 界面部分

效果图如下
在这里插入图片描述

LoginMenu.java
package YLBlibrary;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginMenu  extends JFrame{
	/*注册登陆按钮*/
	JFrame f = new JFrame("英才大学图书馆管理系统 By 201831064402杨丽冰");
	JButton button1 = new JButton("注册");
	JButton button2 = new JButton("登录");
	JButton button3 = new JButton("修改密码");
	JTextField id = new JTextField();
	JPasswordField password = new JPasswordField();
	public void Menu() {
		//改变窗口图标
		Toolkit t = Toolkit.getDefaultToolkit();
		Image img = t.getImage("src\\picture\\book.png");
		f.setIconImage(img);
		//设置窗口大小
		f.setSize(740,580);	
		//使窗口居中
		f.setLocationRelativeTo(null);
		
		//设置布局为空
		f.setLayout(null);
		
		//设置窗口背景图案
		Icon i = new ImageIcon("src\\picture\\photo1.jpg");
		JLabel jLable = new JLabel(i);
		jLable.setBounds(0, 0, 740, 580);
		
		
		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		JPanel panel3 = new JPanel();
		JPanel panel4 = new JPanel();
		JPanel panel5 = new JPanel();
		JPanel panel6 = new JPanel();
		
		
		/*添加大标题:图书管理系统*/
		JLabel lable = new JLabel("英才大学图书馆管理系统");
		Font font = new Font("宋体",Font.BOLD,30);//设置字体
		lable.setFont(font);
		panel1.add(lable);
		panel1.setBounds(130, 60, 380, 370);//设置面板大小
		f.add(panel1);
		
		
		/*用户类型标签*/
		JLabel user = new JLabel("用户类型  ");
		Font font1 = new Font("宋体",Font.BOLD,25);
		user.setFont(font1);
		panel2.add(user);
			
			
		/*用户类型的下拉框*/
		JComboBox<String> j = new JComboBox<String>();
		Dimension dimension = new Dimension(200,30);//框框的大小
		j.setPreferredSize(dimension);
		j.addItem("普通用户");
		j.addItem("管理员用户");
		Font font2 = new Font("宋体",Font.BOLD,15);
		j.setFont(font2);
		panel2.setBounds(90, 160, 400, 300);
		panel2.add(j);
		f.add(panel2);
		
		/*账号标签*/
		JLabel labid = new JLabel("   账号: ");
		Font font3 = new Font("宋体",Font.BOLD,25);	
		labid.setFont(font3);
		panel3.add(labid);
		
		/*输入账户的文本框*/
		id.setPreferredSize(dimension);
		panel3.add(id);
		panel3.setBounds(90, 230, 400, 300);
		f.add(panel3);
		
		
		/*密码标签*/
		JLabel labpassword = new JLabel("   密码: ");
		Font font4 = new Font("宋体",Font.BOLD,25);
		labpassword.setFont(font4);
		panel4.add(labpassword);
		
		/*输入密码文本框*/
		password.setPreferredSize(dimension);
		Font font5 = new Font("宋体",Font.BOLD,25);	
		password.setFont(font5);
		panel4.add(password);
		panel4.setBounds(90, 290, 400, 300);
		f.add(panel4);
		//按钮
		button1.setFont(font4);
		button2.setFont(font4);
		button3.setFont(font4);
		Dimension dimension2 = new Dimension(100,50);
		Dimension dimension3 = new Dimension(150,50);
		button1.setPreferredSize(dimension2);
		button2.setPreferredSize(dimension2);
		button3.setPreferredSize(dimension3);
		panel5.add(button1);
		panel6.add(button2);
		panel6.add(button3);
		panel5.setBounds(180, 370, 150, 400);
		panel6.setBounds(270, 370, 350, 400);
		f.add(panel5);
		f.add(panel6);
                //把组件变成透明
		panel1.setOpaque(false);
		panel2.setOpaque(false);
		panel3.setOpaque(false);
		panel4.setOpaque(false);
		panel5.setOpaque(false);
		panel6.setOpaque(false);
		
		f.add(jLable);
		//设置不可改变窗口大小
		f.setResizable(false);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// 给按钮注册事件侦听
		MyButtonListener listen = new MyButtonListener();
		button1.addActionListener(listen);
		button2.addActionListener(listen);
		button3.addActionListener(listen);
	}
	
	class MyButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			// 根据事件源是不同按钮,调用不同的方法
			if (evt.getSource() == button3)
				FindPaw();
			if (evt.getSource() == button2)
				Login();
			if (evt.getSource() == button1)
				Register();
		}
	} 
	// 添加数据对应的代码封装在这里
	private void Login() {
		Contro con = new Contro();
		String ID = id.getText();
		String pawd = password.getText();
		int state = con.loginConf(ID, pawd);
		if(state == 1) {
			Globle gl =new Globle();
			User user =new User();
			user.setID(ID);
			gl.set(user);
			f.dispose();
			Mangament obj = new Mangament();
			obj.setSize(740, 580);
			obj.setVisible(true);
			obj.setLocationRelativeTo(null);
		}
		else
			showMsg("登录失败!");
	}
	
	private void showMsg(String sArgs) {
		JOptionPane.showMessageDialog(LoginMenu.this, sArgs);
	}
	
	private void Register() {
		f.dispose();
		RegisterMenu objRej = new RegisterMenu();
	}
	private void FindPaw() {
		f.dispose();
		FindPawMenu objRej = new FindPawMenu();
	}
	public static void main(String args[]) {
		LoginMenu obj = new LoginMenu();
		obj.Menu();
	}	
}

在这里插入图片描述
注册:

package YLBlibrary;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

 
public class RegisterMenu  extends JFrame {
	/*注册按钮*/
	 static JFrame f = new JFrame("英才大学图书馆管理系统 By 201831064402杨丽冰");
	 static JButton button1 = new JButton("注册");
	 JTextField id = new JTextField();
	 JPasswordField password = new JPasswordField();
	 JTextField name = new JTextField();
	 JTextField phone = new JTextField();
	 
	 JPanel panel1 = new JPanel();
	 JPanel panel2 = new JPanel();
	 JPanel panelID = new JPanel();
	 JPanel panelPassWord = new JPanel();
	 JPanel panelName = new JPanel();
	 JPanel panelPhone = new JPanel();
	 JPanel panel5 = new JPanel();
	 public RegisterMenu() { 
		//改变窗口图标
		Toolkit t = Toolkit.getDefaultToolkit();
		Image img = t.getImage("src\\picture\\book.png");
		f.setIconImage(img);
		//设置窗口大小
		f.setSize(740,580);	
		//使窗口居中
		f.setLocationRelativeTo(null);
		//设置布局为空
		f.setLayout(null);
		//设置窗口背景图案
		Icon i = new ImageIcon("src\\picture\\photo1.jpg");
		JLabel jLable = new JLabel(i);
		jLable.setBounds(0, 0, 740, 580);
				
		/*添加大标题:图书管理系统*/
		JLabel lable = new JLabel("英才大学图书馆管理系统");
		Font font = new Font("宋体",Font.BOLD,30);//设置字体
		lable.setFont(font);
		panel1.add(lable);
		panel1.setBounds(130, 60, 380, 370);//设置面板大小
		f.add(panel1);
				
		/*用户类型标签*/
		JLabel user = new JLabel("用户类型  ");
		Font font1 = new Font("宋体",Font.BOLD,25);
		user.setFont(font1);
		panel2.add(user);
				
		/*用户类型的下拉框*/
		JComboBox<String> j = new JComboBox<String>();
		Dimension dimension = new Dimension(200,30);//框框的大小
		j.setPreferredSize(dimension);
		j.addItem("普通用户");
		j.addItem("管理员用户");
		Font font2 = new Font("宋体",Font.BOLD,15);
		j.setFont(font2);
		panel2.setBounds(90, 140, 400, 300);
		panel2.add(j);
		f.add(panel2);
		
		/*账号标签*/
		JLabel labid = new JLabel("   账号: ");
		Font font3 = new Font("宋体",Font.BOLD,25);	
		labid.setFont(font3);
		panelID.add(labid);
		
		/*输入账户的文本框*/
		id.setPreferredSize(dimension);
		panelID.add(id);
		panelID.setBounds(90, 180, 400, 300);
		f.add(panelID);
		
		/*密码标签*/
		JLabel labpassword = new JLabel("   密码: ");
		Font font4 = new Font("宋体",Font.BOLD,25);
		labpassword.setFont(font4);
		panelPassWord.add(labpassword);
		
		/*输入密码文本框*/
		password.setPreferredSize(dimension);
		Font font5 = new Font("宋体",Font.BOLD,25);	
		password.setFont(font5);
		panelPassWord.add(password);
		panelPassWord.setBounds(90, 220, 400, 300);
		f.add(panelPassWord);
		
		/*姓名标签*/
		JLabel labname = new JLabel("   姓名: ");
		Font font6 = new Font("宋体",Font.BOLD,25);
		labname.setFont(font6);
		panelName.add(labname);
		
		/*输入姓名文本框*/
		name.setPreferredSize(dimension);
		Font font7 = new Font("宋体",Font.BOLD,25);	
		name.setFont(font7);
		panelName.add(name);
		panelName.setBounds(90, 260, 400, 300);
		f.add(panelName);
		
		/*电话标签*/
		JLabel labphone = new JLabel("   电话: ");
		labphone.setFont(font6);
		panelPhone.add(labphone);
		
		/*输入电话文本框*/
		phone.setPreferredSize(dimension);
		phone.setFont(font7);
		panelPhone.add(phone);
		panelPhone.setBounds(90, 300, 400, 300);
		f.add(panelPhone);
		
		button1.setFont(font4);
		Dimension dimension2 = new Dimension(100,50);
		
		button1.setPreferredSize(dimension2);
		panel5.add(button1);
		panel5.setBounds(160, 350, 300, 400);
		f.add(panel5);

        //把组件变成透明
		panel1.setOpaque(false);
		panel2.setOpaque(false);
		panelID.setOpaque(false);
		panelPassWord.setOpaque(false);
		panelName.setOpaque(false);
		panelPhone.setOpaque(false);
		panel5.setOpaque(false);
	
		f.add(jLable);
		//设置不可改变窗口大小
		f.setResizable(false);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// 给按钮注册事件侦听
		MyButtonListener listen = new MyButtonListener();
		button1.addActionListener(listen);
	}
	
	class MyButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			// 根据事件源是不同按钮,调用不同的方法
			if (evt.getSource() == button1)
				Register();
		}
	} 
	private void showMsg(String sArgs) {
		JOptionPane.showMessageDialog(RegisterMenu.this, sArgs);
	}
	
	// 对应的代码封装在这里
	private void Register() {
		String ID = id.getText();
		String pawd = password.getText();
		String Name = name.getText();
		String pho = phone.getText(); 
		//System.out.println(ID+" "+Name+" "+pawd+pho);

		Contro con = new Contro();
		int state;
		if(ID.length() == 0 || ID.equals(" "))
			state = 0;
		else
			state= con.adduser(ID, pawd, Name, pho);
		if(state == 1) {
			showMsg("注册成功!");
			LoginMenu objLog = new LoginMenu();
			objLog.Menu();
		}
		else
			showMsg("注册失败!");
		f.dispose();
	}
}

在这里插入图片描述
系统界面

package YLBlibrary;
//作者 swpu杨丽冰 201831064402

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.sql.*;

//Java图形用户界面
public class Mangament extends JFrame {
	JTabbedPane tabbedPane = new JTabbedPane(); // 选项卡
	JPanel panelA, panelB, panelC,panelD,panelE,panelF; // 选项卡上的面板
	// 面板A上的控件
	JLabel lblBno, lblBName, lblAName, lblPrice, lblPhone, lblEmail;
	JTextField txtBno, txtBName, txtAName, txtPrice, txtEmail;
	JButton btnInsertData, buttonCancel;

	// 面板B上的控件
	JLabel lblNoToDel;
	JTextField txtNoToDel;
	JButton btnNoToDel;

	// 面板C上的控件
	JLabel lblNameToFind,lblANameToFind;
	JTextField txtNameToFind,txtANameToFind;
	JButton btnNameToFind,btnANameToFind;
	JTextArea areaShowResult;

		
	//D
	JLabel lblNoLend,lblNameLend,lblIDLend ;
	JTextField txtNoLend,txtNameLend,txtIDLend;
	JButton btnLend;
	
	//E
	JLabel lblNoBorrow,lblIDBorrow ;
	JTextField txtNoBorrow,txtIDBorrow;
	JButton btnBorrow;
	
	//F
	JLabel lblPoint,lblUpBno, lblUpBName, lblUpAName, lblUpPrice, lblUpPhone, lblUpEmail;
	JTextField txtUpBno, txtUpBName, txtUpAName, txtUpPrice, txtUpEmail;
	JButton btnUpData, buttonUpCancel;
	// 构造方法
	
	
	public Mangament() {
		super("(英才大学图书馆管理系统 By 201831064402杨丽冰)"); // 设置窗口标题栏文字
		//图标
		Toolkit t = Toolkit.getDefaultToolkit();
		Image img = t.getImage("src\\picture\\book.png");
		this.setIconImage(img);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		tabbedPane = new JTabbedPane();
		panelA = new JPanel();
		panelB = new JPanel();
		panelC = new JPanel();
		panelD = new JPanel();
		panelE = new JPanel();
		panelF = new JPanel();
		tabbedPane.add("添加图书", panelA);
		tabbedPane.add("删除图书", panelB);
		tabbedPane.add("查询图书", panelC);
		tabbedPane.add("更新图书", panelF);
		tabbedPane.add("借阅图书", panelD);
		tabbedPane.add("归还图书", panelE);
	
//		Icon i = new ImageIcon("src\\picture\\3.jpg");
//		JLabel jLable = new JLabel(i);
//		jLable.setBounds(0, 400, 740, 180);
//		this.add(jLable);
		// 调用方法,往面板上添加组件
		addControlToPanelAdd();
		addControlToPanelDel();
		addControlToPanelQuery();
		addControlToPanelLend();
		addControlToPanelUpdate();
		addControlToPanelBorrow();
		
		// 创建一个标签,用来显示文字,添加在North
		JLabel labelTitle = new JLabel("英才大学图书馆管理系统");
		Globle gl =new Globle();
		User user =new User();
		user = gl.get();
		JLabel labelWelcome = new JLabel("欢迎您用户"+user.getID()+"!");
		labelTitle.setFont(new Font(null, Font.BOLD, 36)); // 设置字体
		labelTitle.setForeground(new Color(60, 60, 160)); // 设置颜色
		JPanel pane1 = new JPanel(); 
		pane1.add(labelTitle);
		pane1.add(labelWelcome);
		getContentPane().add("North", pane1);
		// 添加选项卡组件到居中位置
		getContentPane().add("Center", tabbedPane);
		//按键响应
		MyButtonListener listen = new MyButtonListener();
		btnInsertData.addActionListener(listen);
		btnNoToDel.addActionListener(listen);
		btnNameToFind.addActionListener(listen);
		btnANameToFind.addActionListener(listen);
		btnLend.addActionListener(listen);
		btnUpData.addActionListener(listen);
		btnBorrow.addActionListener(listen);
		
	} 

	// 添加控件到面板B(删除数据)上的方法
	public void addControlToPanelDel() {
		lblNoToDel = new JLabel("请输入要删除的图书编号:");
		txtNoToDel = new JTextField("", 15);
		btnNoToDel = new JButton("删除");
		panelB.add(lblNoToDel);
		panelB.add(txtNoToDel);
		panelB.add(btnNoToDel);
	}

	// 添加控件到面板C(查询数据)上的方法
	public void addControlToPanelQuery() {
		lblNameToFind = new JLabel("请输入要查询的书名(支持模糊查询)");
		txtNameToFind = new JTextField("", 6);
		btnNameToFind = new JButton("查询");
		lblANameToFind = new JLabel("请输入要查询的作者名(支持模糊查询)");
		txtANameToFind = new JTextField("", 6);
		btnANameToFind = new JButton("查询");
		areaShowResult = new JTextArea(20, 80); // 20行,80列

		JPanel pTmp = new JPanel(new GridLayout(2,3));
		pTmp.add(lblNameToFind);
		pTmp.add(txtNameToFind);
		pTmp.add(btnNameToFind);
		pTmp.add(lblANameToFind);
		pTmp.add(txtANameToFind);
		pTmp.add(btnANameToFind);
		
		// 面板C采用边界布局,上方是输入信息组件,下方是显示信息组件
		BorderLayout bl = new BorderLayout();
		panelC.setLayout(bl);

		// 面板C的靠上组件pTmp,居中组件areaShowResult
		panelC.add("North", pTmp);
		panelC.add("Center", areaShowResult);
	}

	// 添加控件到面板A上的方法
	public void addControlToPanelAdd() {
		lblBno = new JLabel("书本编号 *  ");
		lblBName = new JLabel("书名 *  ");
		lblAName = new JLabel("作者姓名 *  "); // 标签后面跟上1个星号提醒用户,这是必须输入的字段
		lblPrice = new JLabel("书本单价");
		lblPhone = new JLabel("电话号码");
		//lblEmail = new JLabel("电子邮箱");

		txtBno = new JTextField("", 12);
		txtBName = new JTextField("", 10);
		txtAName = new JTextField("", 10);
		txtPrice = new JTextField("", 15);
		//txtEmail = new JTextField("", 18);

		btnInsertData = new JButton("添加");
		buttonCancel = new JButton("清空");
		// 对当前面板设置布局管理
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		panelA.setLayout(gbl);
		// 添加控件 第1行
		gbc.gridy = 1;
		gbc.gridx = 1;
		gbl.setConstraints(lblBno, gbc);
		panelA.add(lblBno);
		gbc.gridy = 1;
		gbc.gridx = 2;
		gbl.setConstraints(txtBno, gbc);
		panelA.add(txtBno);
		// 添加控件 第2行
		gbc.gridy = 2;
		gbc.gridx = 1;
		gbl.setConstraints(lblBName, gbc);
		panelA.add(lblBName);
		gbc.gridy = 2;
		gbc.gridx = 2;
		gbl.setConstraints(txtBName, gbc);
		panelA.add(txtBName);
		//
		gbc.gridy = 3;
		gbc.gridx = 1;
		gbl.setConstraints(lblAName, gbc);
		panelA.add(lblAName);
		gbc.gridy = 3;
		gbc.gridx = 2;
		gbl.setConstraints(txtAName, gbc);
		panelA.add(txtAName);
		// 添加控件 第4行
		gbc.gridy = 4;
		gbc.gridx = 1;
		gbl.setConstraints(lblPrice, gbc);
		panelA.add(lblPrice);
		gbc.gridy = 4;
		gbc.gridx = 2;
		gbl.setConstraints(txtPrice, gbc);
		panelA.add(txtPrice);
//		// 添加控件 第5行
//		gbc.gridy = 5;
//		gbc.gridx = 1;
//		gbl.setConstraints(lblEmail, gbc);
//		panelA.add(lblEmail);
//		gbc.gridy = 5;
//		gbc.gridx = 2;
//		gbl.setConstraints(txtEmail, gbc);
//		panelA.add(txtEmail);

		// 添加控件 第7行
		gbc.gridy = 7;
		gbc.gridx = 3;
		gbl.setConstraints(btnInsertData, gbc);
		panelA.add(btnInsertData);
		gbc.gridy = 7;
		gbc.gridx = 4;
		gbl.setConstraints(buttonCancel, gbc);
		panelA.add(buttonCancel);
	}
	//添加控件到面板D上的方法
	public void addControlToPanelLend() {
		lblNoLend = new JLabel("请输入要借阅的图书编号:");
		txtNoLend = new JTextField("", 15);
		lblNameLend = new JLabel("请输入你的姓名:");
		txtNameLend = new JTextField("", 15);
		lblIDLend = new JLabel("请输入你的借阅证号(账号):");
		txtIDLend = new JTextField("", 15);
		btnLend = new JButton("借阅");
		
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		panelD.setLayout(gbl);
		gbc.gridy = 1;
		gbc.gridx = 1;
		gbl.setConstraints(lblNoLend, gbc);
		panelD.add(lblNoLend);
		gbc.gridy = 1;
		gbc.gridx = 2;
		gbl.setConstraints(txtNoLend, gbc);
		panelD.add(txtNoLend);
		gbc.gridy = 2;
		gbc.gridx = 1;
		gbl.setConstraints(lblNameLend, gbc);
		panelD.add(lblNameLend);
		gbc.gridy = 2;
		gbc.gridx = 2;
		gbl.setConstraints(txtNameLend, gbc);
		panelD.add(txtNameLend);
		gbc.gridy = 3;
		gbc.gridx = 1;
		gbl.setConstraints(lblIDLend, gbc);
		panelD.add(lblIDLend);
		gbc.gridy = 3;
		gbc.gridx = 2;
		gbl.setConstraints(txtIDLend, gbc);
		panelD.add(txtIDLend);
		gbc.gridy = 4;
		gbc.gridx = 2;
		gbl.setConstraints(btnLend, gbc);
		panelD.add(btnLend);
	}
	
	//E
	public void addControlToPanelBorrow() {
		lblNoBorrow = new JLabel("请输入要归还的图书编号:");
		txtNoBorrow = new JTextField("", 15);
		lblIDBorrow = new JLabel("请输入你的借阅证号(账号):");
		txtIDBorrow = new JTextField("", 15);
		btnBorrow = new JButton("归还");
		
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		panelE.setLayout(gbl);
		gbc.gridy = 1;
		gbc.gridx = 1;
		gbl.setConstraints(lblNoBorrow, gbc);
		panelE.add(lblNoBorrow);
		gbc.gridy = 1;
		gbc.gridx = 2;
		gbl.setConstraints(txtNoBorrow, gbc);
		panelE.add(txtNoBorrow);
		gbc.gridy = 2;
		gbc.gridx = 1;
		gbl.setConstraints(lblIDBorrow, gbc);
		panelE.add(lblIDBorrow);
		gbc.gridy = 2;
		gbc.gridx = 2;
		gbl.setConstraints(txtIDBorrow, gbc);
		panelE.add(txtIDBorrow);
		gbc.gridy = 3;
		gbc.gridx = 2;
		gbl.setConstraints(btnBorrow, gbc);
		panelE.add(btnBorrow);
	}
	//添加控件到面板F上的方法
	public void  addControlToPanelUpdate(){
		lblPoint = new JLabel("请输入您要更新的图书信息 ");
		lblUpBno = new JLabel("书本编号 *  ");
		lblUpBName = new JLabel("书名 *  ");
		lblUpAName = new JLabel("作者姓名 *  "); // 标签后面跟上1个星号提醒用户,这是必须输入的字段
		lblUpPrice = new JLabel("书本单价");
		lblUpPhone = new JLabel("电话号码");
		//lblEmail = new JLabel("电子邮箱");

		txtUpBno = new JTextField("", 12);
		txtUpBName = new JTextField("", 10);
		txtUpAName = new JTextField("", 10);
		txtUpPrice = new JTextField("", 15);
		//txtEmail = new JTextField("", 18);

		btnUpData = new JButton("更新");
		buttonUpCancel = new JButton("清空");
		// 对当前面板设置布局管理
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		panelF.setLayout(gbl);
		// 添加控件 第1行
		gbc.gridy = 1;
		gbc.gridx = 1;
		gbl.setConstraints(lblUpBno, gbc);
		panelF.add(lblUpBno);
		gbc.gridy = 1;
		gbc.gridx = 2;
		gbl.setConstraints(txtUpBno, gbc);
		panelF.add(txtUpBno);
		// 添加控件 第2行
		gbc.gridy = 2;
		gbc.gridx = 1;
		gbl.setConstraints(lblUpBName, gbc);
		panelF.add(lblUpBName);
		gbc.gridy = 2;
		gbc.gridx = 2;
		gbl.setConstraints(txtUpBName, gbc);
		panelF.add(txtUpBName);
		//
		gbc.gridy = 3;
		gbc.gridx = 1;
		gbl.setConstraints(lblUpAName, gbc);
		panelF.add(lblUpAName);
		gbc.gridy = 3;
		gbc.gridx = 2;
		gbl.setConstraints(txtUpAName, gbc);
		panelF.add(txtUpAName);
		// 添加控件 第4行
		gbc.gridy = 4;
		gbc.gridx = 1;
		gbl.setConstraints(lblUpPrice, gbc);
		panelF.add(lblUpPrice);
		gbc.gridy = 4;
		gbc.gridx = 2;
		gbl.setConstraints(txtUpPrice, gbc);
		panelF.add(txtUpPrice);

		// 添加控件 第7行
		gbc.gridy = 7;
		gbc.gridx = 3;
		gbl.setConstraints(btnUpData, gbc);
		panelF.add(btnUpData);
		gbc.gridy = 7;
		gbc.gridx = 4;
		gbl.setConstraints(buttonUpCancel, gbc);
		panelF.add(buttonUpCancel);
	}
	// 显示提示信息的方法
	private void showMsg(String sArgs) {
		JOptionPane.showMessageDialog(Mangament.this, sArgs);
	}

	class MyButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			// 根据事件源是不同按钮,调用不同的方法
			if (evt.getSource() == btnInsertData)
				insertData();
			if (evt.getSource() == btnNoToDel)
				deleteData();
			if (evt.getSource() == btnNameToFind)
				try {
					selectData();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (evt.getSource() == btnANameToFind)
				try {
					selectAData();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (evt.getSource()  == btnLend)
				try {
					LendBook();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if( evt.getSource() == btnBorrow)
				try {
					BorrowBook();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(evt.getSource()==btnUpData)
				updateData();
		}
	} // end of class MyButtonListener

	// 添加数据对应的代码封装在这里
	private void insertData() {
		String BNo = txtBno.getText();
		int id = Integer.parseInt(BNo);
		String BName = txtBName.getText();
		String AName = txtAName.getText();
		String Price = txtPrice.getText();
		double price = Double.parseDouble(Price);
		String sSapce = "    ";// 四个空格
		String sTmp = BNo + sSapce + BName + AName + Price ;
		showMsg("图书添加成功!");
		Contro control = new Contro();
		try {
			control.addbook(id, BName, AName, price);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 删除数据对应的代码封装在这里
	private void deleteData() {
		String BNo = txtNoToDel.getText();
		int id = Integer.parseInt(BNo);
		Contro control = new Contro();
		control.deletebook(id);
		showMsg("您要删除的书本号是:" + BNo+",删除成功!");
	}

	// 查询数据对应的代码封装在这里
	private void selectData() throws Exception {
		String BName = txtNameToFind.getText();
		showMsg("您要查询的书本信息:" + BName);
		Contro control = new Contro();
		control.qury(areaShowResult, "name", BName);
	}
	
	private void selectAData() throws Exception {
		String AName = txtANameToFind.getText();
		showMsg("您根据作者查询的信息:" + AName);
		Contro control = new Contro();
		control.qury(areaShowResult, "aname", AName);
	}
	
	private void LendBook() throws Exception {
		String ID = txtNoLend.getText();
		int id =Integer.parseInt(ID);
		String name = txtNameLend.getText();
		String Uid = txtIDLend.getText();
		showMsg(name+"您要借阅的图书编号:"+id);
		Contro con = new Contro();
		if(con.lend(id, name, Uid)== 0)
			showMsg("借阅失败!");
		else
			showMsg("借阅成功!");
	}
	
	private void BorrowBook() throws Exception {
		String ID = txtNoBorrow.getText();
		int id =Integer.parseInt(ID);
		String Uid = txtIDBorrow.getText();
		showMsg("您要归还的图书编号:"+id);
		Contro con = new Contro();
		if(con.borrow(id,Uid)== 0)
			showMsg("归还失败!");
		else
			showMsg("归还成功!");
	}
	
	private void updateData() {
		String BNo = txtUpBno.getText();
		int id = Integer.parseInt(BNo);
		String BName = txtUpBName.getText();
		String AName = txtUpAName.getText();
		String Price = txtUpPrice.getText();
		String sSapce = "    ";// 四个空格
		String sTmp = BNo + sSapce + BName + AName + Price ;
		System.out.println( sTmp );
		Contro control = new Contro();
		try {
			control.updatebook(id, BName,Price , AName);
			showMsg("图书更新成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			showMsg("图书更新失败!");
			e.printStackTrace();
		}
	}
}
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值