第一个JAVA项目—————图书管理系统

暑假期间学习了一些JAVA的基础知识,为了验证自己的所学,写了这个简单的图书管理系统,本来想写完就发到博客上但一直没想起来就拖了很久,最近想起来还没发然后想发一下,本系统实现了登陆注册功能以及图书的增删改查功能,运用JDBC连接数据库,简单运用了一些四则运算控制了账号和密码的格式,修改账号信息功能,写的很水,以后有空会重新写一下。

 

数据库表

 

 

各个界面的实现

登陆:

注册:

主界面:

 

 

图书借阅:(实现模糊查询功能,分类查找功能)

 

借阅记录:

修改账户信息:

 

写这个系统的时候写着写着不太想写了,然后就把修改图书信息直接写到普通账号里了。(本人比较懒)

 

下面是一些功能实现的代码:

 

登陆窗口:

package conn.java123.Fram;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener;


import conn.java123.Dao.UserDao;
import conn.java123.Dao.UserModel;
import conn.java123.Util.DbUtil;

public class SignFrame {
	UserModel user;
	
	

	public static void main(String[] args) {
		SignFrame s = new SignFrame();
		
		s.show();
	}
	
	public void show() {
		JFrame frame = new JFrame("图书管理系统");
		frame.setDefaultCloseOperation(3);
		frame.setBounds(500, 150, 500, 600);
		frame.setVisible(true);
		frame.setLayout(new FlowLayout());
		
		JLabel label1 = new JLabel("账号:");
		JTextField text1 = new JTextField(40);
		
		JLabel label2 = new JLabel("密码:");
		JPasswordField text2 = new JPasswordField(40);
		JLabel l = new JLabel();
		l.setIcon(new ImageIcon("C:\\Users\\sd-acm\\Desktop\\sign.jpg"));
		frame.add(l);
		frame.add(label1);
		frame.add(text1);
		frame.add(label2);
		frame.add(text2);
		
		JButton button1 = new JButton("登录");
		frame.add(button1);
		button1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String account = text1.getText().trim();
				String password = text2.getText().trim();
				
				try {
					DbUtil.getCon();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
				user = new UserModel();
				user.setAccount(account);
				user.setPassword(password);
				
				ModAccountFace.setAccount(account);
				
				try {
					ResultSet rs = UserDao.findUser(user);
					
					if(rs != null) {
						JLabel label1 = new JLabel();
						JOptionPane.showMessageDialog(label1,"登录成功" );
						while(rs.next()) {
							FindBookFace.setUser(rs.getString(3));
						}
						frame.dispose();
						new MainFrame();
					}else {
						JLabel label = new JLabel();
						JOptionPane.showMessageDialog(label,"用户不存在或账号密码有误" );
					}
					
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				try {
					DbUtil.getConnection();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		JButton button2 = new JButton("注册");
		frame.add(button2);
		button2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				RegisFrame regis = new RegisFrame();
				frame.dispose();
			}
		});
		
		frame.setResizable(false);
	}
}

 

注册实现:

package conn.java123.Fram;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.concurrent.ConcurrentHashMap;

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

import com.mysql.jdbc.PreparedStatement;

import conn.java123.Dao.UserDao;
import conn.java123.Dao.UserModel;
import conn.java123.Util.DbUtil;



public class RegisFrame extends JFrame {
	
	public RegisFrame() {
		setTitle("图书管理系统");
		setDefaultCloseOperation(3);
		setBounds(500, 150, 500, 600);
		setVisible(true);
		setLayout(new FlowLayout());
		setResizable(false);
		
		init();
	}
	
