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

📄 book.java

📁 用java 语言编写的用于小型图书管理的场所的图书管理代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.sql.*;
class bookAdd extends Frame implements 	ActionListener//未连接数据库
{
	Box baseBox,box1,box2;//两个盒式布局对象
	Button button1,button2;
	Label label1,label2,label3,label4,label5,label6,label7,label8,label9,label10;
	TextField text1,text2,text3,text4,text5,text6,text7,text8,text9;
	Choice choice1; 
	TextArea area1;
	Panel panel1;
	bookAdd(String s)
	{
		super(s);
		setLayout(new BorderLayout());//设置布局方式
		panel1=new Panel();
		area1=new TextArea(20,30);
		area1.setBackground(Color.lightGray);
		area1.setForeground(Color.BLACK);
		area1.setEditable(false);
		box1=Box.createVerticalBox();//box1为列支撑方式
		Font f=new Font("",Font.LAYOUT_LEFT_TO_RIGHT,20);
		label1=new Label("ISBN");		label1.setFont(f);/*ISBN图书编号*/
		label2=new Label("图书名称");	label2.setFont(f);
		label3=new Label("图书作者");	label3.setFont(f);
		label4=new Label("出版社名称");	label4.setFont(f);
		label5=new Label("出版社地址");	label5.setFont(f);
		label6=new Label("图书版次");	label6.setFont(f);
		label7=new Label("出版时间");	label7.setFont(f);
		label8=new Label("定价");		label8.setFont(f);
		label9=new Label("数量");		label9.setFont(f);
		label10=new Label("图书类别");	label10.setFont(f);
		
		box1.add(label1);  
		box1.add(Box.createVerticalStrut(8));//列支撑高度8
		box1.add(label2);
		box1.add(Box.createVerticalStrut(8));
		box1.add(label3);
		box1.add(Box.createVerticalStrut(8));
		box1.add(label4);
		box1.add(Box.createVerticalStrut(8));
		box1.add(label5);
		box1.add(Box.createVerticalStrut(8));
		box1.add(label6);
		box1.add(Box.createVerticalStrut(8));
		box1.add(label7);
		box1.add(Box.createVerticalStrut(8));
		box1.add(label8);
		box1.add(Box.createVerticalStrut(8));
		box1.add(label9);
		box1.add(Box.createVerticalStrut(8));
		box1.add(label10);
		box1.setBackground(Color.lightGray);
		box1.setForeground(Color.blue);
		
		box2=Box.createVerticalBox();//box2为行支撑方式
		text1=new TextField(20); text1.setFont(f);
		text2=new TextField(20); text2.setFont(f);
		text3=new TextField(20); text3.setFont(f);
		text4=new TextField(20); text4.setFont(f);
		text5=new TextField(20); text5.setFont(f);
		text6=new TextField(20); text6.setFont(f);
		text7=new TextField(20); text7.setFont(f);
		text8=new TextField(20); text8.setFont(f);
		text9=new TextField(20); text9.setFont(f);
		box2.add(text1);
		box2.add(Box.createVerticalStrut(8));
		box2.add(text2);
		box2.add(Box.createVerticalStrut(8));
		box2.add(text3);
		box2.add(Box.createVerticalStrut(8));
		box2.add(text4);
		box2.add(Box.createVerticalStrut(8));
		box2.add(text5);
		box2.add(Box.createVerticalStrut(8));
		box2.add(text6);
		box2.add(Box.createVerticalStrut(8));
		box2.add(text7);
		box2.add(Box.createVerticalStrut(8));
		box2.add(text8);
		box2.add(Box.createVerticalStrut(8));
		box2.add(text9);
		box2.add(Box.createVerticalStrut(8));
		choice1=new Choice();
		choice1.add("");
		choice1.add("自然科学");
		choice1.add("社会科学");
		choice1.add("人文科学");
		choice1.add("经济");
		choice1.add("法律");
		box2.add(choice1);
		box2.setBackground(Color.blue);
		box2.setForeground(Color.MAGENTA);
		
		baseBox=Box.createHorizontalBox();
		baseBox.add(box1);
		baseBox.add(Box.createHorizontalStrut(10));//水平支撑10
		baseBox.add(box2);
		baseBox.add(Box.createHorizontalStrut(10));
		baseBox.add(area1);
		button1=new Button("添加");
		button2=new Button("刷新");
		button1.addActionListener(this);//添加监视器
		button2.addActionListener(this);//添加监视器
		baseBox.setBackground(Color.MAGENTA);
		baseBox.setForeground(Color.blue);
		add(baseBox,BorderLayout.CENTER);
		panel1.add(button1);
		panel1.add(button2);
		add(panel1,BorderLayout.SOUTH);
		setResizable(false);
		setBounds(300,100,700,500);
		setBackground(Color.LIGHT_GRAY);
		setVisible(true);
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				dispose();
			}
		});
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==button1)
		{
			int n=JOptionPane.showConfirmDialog(this, "确认要添加吗?","确认对话框",JOptionPane.YES_NO_CANCEL_OPTION);
			if(n==JOptionPane.NO_OPTION||n==JOptionPane.CANCEL_OPTION)
			{
				area1.setText("已经取消添加图书信息\n");
			}
			else if(n==JOptionPane.YES_OPTION)//向数据库中添加信息,尚未连接数据库
			{
				if(text1.getText().equals("")&&text2.getText().equals(""))
				{
					area1.setText(null);
					JOptionPane.showMessageDialog(this, "缺少重要信息","警告",JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					 Connection con;
					 Statement sql;
					 ResultSet rs;
					 String insertStr;
					 try 
					 {
						Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
					 }
					 catch(ClassNotFoundException e1){	area1.setText("没有建立连接\n");}
					 try
					 {
					 	con=DriverManager.getConnection("jdbc:odbc:bcfshuju","","");
					 	sql=con.createStatement();
					 	insertStr="insert into book values('"+text1.getText()+"'"+","+"'"+text2.getText()+"'"+","+"'"+text3.getText()+"'"+","+"'"+text4.getText()+"'"+","+"'"+text5.getText()+"'"+","+"'"+text6.getText()+"'"+","+"'"+text7.getText()+"'"+","+"'"+text8.getText()+"'"+","+"'"+text9.getText()+"','"+choice1.getSelectedItem()+"'"+")"+";";
					 	sql.executeUpdate(insertStr);
						JOptionPane.showConfirmDialog(this, "信息插入成功?","确认对话框",JOptionPane.PLAIN_MESSAGE);
					 	con.close();
					 }
					 catch (SQLException e2)
					 {
						 JOptionPane.showConfirmDialog(this, "信息插入失败?","确认对话框",JOptionPane.PLAIN_MESSAGE);
					 }
					text1.setText(null);
					text2.setText(null);
					text3.setText(null);
					text4.setText(null);
					text5.setText(null);
					text6.setText(null);
					text7.setText(null);
					text8.setText(null);
					text9.setText(null);
					choice1.select(0);
				}
			}
		}
		else if(e.getSource()==button2)
		{
			area1.setText(null);
			text1.setText(null);
			text2.setText(null);
			text3.setText(null);
			text4.setText(null);
			text5.setText(null);
			text6.setText(null);
			text7.setText(null);
			text8.setText(null);
			text9.setText(null);
		}
	}
}
class bookDel extends Frame implements 	ActionListener
{
	Label label1,label2,label3,label4;
	TextField text1,text2,text3,text4;
	Button button1,button2,button3,button4,button5;
	TextArea area;
	Box box1,box2,box3,box4,basebox;
	bookDel(String s)
	{
		super(s);
		Font f=new Font("",Font.LAYOUT_LEFT_TO_RIGHT,20);
		label1=new Label("ISBN");  label1.setFont(f);
		label2=new Label("作者");  label2.setFont(f);
		label3=new Label("书名");  label3.setFont(f);
		label4=new Label("出版社");label4.setFont(f);
		box1=Box.createVerticalBox();
		box1.add(label1);    box1.add(Box.createVerticalStrut(8));
		box1.add(label2);    box1.add(Box.createVerticalStrut(8));
		box1.add(label3);    box1.add(Box.createVerticalStrut(8));
		box1.add(label4);    box1.add(Box.createVerticalStrut(8));
		box1.setForeground(Color.BLACK);
		
		text1=new TextField(20);	text1.setFont(f);text1.setBackground(Color.green);
		text2=new TextField(20);	text2.setFont(f);text2.setBackground(Color.green);
		text3=new TextField(20);	text3.setFont(f);text3.setBackground(Color.green);
		text4=new TextField(20);	text4.setFont(f);text4.setBackground(Color.green);
		box2=Box.createVerticalBox();
		box2.add(text1);	box2.add(Box.createVerticalStrut(8));
		box2.add(text2);	box2.add(Box.createVerticalStrut(8));
		box2.add(text3);	box2.add(Box.createVerticalStrut(8));
		box2.add(text4);	box2.add(Box.createVerticalStrut(8));
		box2.setForeground(Color.orange);
		
		button1=new Button("删除");	button1.addActionListener(this);
		button2=new Button("删除");	button2.addActionListener(this);
		button3=new Button("删除");	button3.addActionListener(this);
		button4=new Button("删除");	button4.addActionListener(this);
		box3=Box.createVerticalBox();
		box3.add(button1);		box3.add(Box.createVerticalStrut(8));	button1.setFont(f);
		box3.add(button2);		box3.add(Box.createVerticalStrut(8));	button2.setFont(f);
		box3.add(button3);		box3.add(Box.createVerticalStrut(8));	button3.setFont(f);
		box3.add(button4);		box3.add(Box.createVerticalStrut(8));	button4.setFont(f);
		
		box4=Box.createVerticalBox();
		area=new TextArea(9,20);
		area.setBackground(Color.green);
		area.setForeground(Color.blue);
		box4.add(area);
		area.setEditable(false);
		basebox=Box.createHorizontalBox();
		basebox.add(box1); basebox.add(Box.createHorizontalStrut(15));	
		basebox.add(box2); basebox.add(Box.createHorizontalStrut(15));  
		basebox.add(box3); basebox.add(Box.createHorizontalStrut(15));  
		basebox.add(box4); basebox.add(Box.createHorizontalStrut(15)); 
		add(basebox,BorderLayout.CENTER);
		setBackground(Color.lightGray);
		//setResizable(false);
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				dispose();
			}
		});
		setVisible(true);
		setBounds(300,250,600,600);
	}
	public void actionPerformed (ActionEvent e2)
	{
		int n=JOptionPane.showConfirmDialog(this, "确认要删除吗?","确认对话框",JOptionPane.YES_NO_CANCEL_OPTION);
		if(n==JOptionPane.NO_OPTION||n==JOptionPane.CANCEL_OPTION)
		{
			area.append("已经取消删除图书信息\n\n");	
		}
		else
		{
			Connection con;
			 Statement sql;
			 ResultSet rs;
			 String delStr;
			if(e2.getSource()==button1)
			{
				if(text1.getText().equals(""))
				{
					JOptionPane.showMessageDialog(this, "不能 为空值", "警告",JOptionPane.WARNING_MESSAGE);
					text1.setText(null);
				}
				else
				{
					try 
					 {
						Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
					 }
					 catch(ClassNotFoundException e1){area.append("没有建立连接");}
					 try
					 {
					 	 con=DriverManager.getConnection("jdbc:odbc:bcfshuju","","");
					 	sql=con.createStatement();
					 	sql.executeUpdate("delete  from book where ISBN="+text1.getText()+";");
					 	con.close();
					 	area.append("按照ISBN方式删除成功\n\n");
						text1.setText(null);
					 }
					 catch (SQLException e)
					 {
						 JOptionPane.showConfirmDialog(this, "信息删除失败?","确认对话框",JOptionPane.PLAIN_MESSAGE);
						 text1.setText(null);
					 }
				}
			}
			else if(e2.getSource()==button2)
			{
				if(text2.getText().equals(""))
				{
					JOptionPane.showMessageDialog(this, "不能 为空值", "警告",JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					try 
					 {
						Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
					 }
					 catch(ClassNotFoundException e1){area.append("没有建立连接");}
					 try
					 {
					 	 con=DriverManager.getConnection("jdbc:odbc:bcfshuju","","");
					 	sql=con.createStatement();
					 	sql.executeUpdate("delete  from book where writer="+text2.getText()+";");
					 	con.close();
					 	area.append("按照作者方式删除成功\n\n");
					 }
					 catch (SQLException e)
					 {
						 JOptionPane.showConfirmDialog(this, "信息删除失败?","确认对话框",JOptionPane.PLAIN_MESSAGE);
					 }
					 text2.setText(null);
				}
			}
			else if(e2.getSource()==button3)
			{
				if(text3.getText().equals(""))
				{
					JOptionPane.showMessageDialog(this, "不能 为空值", "警告",JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					try 
					 {
						Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
					 }
					 catch(ClassNotFoundException e1){area.append("没有建立连接");}
					 try
					 {
					 	 con=DriverManager.getConnection("jdbc:odbc:bcfshuju","","");
					 	sql=con.createStatement();
					 	//结果集的游标可以上下移动,数据库变化时,当前结果集不变,不能更新数据库 
					 	sql.executeUpdate("delete  from book where bName="+text3.getText()+";");

⌨️ 快捷键说明

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