Java实现简单的图书管理系统

最近在学习Java网络编程,写了一个图书管理系统练手。使用文件的输入和输出类实现对书记数据的保存和读取。

FileInputStream和FileOutputStream类为建立一个与文件相关的输入与输出流,提供从文件中读取或者写入一个字节或一组数据的方法。而ObjectOutputStream是用来输入对象。最后程序还有一点小问题,加入书籍会覆盖以前加入的书籍数据,之后有时间再修改。

package test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Jmain {

	/**
	 * @param args
	 */
	static int a;

	static FileOutputStream nos=null;
	static FileInputStream nis=null;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			nos=new FileOutputStream("savenum.txt",true);
			nis=new FileInputStream("savenum.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		m();
	}
	public static void m(){
		
		Scanner snr = new Scanner(System.in); 
		System.out.println("选择功能");
		System.out.println("1 加入书籍");
		System.out.println("2 查找书籍");
		System.out.println("3 退出");
		a=snr.nextInt();
		d(a);
	}
	public static void d(int a){
		int i=0,n,isbn,num=0;
		Scanner sn = new Scanner(System.in); 
		switch(a){
		case 1:{
			System.out.println("输入需要加入的数量");
			i=sn.nextInt();
			try {
				nos.write(i);
			} catch (IOException e) {
				e.printStackTrace();
			}
			inputbook.newb();
			for(n=0;n<i;n++){
				inputbook.input();
			}
			m();
		}
		case 2:{
			try {
				num=nis.read();
				while(num==-1){
				num+=nis.read();}
			} catch (IOException e) {
				e.printStackTrace();
			}
			//System.out.println(num);
			System.out.println("输入isbn");
			isbn=sn.nextInt();
			findBook.fba();
//			System.out.println(num);
			for(n=0;n<num;n++){
			findBook.fb(isbn);
			}
			m();
		}
		case 3:{
			break;
		}
		}
		
	}
}
package test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class findBook {
	static ObjectInputStream ois;
	public static void fba(){
		try {
			ois=new ObjectInputStream(new FileInputStream("save.txt"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void fb(int isbn){
		
		inputbook book=new inputbook() ;
	try {
		book=(inputbook) ois.readObject();
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (ClassNotFoundException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
		if(isbn==book.isbn)
		{
			System.out.println("ISBN:");
			System.out.println(book.isbn);
			System.out.println("价格:");
			System.out.println(book.price);
			System.out.println("书名:");
			System.out.println(book.name);
		}
		else
			System.out.println("未找到记录");
	
	}
}

package test;
import java.io.*;
import java.util.Scanner;
public class inputbook implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int isbn;
	int price;
	String name;
	public int getIsbn() {
		return isbn;
	}
	public void setIsbn(int isbn) {
		this.isbn = isbn;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	static ObjectOutputStream fos;
	static int a=1;
	public static  void newb(){
		
			try {
				fos=new ObjectOutputStream(new FileOutputStream("save.txt"));
				a=2;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	public static void input(){
		int i,p;
		String n;
//		System.out.println(a);
		Scanner scanner = new Scanner(System.in); 
		inputbook book=new inputbook() ;
		System.out.println("请输入ISBN:");
		i=scanner.nextInt();
//		System.out.println(i);
		book.setIsbn(i);
		System.out.println("请输入书名:");
		n=scanner.nextLine();
		n=scanner.nextLine();
		book.setName(n);
		System.out.println("请输入价格:");
		p=scanner.nextInt();
		book.setPrice(p);
		try {
			fos.writeObject(book);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
}



  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; import javax.swing.event.*; import javax.swing.border.*; import java.sql.*; import java.io.*; import java.net.*; import javax.swing.*; public class Java{ public static void main(String args[]){ One one=new One(); } } class One extends JFrame implements ActionListener{ JLabel jlabel1,jlabel2; JButton jbutton1,jbutton2; JTextField jtextfield1; JPanel jpanel1; JPasswordField passwordText; One(){ jpanel1=new JPanel(); jlabel1=new JLabel("用户名:"); jlabel2=new JLabel("密 码:"); jbutton1=new JButton("登录"); jbutton2=new JButton("取消"); Font f=new Font("楷体_GB2312",Font.BOLD,22); jlabel1.setFont(f); jlabel2.setFont(f); jtextfield1=new JTextField(10); passwordText=new JPasswordField(10); passwordText.setEchoChar('*'); jbutton1.addActionListener(this); jbutton2.addActionListener(this); setLayout(new FlowLayout()); jpanel1.add(jbutton1); jpanel1.add(jbutton2); add(jlabel1); add(jtextfield1); add(jlabel2); add(passwordText); add(jpanel1); setBounds(100,100,200,200); setVisible(true); validate(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if(e.getSource()==jbutton1){ String str1,str2; str1=jtextfield1.getText().trim(); str2=passwordText.getText(); Connection con; Statement sql; ResultSet rs; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException ee){ System.out.println(""+ee); } try{ con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(); rs=sql.executeQuery("SELECT * FROM login"); boolean boo=true; while(rs.next()){ if(rs.getString(1).equals(str1) && rs.getString(2).equals(str2)){ JOptionPane.showMessageDialog(this,"欢迎进入图书管理系统","login",JOptionPane.PLAIN_MESSAGE); setVisible(false); FrameMain t=new FrameMain(); boo=false; } } if(boo){ JOptionPane.showMessageDialog(this,"用户不存在或者输入错误请重新输入!","提示",JOptionPane.WARNING_MESSAGE); jtextfield1.setText(""); passwordText.setText(""); } con.close(); }catch(SQLException eee){System.out.println(eee);} } else{ System.exit(0); } } } class FrameMain extends JFrame implements ActionListener{ JPanel contentPane; JMenuBar mb1=new JMenuBar(); JMenu readr=new JMenu("读者"); JMenu book=new JMenu("图书"); JMenu help=new JMenu("帮助"); JMenu exit=new JMenu("退出"); JMenuItem r1=new JMenuItem("浏览读者信息"); JMenuItem b1=new JMenuItem("浏览图书信息"); JMenuItem b2=new JMenuItem("增加图书"); JMenuItem b3=new JMenuItem("删除图书"); JMenuItem b4=new JMenuItem("修改图书信息"); JMenuItem h1=new JMenuItem("使用网络信息帮助"); JMenuItem h2=new JMenuItem("读取java文件帮助信息"); JMenuItem e1=new JMenuItem("退出系统"); FrameMain(){ setTitle("图书馆信息管理系统"); readr.add(r1); book.add(b1); book.add(b2); book.add(b3); book.add(b4); help.add(h1); help.add(h2); exit.add(e1); mb1.add(readr); mb1.add(book); mb1.add(help); mb1.add(exit); setJMenuBar(mb1); r1.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); h1.addActionListener(this); h2.addActionListener(this); e1.addActionListener(this); setBounds(200,200,500,500); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if(e.getSource()==r1){ new showReader(); } else if(e.getSource()==b1){ new showBook(); } else if(e.getSource()==b2){ new InsertBook(); } else if(e.getSource()==b3){ new Delete(); } else if(e.getSource()==b4){ new Modify(); } else if(e.getSource()==h1){ Help1 flink=new Help1(); } else if(e.getSource()==h2){ class Help2 extends JFrame { Help2(){ byte b[]=new byte[30]; JTextArea text=new JTextArea(20,20); setSize(330,300); setVisible(true); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); add(text,BorderLayout.CENTER); try{ FileInputStream input=new FileInputStream("help.java"); ProgressMonitorInputStream in=new ProgressMonitorInputStream(this,"读取help文件",input); ProgressMonitor p=in.getProgressMonitor(); while(in.read(b)!=-1){ String s=new String(b); text.append(s); Thread.sleep(200); } }catch(InterruptedException e){} catch(IOException e){} } } new Help2(); } else if(e.getSource()==e1){ System.exit(0); } } } class Help1 extends JFrame implements ActionListener,Runnable{ JButton button; URL url; JTextField text; JEditorPane editPane; byte b[]=new byte[118]; Thread thread; public Help1(){ text=new JTextField(20); editPane=new JEditorPane(); editPane.setEditable(false); button=new JButton("确定"); button.addActionListener(this); thread=new Thread(this); JPanel p=new JPanel(); p.add(new JLabel("输入网址: ")); p.add(text); p.add(button); Container con=getContentPane(); con.add(new JScrollPane(editPane),BorderLayout.CENTER); con.add(p,BorderLayout.NORTH); setBounds(60,60,500,500); setVisible(true); validate(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); editPane.addHyperlinkListener(new HyperlinkListener (){ public void hyperlinkUpdate(HyperlinkEvent e){ if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){ try{ editPane.setPage(e.getURL()); }catch(IOException e1){ editPane.setText(""+e1); } } } }); } public void actionPerformed(ActionEvent e){ if(!(thread.isAlive())) thread=new Thread(this); try{ thread.start(); }catch(Exception ee){ text.setText("我正在读取"+url); } } public void run(){ try{ int n=-1; editPane.setText(null); url=new URL(text.getText().trim()); editPane.setPage(url); }catch(MalformedURLException e1){ text.setText(""+e1); return; }catch(IOException eee){ text.setText(""+eee); return; } } } class InsertBook extends JDialog implements ActionListener{ JLabel insertBook; Object name[]={"编号","书名","出版社","出版日期"}; Object a[][]=new Object[1][4]; JTable table; JButton insert; Connection con; Statement sql; ResultSet rs; InsertBook(){ insertBook=new JLabel("请输入新书信息:"); table=new JTable(a,name); insert=new JButton("确定插入"); setLayout(new FlowLayout()); add(insertBook); add(table); add(insert); setBounds(100,100,800,200); setVisible(true); insert.addActionListener(this); } public void actionPerformed(ActionEvent e){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException ee){ System.out.println(""+ee); } try{ con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(); int k=sql.executeUpdate("INSERT INTO bookinformation VALUES('"+a[0][0]+"','"+a[0][1]+"','"+a[0][2]+"','"+a[0][3]+"')"); if(k==1) JOptionPane.showMessageDialog(this,"插入成功","成功",JOptionPane.PLAIN_MESSAGE); setVisible(false); con.close(); } catch(SQLException ee){ JOptionPane.showMessageDialog(this,"插入失败","失败",JOptionPane.ERROR_MESSAGE); } } } class showReader extends JDialog implements ActionListener{ JTable table; Object a[][]; Object name[]={"学号","姓名","年龄","出生日期"} ; JButton showReader; showReader(){ showReader=new JButton("显示信息"); showReader.addActionListener(this); add(showReader,BorderLayout.NORTH); setBounds(200,200,500,500); setVisible(true); } public void actionPerformed(ActionEvent e){ Connection con; Statement sql; ResultSet rs; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException ee){ System.out.println(""+ee); } try{ con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=sql.executeQuery("SELECT * FROM readerinformation"); rs.last(); int lastNumber=rs.getRow(); a=new Object[lastNumber][4]; int k=0; rs.beforeFirst(); while(rs.next()){ a[k][0]=rs.getString(1); a[k][1]=rs.getString(2); a[k][2]=rs.getString(3); a[k][3]=rs.getString(4); k++; } con.close(); }catch(SQLException ee){ System.out.println(ee); } table=new JTable(a,name); getContentPane().removeAll(); add(showReader,BorderLayout.NORTH); add(new JScrollPane(table),BorderLayout.CENTER); setVisible(true); validate(); } } class showBook extends JDialog implements ItemListener{ JTable table; Object a[][]; Object name[]={"编号","书名","出版社","出版日期"} ; JCheckBox showBook; showBook(){ showBook=new JCheckBox("显示信息"); showBook.addItemListener(this); add(showBook,BorderLayout.NORTH); setBounds(200,200,500,500); setVisible(true); } public void itemStateChanged(ItemEvent e){ JCheckBox box=(JCheckBox)e.getItemSelectable(); if(box.isSelected()){ Connection con; Statement sql; ResultSet rs; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException ee){ System.out.println(""+ee); } try{ con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=sql.executeQuery("SELECT * FROM bookinformation"); rs.last(); int lastNumber=rs.getRow(); a=new Object[lastNumber][4]; int k=0; rs.beforeFirst(); while(rs.next()){ a[k][0]=rs.getString(1); a[k][1]=rs.getString(2); a[k][2]=rs.getString(3); a[k][3]=rs.getString(4); k++; } con.close(); }catch(SQLException ee){ System.out.println(ee); } table=new JTable(a,name); getContentPane().removeAll(); add(showBook,BorderLayout.NORTH); add(new JScrollPane(table),BorderLayout.CENTER); setVisible(true); validate(); } else table.setVisible(false); } } class Delete extends JDialog implements ActionListener{ JLabel input; JTextField inputNumber; Object name[]={"编号","书名","出版社","出版日期"} ; Object a[][]=new Object[1][4]; JTable table; JButton delete; JPanel pane=new JPanel(); Connection con; Statement sql; ResultSet rs; String num; Delete(){ input=new JLabel("请输入删除书的编号:(以回车结束)"); inputNumber=new JTextField(20); delete=new JButton("删除"); table=new JTable(a,name); pane.add(input); pane.add(inputNumber); add(pane,BorderLayout.NORTH); add(table,BorderLayout.CENTER); add(delete,BorderLayout.SOUTH); inputNumber.addActionListener(this); delete.addActionListener(this); setBounds(100,100,800,300); setVisible(true); } public void actionPerformed(ActionEvent e){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException eee){ System.out.println(""+eee); } try{ num=inputNumber.getText().trim(); con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(); rs=sql.executeQuery("SELECT *FROM bookinformation where 编号='"+num+"'"); boolean boo=rs.next(); if(boo==false){ JOptionPane.showMessageDialog(this,"编号不存在","提示",JOptionPane.WARNING_MESSAGE); } else{ a[0][0]=rs.getString(1); a[0][1]=rs.getString(2); a[0][2]=rs.getString(3); a[0][3]=rs.getDate(4).toString(); table.repaint(); if(e.getSource()==delete){ int n=JOptionPane.showConfirmDialog(this,"确认要删除吗?","确认对话框",JOptionPane.YES_OPTION); if(n==JOptionPane.YES_OPTION){ sql=con.createStatement(); int k=sql.executeUpdate("DELETE FROM bookinformation WHERE 编号='"+num+"'"); if(k==1) { JOptionPane.showMessageDialog(this,"删除成功!","删除",JOptionPane.PLAIN_MESSAGE); setVisible(false); } else { JOptionPane.showMessageDialog(this,"删除失败!","删除",JOptionPane.ERROR_MESSAGE); setVisible(false); } } } } con.close(); }catch(SQLException ee){ System.out.println(ee); } } } class Modify extends JDialog implements ActionListener{ JLabel infor; JTextField inputNumber; Object name[]={"编号","书名","出版社","出版日期"}; Object a[][]=new Object[1][4]; JTable table; JButton queren; Connection con; Statement sql; ResultSet rs; String num; Modify(){ infor=new JLabel("输入编号(以回车确认):"); inputNumber=new JTextField(20); table=new JTable(a,name); queren=new JButton("修改记录"); setLayout(new FlowLayout()); infor.setPreferredSize(new Dimension(100,70)); add(infor); add(inputNumber); add(table); queren.setPreferredSize(new Dimension(80,50)); add(queren); inputNumber.addActionListener(this); queren.addActionListener(this); setBounds(20,60,300,500); setVisible(true); } public void actionPerformed(ActionEvent e){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException ee){ System.out.println(""+ee); } if(e.getSource()==inputNumber){ try{ num=inputNumber.getText().trim(); con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(); rs=sql.executeQuery("SELECT * FROM bookinformation WHERE 编号='"+num+"'"); boolean boo=rs.next(); if(boo==false){ JOptionPane.showMessageDialog(this,"编号不存在","提示",JOptionPane.WARNING_MESSAGE); } else{ a[0][0]=rs.getString(2); a[0][1]=rs.getString(3); a[0][2]=rs.getDate(4).toString(); table.repaint(); } con.close(); } catch(SQLException eee){ System.out.println(eee); } } if(e.getSource()==queren){ try{ table.repaint(); con=DriverManager.getConnection("jdbc:odbc:renshi","",""); sql=con.createStatement(); int k=sql.executeUpdate("UPDATE bookinformation SET 书名='"+a[0][0]+"',出版社='"+a[0][1]+"',出版日期 ='"+a[0][2]+"' WHERE 编号='"+num+"'"); if(k==1){ JOptionPane.showMessageDialog(this,"更新成功","成功",JOptionPane.PLAIN_MESSAGE); con.close(); setVisible(false); } }catch(SQLException eeee){ JOptionPane.showMessageDialog(this,"更新失败"+eeee,"失败",JOptionPane.ERROR_MESSAGE); } } } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值