	public void init() {
		JLabel l = new JLabel();
		l.setIcon(new ImageIcon("C:\\Users\\sd-acm\\Desktop\\sign.jpg"));
		l.setBounds(0, 0, 100, 100);
		
		JLabel label1 = new JLabel("账号:      ");
		JTextField text1 = new JTextField(40);
		
		JLabel label2 = new JLabel("学号:      ");
		JTextField text2 = new JTextField(40);
		
		JLabel label3 = new JLabel("用户名:   ");
		JTextField text3 = new JTextField(40);
		
		JLabel label4 = new JLabel("密码:  	    ");
		JPasswordField text4 = new JPasswordField(40);
		
		JLabel label5 = new JLabel("重复密码:");
		JPasswordField text5 = new JPasswordField(40);
		
		this.add(l);
		this.add(label1);
		this.add(text1);
		this.add(label2);
		this.add(text2);
		this.add(label3);
		this.add(text3);
		this.add(label4);
		this.add(text4);
		this.add(label5);
		this.add(text5);
		
		JButton button1 = new JButton("注册");
		add(button1);
		button1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String account = text1.getText().trim();
				String studentId = text2.getText().trim();
				String username = text3.getText().trim();
				String password = text4.getText().trim();
				String repeatpassword = text5.getText().trim();
				
				String accountregex = "[0-9] {6,10}";
				String studentIdregex = "[0-9] {12}";
				String usernameregex = "[a-zA-Z] {5}";
				String passwordregex = "\\w{6,12}";
				
				if(account.matches(accountregex)) {
					System.out.println(account);
					JOptionPane.showMessageDialog(text1, "账号不满足由0~10个数字组成");
					text1.setText("");
					text1.requestFocus();
					return;
				}
				
				if(studentId.matches(studentIdregex)) {
					JOptionPane.showMessageDialog(text2, "学号不满足由12位数字组成");
					text2.setText("");
					text2.requestFocus();
					return;
				}
				
				if(username.matches(usernameregex)) {
					JOptionPane.showMessageDialog(text3, "用户名不满足5个英文字母组成");
					text3.setText("");
					text3.requestFocus();
					return;
				}
				
				if(!password.matches(passwordregex)) {
					JOptionPane.showMessageDialog(text4, "密码不满足由6~12个任意单词字符组成");
					text4.setText("");
					text4.requestFocus();
					return;
				}
				
				if(!repeatpassword.equals(password)) {
					JOptionPane.showMessageDialog(text5, "两次输入的密码不一致");
					text5.setText("");
					text5.requestFocus();
					return;
				}
				
				if(true) {
					try {
						DbUtil.getCon();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					UserModel user = new UserModel();
					user.setAccount(account);
					user.setPassword(password);
					user.setStudentId(studentId);
					user.setUsername(username);
					
					try {
						UserDao.addUser(user);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
					try {
						DbUtil.getConnection();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
					
					JLabel l1 = new JLabel();
					JOptionPane.showInternalMessageDialog(l1, "注册成功");
					SignFrame sign = new SignFrame();
					sign.show();
					dispose();
				}
				
			}
		});
		
		JButton button2 = new JButton("重置");
		add(button2);
		button2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				text1.setText("");
				text2.setText("");
				text3.setText("");
				text4.setText("");
				text5.setText("");
			}
		});
		
		JButton button3 = new JButton("取消");
		add(button3);
		button3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				SignFrame frame = new SignFrame();
				frame.show();
				dispose();
			}
		});
		
		

	}
}

数据库连接:

package conn.java123.Util;

import java.sql.Connection;
import java.sql.DriverManager;



public class DbUtil {
	private static final String URL = "jdbc:mysql://localhost:3306/book?useSSL=true&characterEncoding=GBK";
	private static final String Name = "root";
	private static final String Password = "root";
	private static final String jdbcName = "java.sql.Driver";
	
	public static Connection getCon() throws Exception {
		Class.forName(jdbcName);
		Connection con = (Connection)DriverManager.getConnection(URL,Name,Password);
		return con;
	}
	
	public void closeCon(java.sql.Connection con) throws Exception {
		if(con != null) {
			con.close();
		}
	}
	
//	public static void main(String[] args) {
//		DbUtil d = new DbUtil();
//		try {
//			d.getCon();
//			System.out.println("数据库连接成功");
//		}catch(Exception e) {
//			e.printStackTrace();
//			System.out.println("数据库连接失败");
//		}
//	}

	public static Connection getConnection() throws Exception {
		// TODO Auto-generated method stub
		return getCon();
	}

}

表格构建:

package conn.java123.Putin;

import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;

import com.mysql.jdbc.ResultSetMetaData;

import conn.java123.Dao.BookDao;
import conn.java123.Util.DbUtil;

public class PutinStorageBook {
	public static Vector getRows() throws Exception {
		DbUtil.getCon();
		
		Vector rows = null;
		Vector columnHeads = null;
		ResultSet rs = BookDao.findBook();
		
		if(rs.wasNull())
			JOptionPane.showMessageDialog(null, "结果集中无记录");
		
		rows = new Vector();
		ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
		
		while(rs.next()) {
			rows.addElement(getNextRow(rs,rsmd));
		}
		
		return rows;
	}
	
	public static Vector getHead() throws Exception {
		DbUtil.getCon();
		
		Vector columnHeads = null;
		
		ResultSet rs = BookDao.findBook();
		boolean moreRecords = rs.next();
		
		if(!moreRecords)
			JOptionPane.showMessageDialog(null, "结果集中无记录");
		
		columnHeads = new Vector();
		
		ResultSetMetaData rsmd = (ResultSetMetaData)rs.getMetaData();
		for(int i = 1;i<=rsmd.getColumnCount();i ++) {
			columnHeads.addElement(rsmd.getColumnName(i));
		}
		
		return columnHeads;
	}
	
	public static Vector getNextRow(ResultSet rs,ResultSetMetaData rsmd) throws Exception {
		Vector currentRow = new Vector();
		
		for(int i = 1;i<=rsmd.getColumnCount();i ++) {
			currentRow.addElement(rs.getString(i));
		}
		
		return currentRow;
	}
}

图书信息查找:

package conn.java123.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.sun.net.httpserver.Authenticator.Result;

import conn.java123.Model.BookModel;
import conn.java123.Util.DbUtil;

