Java图书管理系统,java课程实习

前序

运行环境:Eclipse,SQL Server
完整zip下载链接:http://download.csdn.net/download/android_tools/10186799
如果没有C币的话可以联系我哈~:qq1403528138
里面包括project文件 sql server 数据库文件 皮肤包
皮肤包使用:beautyeye_inf 是其中的皮肤包,下载后lib里添加依赖,运行主函数里写入

try{
  org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
}
catch(Exception e){
  //TODO exception
}
  //这里写你的代码

在此感谢JackJiang 大神提供的皮肤包。
正文

代码片太多,建议下载完整project体会使用
主要功能:管理员登陆,读者模式,图书借还,图书管理,用户管理。
里面包括图书—用户的搜索、添加、修改、删除。

登陆界面:
Java图书管理系统,java课程实习_第1张图片

package bbm.view;
import java.awt.*;
import javax.swing.*;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class login extends JFrame implements ActionListener{
      public JButton jb1,jb2=null;
      public JRadioButton jrb1,jrb2=null;
      public JPanel jp1,jp2,jp3,jp4=null;
      public JTextField jtf=null;
      public JLabel jlb1,jlb2,jlb3=null;
      public JPasswordField jpf=null;
      public ButtonGroup bg=null;
	  
      boolean readerModel=false;
	  final String admin_name="admin";
	  final String admin_password="123456";
	  public int width;
	  public int height;	
		
		public login() {
			
			jb1=new JButton("登录");
		    jb2 = new JButton("重置");
			
			jb1.addActionListener(this);
			jb2.addActionListener(this);
			
			jrb1=new JRadioButton("管理员");
			jrb2=new JRadioButton("读者模式");
			
			bg=new ButtonGroup();
			bg.add(jrb1);
			bg.add(jrb2);
			jrb2.setSelected(true);
			
			jp1=new JPanel();
			jp2=new JPanel();
			jp3=new JPanel();
			jp4=new JPanel();
			
			jlb1=new JLabel("用 户 名:");
		    jlb2=new JLabel("密    码:");
		    jlb3=new JLabel(" 权限:");
		    
		    jtf=new JTextField(15);
		    jpf=new JPasswordField(15);
		    
		    jp1.add(jlb1);
		    jp1.add(jtf);
		    
		    jp2.add(jlb2);
		    jp2.add(jpf);
		    
		    jp3.add(jlb3);
		    jp3.add(jrb1);
		    jp3.add(jrb2);
		    
		    jp4.add(jb1);
		    jp4.add(jb2);
		    
		    this.add(jp1);
		    this.add(jp2);
		    this.add(jp3);
		    this.add(jp4);
		    //这里体会表格布局前行后列
		    this.setLayout(new GridLayout(4,1));
		    this.setTitle("图书借阅管理系统");
		    // 得到显示器屏幕的宽高
		    width = Toolkit.getDefaultToolkit().getScreenSize().width;
		    height = Toolkit.getDefaultToolkit().getScreenSize().height;
		    
		    
		    //设置窗体大小
		    this.setSize(300,250);
		    //设置窗体位置
		    this.setLocation((width - 300)/2 ,(height - 200)/2);
		    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		    
		    this.setVisible(true);
		    this.setResizable(true);
		    
		}
		
		
		public static void main(String args[]) {
			try
		    {
		        org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
		    }
		    catch(Exception e)
		    {
		        //TODO exception
		    }			
			new login();
			
		}
		
		
		//实现点击事件
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(e.getActionCommand()=="登录") {
				if(jrb1.isSelected()) {
					adminlogin();
					
				}else if(jrb2.isSelected()) {
					readerlogin();	
				}
			}
			
			if(e.getActionCommand()=="重置") {
			    	clear();
			}
			
		}
		//这里是两种登陆模式,由于时间原因还没有时间细化
		
		public void readerlogin() {
			readerModel=true;
			this.dispose();
			MainPage main=new MainPage();
			
		}
		
		
		public void adminlogin() {
			if(admin_name.equals(jtf.getText())&&admin_password.equals(jpf.getText())) {
				JOptionPane.showMessageDialog(null, "登陆成功!","提示消息",JOptionPane.PLAIN_MESSAGE);
				this.dispose();
				MainPage main=new MainPage();
				
			}else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty()) {
				JOptionPane.showMessageDialog(null, "请输入用户名和密码!","提示消息",JOptionPane.WARNING_MESSAGE);
				
			}else if(jtf.getText().isEmpty()) {
				JOptionPane.showMessageDialog(null, "请输入用户名!","提示消息", JOptionPane.WARNING_MESSAGE);
				
			}else if(jpf.getText().isEmpty()) {
				JOptionPane.showMessageDialog(null, "请输密码!","提示消息", JOptionPane.WARNING_MESSAGE);
				
			}else {
				JOptionPane.showMessageDialog(null, "用户名或密码错误!","提示消息", JOptionPane.ERROR_MESSAGE);
				clear();
			}
			
		}
		
		public void clear() {
			jtf.setText("");
			jpf.setText("");
			
		}	
	}

