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

📄 bookstore.java

📁 一个学习java数据库连接的好例子——在线书店的管理。
💻 JAVA
字号:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;

public class BookStore{
	public static void main(String[] args){
		UI ui = new UI();
		ui.initial();
	}
}

class UI implements ActionListener{
	private Frame fm;
	private Panel pm;
	private Vector books;
	private int[] pq;
	private int idx;
	private Checkbox[] cb;
	private TextField[] tfpq;
	private Button[] bnote;
	private Button clear;
	private Button prev;
	private Button next;
	private Button jump;
	private Button review;
	private Button submit;
	private Button ext;
	private Dialog rew;
	private Dialog dnote;
	private TextArea revTa;
	private TextField toPage=new TextField(4);
	private String dbServer="127.0.0.1";
	private Connection conn;
	private Statement stmt;
	private NumberFormat nf1,nf2;
	
	public void initial(){
		this.connectDB();
		this.readDB();
		
		pm=new Panel();
		pm.setLayout(new GridLayout(16,1,1,1));
		pm.setBackground(new Color(95,145,145));
		
		Panel p =new Panel();
		p.setBackground(new Color(95,145,145));
		p.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));
		
		clear=new Button("Clear All");
		clear.setActionCommand("Clear");
		clear.addActionListener(this);
		
		prev=new Button("Last Page");
		prev.setActionCommand("Prev");
		prev.addActionListener(this);
		
		next=new Button("Next Page");
		next.setActionCommand("Next");
		next.addActionListener(this);
		
		jump=new Button("Turn to page");
		jump.setActionCommand("GoTo");
		jump.addActionListener(this);
		
		review=new Button("Selected book list");
		review.setActionCommand("Review");
		review.addActionListener(this);
		
		submit=new Button("Submit");
		submit.setActionCommand("Submit");
		submit.addActionListener(this);
		
		ext=new Button("Exit");
		ext.setActionCommand("Exit");
		ext.addActionListener(this);
		
		p.add(clear);
		p.add(prev);
		p.add(next);
		p.add(jump);
		p.add(toPage);
		p.add(review);
		p.add(submit);
		p.add(ext);
		
		nf1=NumberFormat.getInstance();
		nf2=NumberFormat.getInstance();
		nf1.setMinimumIntegerDigits(3);
		nf2.setMinimumFractionDigits(2);
		
		fm=new Frame("Basterm Bookstore");
		fm.setLayout(new BorderLayout(1,1));
		fm.add(pm,BorderLayout.CENTER);
		fm.add(p,BorderLayout.SOUTH);
		fm.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.exit(1);
				}
			});
		fm.setSize(750,390);
		fm.setVisible(true);
		idx=0;
		this.setButtonState();
		this.showPage(idx);
	}
	
	public void connectDB(){
		String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
		String sConnStr="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=D:/javasource/BookStore.mdb";
		try{
			Class.forName(sDBDriver);
			conn=DriverManager.getConnection(sConnStr,"","");
			stmt=conn.createStatement();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void closeDB(){
		try{
			conn.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	public void exit(){
		this.closeDB();
		System.exit(1);
	}
	
	public void readDB(){
		books=new Vector();
		Book b;
		try{
			ResultSet rs=stmt.executeQuery("select * from bstore");
			while(rs.next()){
				b=new Book(
					rs.getString(1).trim(),
					rs.getString(2).trim(),
					rs.getFloat(3),
					rs.getString(4).trim(),
					rs.getString(5).trim(),
					rs.getString(6).trim(),
					rs.getInt(7)
				);
				books.add(b);
			}
			pq=new int[books.size()];
			cb=new Checkbox[books.size()];
			bnote=new Button[books.size()];
			tfpq=new TextField[books.size()];
			for(int i=0;i<books.size();i++){
				cb[i]=new Checkbox();
				bnote[i]=new Button("Content Introduce");
				bnote[i].setActionCommand("Note"+i);
				bnote[i].addActionListener(this);
				tfpq[i]=new TextField("",3);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void updateDB(){
		Book b;
		String id;
		String sql;
		try{
			for(int i=0;i<books.size();i++){
				if(cb[i].getState() && pq[i]>0){
					b=(Book)books.elementAt(i);
					id=b.getId();
					int q=b.getQuantity()-pq[i];
					sql="update bstore set quantity = "+ q +" where id="
					    +id+"";
					stmt.executeUpdate(sql);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	
	public void showPage(int page){
		pm.removeAll();
		Panel p;
		TextField t;
		
		p=new Panel();
		p.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		p.add(new Label("Buy"));
		p.add(new Label("No."));
		
		p.add(new Label("                          Book Name                        "));
		p.add(new Label("                Publisher             "));
		p.add(new Label("Price:yuan"));
		p.add(new Label("Buy Number"));
		pm.add(p);
		
		for(int i=page*15;i<(page+1)*15;i++){
			if(i>=books.size()) break;
			p=new Panel();
			p.setLayout(new FlowLayout(FlowLayout.LEFT,1,0));
			p.add(cb[i]);
			
			t=new TextField(nf1.format(i+1)+"",4);
			t.setEditable(false);
			p.add(t);
			
			Book bk=(Book)books.elementAt(i);
			t=new TextField(bk.getName(),30);
			t.setEditable(false);
			p.add(t);
			
			t=new TextField(bk.getPublisher(),20);
			t.setEditable(false);
			p.add(t);
			
			t=new TextField(nf2.format(bk.getPrice())+"",4);
			t.setEditable(false);
			p.add(t);
			if(!cb[i].getState()){
				tfpq[i].setText("");
			}
			p.add(tfpq[i]);
			p.add(bnote[i]);
			
			
			pm.add(p);
		}
		fm.validate();
	}
	
	public void actionPerformed(ActionEvent e){
		String s=e.getActionCommand();
		if(s.equals("Exit")){
			this.exit();
		}else if(s.equals("Review")){
			this.showSelectedBook();
			return;
		}else if(s.equals("Submit")){
			this.purChase();
			return;
		}else if(s.startsWith("Note")){
			s=s.substring(4);
			int i=Integer.parseInt(s);
			this.showNote(i);
			return;
		}
		
		if(s.equals("Next")){
			idx++;
		}else if(s.equals("Prev")){
			idx--;
		}else if(s.equals("Clear")){
			this.reset();
		}else if(s.equals("GoTo")){
			try{
				int i=Integer.parseInt(toPage.getText())-1;
				if(i>=0 && i<=books.size()/15) idx=1;
				
			}catch(Exception e1){
				e1.printStackTrace();
			}
			toPage.setText("");
		}
		
		this.showPage(idx);
		this.setButtonState();
	}
	
	public void reset(){
		for(int i=0;i<=books.size();i++){
			pq[i]=0;
			cb[i].setState(false);
		}
	}
	
	public void createRevInfo(){
		Book b;
		revTa=new TextArea("",20,20);
		revTa.setEditable(false);
		revTa.setText("No./Book Name /Price(yuan)/Number\n");
		revTa.append("----------------------------\n");
		double total=0.0;
		for(int i=0;i<books.size();i++){
			try{
				pq[i]=Integer.parseInt(tfpq[i].getText());
				
			}catch(Exception e){
				pq[i]=0;
			}
			if(cb[i].getState() && pq[i]>0){
				b=(Book)books.elementAt(i);
				revTa.append(nf1.format(i+1)+"\t"
				  +b.getName()+"\t"
				  +nf2.format(b.getPrice())+"\t"
				  +pq[i]+"\n");
				total +=pq[i] * b.getPrice();
			}
		}
		revTa.append("----------------------------\n");
		revTa.append("Should pay:" + nf2.format(total)+"yuan");
	}
	
	public void purChase(){
		if(rew!=null){
			rew.dispose();
		}
		Label title=new Label("you buy list");
		title.setFont(new Font("Impact",Font.PLAIN,20));
		this.createRevInfo();
		rew=new Dialog(fm,true);
		rew.add(title,"North");
		rew.add(revTa);
		
		Button ok=new Button("Ok");
		ok.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				rew.dispose();
				updateDB();
				//reset();
				showPage(idx);
			}
		});
		
		Button cancel=new Button("cancel");
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				rew.dispose();
			}
		});
		Panel p=new Panel();
		p.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));
		p.add(ok);
		p.add(cancel);
		rew.add(p,"South");
		rew.setSize(350,200);
		rew.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				rew.dispose();
			}
		});
		rew.setVisible(true);
	}
	
	public void showNote(int bIndex){
		if(dnote!=null){
			dnote.dispose();
			
		}
		dnote=new Dialog(fm);
		Book b=(Book)books.elementAt(bIndex);
		Label title=new Label("Book Introduce");
		title.setFont(new Font("Impact",Font.PLAIN,20));
		dnote.add(title,"North");
		
		TextArea noteTa=new TextArea("",20,20,1);
		noteTa.setEditable(false);
		noteTa.append("No.:"+(bIndex+1)
		              +"\nBook Name:"+b.getName()
		              +"\nPrice:    "+b.getPrice()
		              +"\nAuthor:   "+b.getAuthor()
		              +"\nIntroduce:"+b.getNote());
		dnote.add(noteTa);
		Button ok=new Button("OK");
		
		dnote.resize(300,200);
		dnote.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				dnote.dispose();
			}
		});
		dnote.show();
	}
	
	public void showSelectedBook(){
		if(rew!=null){
			rew.dispose();
			
		}
		Label title=new Label("You has Choose:");
		title.setFont(new Font("Impact",Font.PLAIN,20));
		this.createRevInfo();
		rew=new Dialog(fm);
		rew.add(title,"North");
		rew.add(revTa);
		
		Button ok=new Button("OK");
		ok.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				rew.dispose();
			}
		});
		
		Panel p=new Panel();
		p.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));
		p.add(ok);
		rew.add(p,"South");
		rew.setSize(300,200);
		rew.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				rew.dispose();
			}
		});
		rew.show();
		
	}
	
	public void setButtonState(){
		if(idx==0){
			prev.setEnabled(false);
		}else{
			prev.setEnabled(true);
		}
		
		if(idx==books.size()/15){
			next.setEnabled(false);
		}else{
			next.setEnabled(true);
		}
	}
	
}

class Book{
	private String id;
	private String name;
	private double price;
	private String author;
	private String publisher;
	private String note;
	private int quantity;
	
	public Book(String id,String name,double price,String author,
				String publisher,String note,int quantity){
		this.id=id;
		this.name=name;
		this.price=price;
		this.author=author;
		this.publisher=publisher;
		this.note=note;
		this.quantity=quantity;			
	}
	
	public void setId(String id){
		this.id=id;
	}
	
	public String getId(){
		return id;
	}
	
	public void setName(String name){
		this.name=name;
	} 
	
	public String getName(){
		return name;
	}
	
	public void setPrice(double price){
		this.price=price;
		
	}
	
	public double getPrice(){
		return price;
	}
	
	public void setAuthor(String author){
		this.author=author;
	}
	
	public String getAuthor(){
		return author;
	}
	
	public void setPublisher(String publisher){
		this.publisher=publisher;
	}
	
	public String getPublisher(){
		return publisher;
	}
	
	public void setNote(String note){
		this.note=note;
	}
	
	public String getNote(){
		return note;
	}
	
	public void setQuantity(int quantiry){
		this.quantity=quantity;
	}
	
	public int getQuantity(){
		return 	quantity;
	}
	
}

⌨️ 快捷键说明

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