public class FindBookDao {
	public static ResultSet findBook(BookModel book) throws Exception {
		Connection con = DbUtil.getConnection();
		//String sql = "select * from book_in where book_name ='"+book.getBook_name()+"' ";
		String sql = " select * from book_in where book_name like '%"+book.getBook_name()+"%'";
		PreparedStatement pstmt = con.prepareStatement(sql);
		
		ResultSet rs = pstmt.executeQuery();
		
		return rs;
	}
	
	public static ResultSet findAuther(BookModel book) throws Exception {
		Connection con = DbUtil.getConnection();
		String sql = " select * from book_in where book_auther like '%"+book.getBook_auther()+"%'";
		PreparedStatement pstmt = con.prepareStatement(sql);
		
		ResultSet rs = pstmt.executeQuery();
		
		return rs;
	}
	
	public static ResultSet findCategory(BookModel book) throws Exception {
		Connection con = DbUtil.getConnection();
		String sql = " select * from book_in where book_category like '%"+book.getBook_category()+"%'";
		PreparedStatement pstmt = con.prepareStatement(sql);
		
		ResultSet rs = pstmt.executeQuery();
		
		return rs;
	}
}

主界面实现:

package conn.java123.Fram;

import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Image;
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.SQLException;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import conn.java123.Putin.PutinStorageBook;

public class AllBookFace extends JPanel {
	private JLayeredPane panel;
	private JPanel p = new JPanel(null);
	DefaultTableModel tableModel;
	JTable table;
	JButton add,del,exit,save;
	
	public AllBookFace() throws Exception {
		setLayout(new GridLayout(1,1));
		
		panel = new JLayeredPane() {
			public void paintComponent(Graphics g) {
				super.paintComponent(g);
				ImageIcon img = new ImageIcon(panel.getClass().getResource("/regis.jpg"));
				img.setImage(img.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_AREA_AVERAGING));
				g.drawImage(img.getImage(), 0, 0, this);
			}
		};
		
		init();
		panel.add(p);
		add(panel);
	}
	
	public void init() throws Exception {
		
		add = new JButton("增加");
		del = new JButton("删除");
		save = new JButton("保存");
		exit = new JButton("退出");
		
		add.setBounds(400, 50, 80, 50);
		del.setBounds(600, 50, 80, 50);
		save.setBounds(800, 50, 80, 50);
		exit.setBounds(1000, 50, 80, 50);
		
		p.add(add);
		p.add(del);
		p.add(exit);
		p.add(save);
		
		
		Vector rowData = PutinStorageBook.getRows();
		Vector columnNames = PutinStorageBook.getHead();
		
		tableModel = new DefaultTableModel(rowData,columnNames);
		tableModel.setColumnIdentifiers(new String[] {"ID","ISBN","图书名称","图书价格","图书作者","出版社","分类号","图书数量"});
		table = new JTable(tableModel);
		
		JScrollPane s = new JScrollPane(table);
		
		s.setBounds(250,100, 1000, 600);
		
		p.add(s);
		
		MyEvent();
		
		p.setOpaque(false);
		p.setBounds(0, 0, 1500, 1500);
	}
	
	public void MyEvent() {
		add.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				tableModel.addRow(new Vector());
			}
		});
		
		del.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int rowcount = table.getSelectedRow();//返回第一个选定行的索引,如果没有则返回-1
				if(rowcount >= 0) {
					tableModel.removeRow(rowcount);//从模型中删除第rowcount行
				}
			}
		});
		
		save.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int column = table.getColumnCount();
				int row = table.getRowCount();
//				System.out.println(row);
//				System.out.println(column);
				
				
				String[][] value = new String[row][column];
				
				for(int i = 0;i<row;i ++) {
					for(int j = 0;j<column;j ++) {
						if(table.getValueAt(i,j)!=null&&!table.getValueAt(i,j).toString().equals("")) {
							value[i][j] = table.getValueAt(i,j).toString();
//							System.out.println(value[i][j]);
						}
						
					}
				}
				
				final String URL = "jdbc:mysql://localhost:3306/book?useSSL=true&characterEncoding=utf-8";
				final String Name = "root";
				final String Password = "root";
				final String jdbcName = "java.sql.Driver";
				
				
				
				try {
					Class.forName(jdbcName);
					Connection con = (Connection)DriverManager.getConnection(URL,Name,Password);
					PreparedStatement pstmt = con.prepareStatement("delete from book_in where true");
					pstmt.executeUpdate();
					
					for(int i = 0;i<row;i ++) {
						//System.out.println(Integer.parseInt(value[i][0]));
						//System.out.println(value[i][1]);
						pstmt = con.prepareStatement("insert into book_in values(" + Integer.parseInt(value[i][0]) + ",'" + value[i][1] + "','" + value[i][2] + "','" + value[i][3] + "','" + value[i][4] + "','" + value[i][5] + "','" + value[i][6] + "','" + value[i][7] + "')");
						pstmt.executeUpdate();
					}
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
				
			}
		});
		
		exit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
	}
}

以上是部分代码的实现,希望能与各位博友互相借鉴。需要完整代码可以私信。

 

 


版权声明:本文为y1356998843原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。