主界面:
Java图书管理系统,java课程实习_第2张图片

package bbm.view;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

public class MainPage extends JFrame  {
	 JPanel main_jp0,JLabelJP=null;
	 JTabbedPane jtp=null;
	 JButton main_jb0,main_jb1;
	 ReaderModel mod=new ReaderModel();
	 BookManage bog=new BookManage();
	 BookBorrow borrow=new BookBorrow();
	 UserManage user=new UserManage();
	public MainPage() {
		 
		 main_jp0=new JPanel();	
		 //这里用的是选项卡JTabbedPane	 
		 jtp =new JTabbedPane();      
		 jtp.add("读者模式", mod.parent);
		 jtp.add("图书借还", borrow.parentJPanel);
		 jtp.add("图书管理", bog.panel);
		 jtp.add("用户管理", user.userJP);
			 
		 jtp.setPreferredSize(new Dimension(960,740));
		 main_jp0.add(jtp);
		 this.add(main_jp0);
		  
		this.setTitle("图书借阅管理系统");
		Toolkit kit=null;
	    int width=kit.getDefaultToolkit().getScreenSize().width;
		int height=kit.getDefaultToolkit().getScreenSize().height;
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(1050, 850);
		this.setLocation((width-1000)/2, (height-800)/2);
		this.setVisible(true);
		
	}
	/*
	public static void main(String[] args) {			
		MainPage main=new MainPage();
	}
	*/
	
}

读者模式:
(上图选项卡即包含读者模式)

package bbm.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.sql.*;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class ReaderModel  {
	JPanel parent;
	JPanel bro2;
	
	Vector rowData,columnNames;
	JTable jt=null;
	JScrollPane jsp=null;
	
	PreparedStatement ps=null;
	Connection ct=null;
	ResultSet rs=null;
	String url="jdbc:sqlserver://localhost:1433;databaseName=BookManagement"; //数据库名
    String user="sa";                  //sa超级管理员登陆用户名
    String password="a869355161";    //sql server登陆密码
	
	
	ReaderModel(){
       //jdbc连接数据库 表格控件显示数据库信息
		parent=new JPanel(new BorderLayout());		
		bro2=new JPanel(new BorderLayout());
		
		columnNames=new Vector();
		columnNames.add("图书号");
		columnNames.add("图书类型");
		columnNames.add("书名");
		columnNames.add("作者");
		columnNames.add("未借出本书");
		columnNames.add("价格");
		
		rowData=new Vector();
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			ct=DriverManager.getConnection(url,user,password);
			ps=ct.prepareStatement("select * from 图书表");
			rs=ps.executeQuery();
			System.out.println("连接数据库成功...");
			while(rs.next()) {
				Vector hang=new Vector();
				hang.add(rs.getString(1));
				hang.add(rs.getString(2));
				hang.add(rs.getString(3));
				hang.add(rs.getString(4));
				hang.add(rs.getString(5));
				hang.add(rs.getString(6));
				System.out.println("已将数据插入至表格...");
				
				//将hang里的数据加入到rowData
				rowData.add(hang);
			}
			
		}catch(Exception e) {
			 e.printStackTrace();
		}finally {
		    try {
		    	if(rs!=null){  
	                  rs.close();  
	                  }  
	                  if(ps!=null){  
	                      ps.close();  
	                  }  
	                  if(ct!=null){  
	                      ct.close();
		              }
		    }catch(Exception e) {
		    	 e.printStackTrace(); 
		    }					
		}
	 //jt.setEnabled(false);	
	 jt=new JTable(rowData,columnNames);
	 jsp=new JScrollPane(jt);
	 jt.getTableHeader().setPreferredSize(new Dimension(1,40));
	 jt.setRowHeight(28);
	 bro2.add(jsp);	 	 
	 parent.add(bro2);
	 researchBook research=new researchBook();	
	 parent.add(research.bro1,BorderLayout.NORTH);
	 
    
			   	
	}
	
 class researchBook implements ActionListener {
    	JTextField jtf=new JTextField(65);
        JPanel bro1=null;
    	JButton jb0=new JButton("搜索");
    	
    	researchBook(){  		
    		bro1=new JPanel();
    		bro1.add(jtf);
    		bro1.add(jb0);
    		jb0.addActionListener(this);
    		 		
    	}

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
			if(e.getActionCommand()=="搜索") {
				System.out.println("点击事件已触发");				
			    DefaultTableModel tableModel = (DefaultTableModel)jt.getModel();
			    tableModel.setRowCount(0);
			    System.out.println("实现清除表格");
			    String s=jtf.getText().toString();
			    System.out.println(s);
			    try {
					Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
					ct=DriverManager.getConnection(url,user,password);
					String expr="select * from 图书表  "
							+ "where 书名 like '%"+s+"%' "
							+ "or 图书类型  like '%"+s+"%' "
							+ "or 作者 like '%"+s+"%' ";
					ps=ct.prepareStatement(expr);				
					rs=ps.executeQuery();
					System.out.println(ps.toString());
					while(rs.next()) {
						Vector hang=new Vector();
						hang.add(rs.getString(1));
						hang.add(rs.getString(2));
						hang.add(rs.getString(3));
						hang.add(rs.getString(4));
						hang.add(rs.getString(5));
						hang.add(rs.getString(6));
						System.out.println("已将数据插入至表格...");
						
						//将hang里的数据加入到rowData
						rowData.add(hang);
						jt=new JTable(rowData,columnNames);
						jsp=new JScrollPane(jt);
						jt.getTableHeader().setPreferredSize(new Dimension(1,40));
						jt.setRowHeight(28);
						bro2.add(jsp);	 	 
						parent.add(bro2);
					}
					
				}catch(Exception es) {
					 es.printStackTrace();
				}finally {
				    try {
				    	if(rs!=null){  
			                  rs.close();  
			                  }  
			                  if(ps!=null){  
			                      ps.close();  
			                  }  
			                  if(ct!=null){  
			                      ct.close();
				              }
				    }catch(Exception es) {
				    	 es.printStackTrace(); 
				    }					
				}				
			}			
		}			
	}
}

图书借还:
Java图书管理系统,java课程实习_第3张图片

package bbm.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class BookBorrow extends JFrame implements ActionListener {
	
	ReturnRecord record=new ReturnRecord();
	ReFrashTable refrash=new ReFrashTable();    
    JPanel son_jp1;
    Vector rowData,columnNames;
	JTable jt=null;
	JScrollPane jsp=null;
	
	//数据库部分
	PreparedStatement ps=null;
	Connection ct=null;
	ResultSet rs=null;
	String url="jdbc:sqlserver://localhost:1433;databaseName=BookManagement";
    String user="sa";                  //sa超级管理员
    String password="a869355161";    //密码
	

	public JPanel parentJPanel,btnJPanel,btnSonJP0,btnSonJP1,btnSonJP2,tableJPanel;
	private JButton borrowBtn,returnBtn,checkBtn,refrashBtn;
	
	
	public BookBorrow (){
		parentJPanel=new JPanel(new BorderLayout());
		
		btnJPanel=new JPanel(new GridLayout(1,4));
		borrowBtn=new JButton("借书");
		returnBtn=new JButton("还书");
	    checkBtn=new JButton("还书记录");
	    refrashBtn=new JButton("未还书籍");
	    borrowBtn.addActionListener(this);
	    returnBtn.addActionListener(this);
	    checkBtn.addActionListener(this);
	    refrashBtn.addActionListener(this);
	    
	    btnJPanel.add(borrowBtn);
	    btnJPanel.add(returnBtn);
	    btnJPanel.add(checkBtn);
	    btnJPanel.add(refrashBtn);

	    
	    parentJPanel.add(btnJPanel,BorderLayout.NORTH);
	    
	    
	  //表列
    	son_jp1=new JPanel(new BorderLayout());
		columnNames=new Vector();
		columnNames.add("用户名");
		columnNames.add("图书号");
		columnNames.add("书名");
		columnNames.add("借出日期");
		columnNames.add("是否归还");
		columnNames.add("逾期罚金");
		
		rowData=new Vector();
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			ct=DriverManager.getConnection(url,user,password);
			ps=ct.prepareStatement("select 用户名,借书记录表.图书号,书名,借出日期,是否归还,逾期罚金  from 借书记录表,图书表"
					+ " where 是否归还='否' and 图书表.图书号=借书记录表.图书号 ");
			rs=ps.executeQuery();
			//          
			System.out.println("连接数据库成功...");
			while(rs.next()) {
				Vector hang=new Vector();
				hang.add(rs.getString(1));
				hang.add(rs.getString(2));
				hang.add(rs.getString(3));				
				hang.add(rs.getString(4));	
				hang.add(rs.getString(5));
				hang.add(rs.getString(6));
				
				System.out.println("已将数据插入至表格...");		
				//将hang里的数据加入到rowData
				rowData.add(hang);
				
			}			
		}catch(Exception e) {
			 e.printStackTrace();
		}finally {
		    try {
		    	if(rs!=null){  
	                  rs.close();  
	                  }  
	                  if(ps!=null){  
	                      ps.close();  
	                  }  
	                  if(ct!=null){  
	                      ct.close();
		              }
		    }catch(Exception e) {
		    	 e.printStackTrace(); 
		    }					
		}	
	 jt=new JTable(rowData,columnNames);
	 jsp=new JScrollPane(jt);
	 jt.getTableHeader().setPreferredSize(new Dimension(1,40));
	 jt.setRowHeight(28);
	 son_jp1.add(jsp);	 	 
	 parentJPanel.add(son_jp1);
	 
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		if(e.getActionCommand()=="借书") {
			BorrowDialog borrow=new BorrowDialog(this);
		    
		}
		if(e.getActionCommand()=="还书") {			
			ReturnDialog returns=new ReturnDialog(this);
			
		}
		if(e.getSource()==checkBtn) {
			son_jp1.remove(jsp);  //移除控件
			son_jp1.remove(refrash.jsp); 
			son_jp1.add(record.jsp);			
			son_jp1.updateUI(); //更新界面
			son_jp1.repaint();  //重绘
			//记录:动态加载控件
		}
		if(e.getSource()==refrashBtn) {
			son_jp1.remove(jsp);
			son_jp1.remove(record.jsp);
			son_jp1.add(refrash.jsp);			
			son_jp1.updateUI();
			son_jp1.repaint();
			//记录:动态加载控件
		}
	}

}

借书Dialog(弹框)
Java图书管理系统,java课程实习_第4张图片

package bbm.view;

import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import bbm.Dao.bookJdbc;

public class BorrowDialog extends JFrame implements ActionListener  {

	private JLabel userName;
	private JLabel bookNumber;
	private JLabel bookName;	
	private JLabel borrowDate;
	private JButton submitBut,cancelBut;
	private JTextField userNameText;  
	private JTextField bookNumberText;  
	private JTextField bookNameText;  
	private JTextField borrowDateText;  
	JDialog dialog;
	private JLabel space;
	JPanel sonAddDialogJP;
	Toolkit kit;
	
	public BorrowDialog(JFrame jframe) {
		dialog=new JDialog(jframe,"借书",true);
		sonAddDialogJP=new JPanel(new FlowLayout());
		userName=new JLabel("用 户 名:");
		bookNumber=new JLabel("图 书 号:");
		bookName=new JLabel(" 书   名:");
		borrowDate=new JLabel("借书日期:");
		
		//不要修改
		space=new JLabel("                                                                  ");
		userNameText=new JTextField(15);
		bookNumberText=new JTextField(15);
		bookNameText=new JTextField(15);		
		borrowDateText=new JTextField(15);
		submitBut=new JButton("确认");  
        cancelBut=new JButton("取消"); 
        submitBut.addActionListener(this);  
        cancelBut.addActionListener(this);
        
        sonAddDialogJP.add(userName);
		sonAddDialogJP.add(userNameText);
		sonAddDialogJP.add(bookNumber);
		sonAddDialogJP.add(bookNumberText);
		sonAddDialogJP.add(bookName);
		sonAddDialogJP.add(bookNameText);
		sonAddDialogJP.add(borrowDate);
		sonAddDialogJP.add(borrowDateText);
		
        sonAddDialogJP.add(space);
		
		sonAddDialogJP.add(submitBut);
        sonAddDialogJP.add(cancelBut);
        dialog.add(sonAddDialogJP);
        int width=kit.getDefaultToolkit().getScreenSize().width;
        int height=kit.getDefaultToolkit().getScreenSize().height;
        dialog.setSize(260,290);
        dialog.setLocation((width-350)/2, (height-300)/2);
        //要设置最后显示画面,不然位置设置还没好,就会先弹出一个压缩后没有大小的框
        dialog.setVisible(true);
	
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==submitBut) {
			if(userNameText.getText().toString()
					.equals("")||bookNameText.getText().toString()
					.equals("")||borrowDateText.getText().toString().equals("")) {
				JOptionPane.showMessageDialog(this,"输入不能为空!","提示",JOptionPane.PLAIN_MESSAGE);
			}else {
				bookJdbc jdbc=new bookJdbc();
	            
				String notReturn="否";
				String fines="0";
				String returnDate=" ";
				String sql="insert into 借书记录表 (用户名,借出日期,是否归还,归还日期,逾期罚金,图书号) "
						+ " values('"+userNameText.getText().toString()+"','"+borrowDateText
						.getText().toString()+"','"+notReturn+"','"+returnDate+"','"+fines+"','"+bookNumberText.getText().toString()+"')";
				jdbc.addBook(sql);
				jdbc.closeJdbc();
				JOptionPane.showMessageDialog(this, 
						"添加成功","提示",JOptionPane.PLAIN_MESSAGE); 
				dialog.dispose();
						
			}
		}	
		if(e.getSource()==cancelBut) {	
			dialog.dispose();
					
		}
		
	}	
	
}

jdbc连接数据库主要代码:

package bbm.Dao;

import java.awt.Dimension;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.JTable;

public class bookJdbc {

	//数据库部分
		PreparedStatement ps=null;
		Connection ct=null;
		ResultSet rs=null;
		String url="jdbc:sqlserver://localhost:1433;databaseName=BookManagement";
	    String user="sa";                  //sa超级管理员
	    String password="a869355161";    //密码
	    
	    public bookJdbc() {
	    	
	    	  try {
  				Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  				ct=DriverManager.getConnection(url,user,password);  						
  			
  			}catch(Exception es) {
  				 es.printStackTrace();
  			}
	    	       
  		}
	    	
	    public void addBook(String sql) {
			try {
				ps=ct.prepareStatement(sql);
				rs=ps.executeQuery();
				//executeUpdate();
			}catch(SQLException e) {
				e.printStackTrace();
	        }
	   }
	    
	   public void deleteBook(String sql){  
	        try {  
	        	ps=ct.prepareStatement(sql);
				rs=ps.executeQuery();
	        } catch (SQLException e1) {  
	            e1.printStackTrace();  
	        }  
	   }
	   //修改书籍
	   public void modifyBook(String sql) {
		   try { 
			   ps=ct.prepareStatement(sql);
			   rs=ps.executeQuery();
	        } catch (SQLException e1) {  
	            e1.printStackTrace();  
	        }  
	   }
	   //搜索String型
	   public String StrResearch(String sql) {
		   String s = null;
		   try { 
			   ps=ct.prepareStatement(sql);
			   rs=ps.executeQuery();
			   while(rs.next()) {
				   s=rs.getString(1);
			   }		   
			   System.out.println(s);
			   
	        } catch (SQLException e1) {  
	            e1.printStackTrace();  
	        }
		   return s;
		
	   }
	   
	   //搜索int型
	   public int IntResearch(String sql) {
		   int s = 0;
		   try { 
			   ps=ct.prepareStatement(sql);
			   rs=ps.executeQuery();
			   while(rs.next()) {
				   s=rs.getInt(1);
			   }		   
			   System.out.println(s);
			   
	        } catch (SQLException e1) {  
	            e1.printStackTrace();  
	        }
		   return s;
		
	   }
	
       //关闭数据库
	   public void closeJdbc() {
		// TODO Auto-generated method stub
		  try {
	    	  if(rs!=null){  
                   rs.close();  
                  }  
                  if(ps!=null){  
                      ps.close();  
                  }  
                  if(ct!=null){  
                      ct.close();
	              }
	      }catch(Exception es) {
	    	 es.printStackTrace(); 
	    }	
	   }
	   
}

sql server数据库表内容
Java图书管理系统,java课程实习_第5张图片

Java图书管理系统,java课程实习_第6张图片

Java图书管理系统,java课程实习_第7张图片

Java图书管理系统,java课程实习_第8张图片

你可能感兴趣的:(Java)