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

📄 book_update.java

📁 图书馆管理系统 用swing界面做的一个图书馆管理系统 里面有增删改等功能 适合初学者看看
💻 JAVA
字号:

//图书更新
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class book_update extends JFrame implements ActionListener{
	
	JLabel la_id=new JLabel("要修改的图书编号:");
	JLabel la_attention=new JLabel("注意: 编号规则为001-999");
	
	JLabel la_info1=new JLabel("欲更新的图书");
	JLabel la_info2=new JLabel("更新简介");
	
	
	JLabel up_name=new JLabel("更新书名");
	JLabel up_isbn=new JLabel("更新ISBN");
	JLabel up_price=new JLabel("更新价格");
	JLabel up_author=new JLabel("更新作者");
	JLabel up_publish=new JLabel("更新出版社");
	
	JTextField t_name=new JTextField(15);
	JTextField t_isbn=new JTextField(15);
	JTextField t_price=new JTextField(15);
	JTextField t_author=new JTextField(15);
	JTextField t_publish=new JTextField(30);
	
	JTextField t_update=new JTextField(15);
	
	JButton b_demand=new JButton("查 询");
	JButton b_turedemand=new JButton("确认更新");
	JButton b_reset=new JButton("重置");
	JButton b_exit=new JButton("返 回");
	
	String[] tab_name={"书目编号","书名","ISBN号","价格","作者","出版社","简介"};
	String[][] tab_id=new String[1][7];
	
	JTable lab_update=new JTable(tab_id,tab_name);
	
	JScrollPane sp1 = new JScrollPane(lab_update);
	
	JTextArea ta_update = new JTextArea();
	JScrollPane sp2 = new JScrollPane(ta_update);
	
	
	action_demand ac_demand =new action_demand();
	action_turndemand ac_turndemand=new action_turndemand();
	action_reset ac_reset =new action_reset();
	
	Connection conn=null;	
	
	public book_update(){
		super("修改图书");
		this.setLayout(null);
		
		JPanel p_up =new JPanel();//上面面板
		JPanel p_down=new JPanel();//下面面板
		
		JPanel p_up1 =new JPanel();
		JPanel p_up2 =new JPanel();
		JPanel p_up3 =new JPanel();
		
		JPanel p_down1 =new JPanel();
		JPanel p_down2 =new JPanel();
		JPanel p_down3 =new JPanel();
		
		JPanel p_info1=new JPanel();
		JPanel p_info2=new JPanel();
		
		p_up.setBounds(3,10,790,100);
		p_up.setLayout(new GridLayout(3,1));
		
		p_up1.add(la_id); p_up1.add(t_update);
		p_up2.add(la_attention);
		p_up3.add(b_demand); p_up3.add(b_turedemand); p_up3.add(b_reset); p_up3.add(b_exit);
		
		p_up.add(p_up1); p_up.add(p_up2); p_up.add(p_up3);
		this.add(p_up);
		
		p_info1.setBounds(3,110,100,20);//添加提示信息1
		p_info1.add(la_info1);
		this.add(p_info1);
		
		sp1.setBounds(3,135,790,55);//添加table1
		this.add(sp1);
		
		p_down.setBounds(3,190,790,90);
		p_down.setLayout(new GridLayout(3,1));
		
		p_down1.add(up_name); p_down1.add(t_name);p_down1.add(up_isbn); p_down1.add(t_isbn);
		p_down2.add(up_price); p_down2.add(t_price);p_down2.add(up_author);p_down2.add(t_author);
		p_down3.add(up_publish);p_down3.add(t_publish);
		
		p_down.add(p_down1);p_down.add(p_down2);p_down.add(p_down3);
		this.add(p_down);
		
		p_info2.setBounds(3,280,100,20);//添加提示信息1
		p_info2.add(la_info2);
		this.add(p_info2);
		
		sp2.setBounds(3,305,790,50);
		this.add(sp2);
		
		//窗口属性
		this.setBounds(200,200,800,410);
		this.setVisible(true);
		
		this.setResizable(false);
		
		//注册监听器
		b_demand.addActionListener(ac_demand);
		b_turedemand.addActionListener(ac_turndemand);
		b_reset.addActionListener(ac_reset);
		b_exit.addActionListener(this);
		
		//连接数据库
		conn = book_sql.getConn();
		//System.out.println(conn);
	}
	public void actionPerformed(ActionEvent e){
			new book_main();//返回主菜单
			this.dispose();
	}
	//查询按钮
	class action_demand implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String emp=t_update.getText().trim(); //得到查询的文本
			String sql=null;
			if(emp.equals("")){
				
				JOptionPane.showMessageDialog(null,"查询关键字不可为空",
							"Caution",JOptionPane.WARNING_MESSAGE);					
						return;
						
			}else{
				sql="select* from bookinfo where bid='"+emp+"'";
				main_sql(sql);
			}
		}
		void main_sql(String sql){
			ResultSet rs=null;
			Statement st=null;
			try{
				st=conn.createStatement();
				rs=st.executeQuery(sql);
				
				rs.beforeFirst();//定位到第一行
				
				while(rs.next()){
					tab_id[0][0] = rs.getString(1);
					tab_id[0][1] = rs.getString(2);
					tab_id[0][2] = rs.getString(3);
					tab_id[0][3] = rs.getString(4);
					tab_id[0][4] = rs.getString(5);
					tab_id[0][5] = rs.getString(6);
					tab_id[0][6] = rs.getString(7);
				}
			}catch(SQLException sqle){
				sqle.printStackTrace();
			}
			lab_update.repaint();
		}
		
	}
	//确认更新按钮
	class action_turndemand implements ActionListener{
		public void actionPerformed(ActionEvent e){
			
			ResultSet rs = null;
			Statement st=null;
			
			String emp=t_update.getText().trim();
			
			String e_name=t_name.getText().trim();
			String e_isbn=t_isbn.getText().trim();
			String e_price=t_price.getText().trim();
			String e_author=t_author.getText().trim();
			
			String e_publish=t_publish.getText().trim();
			String e_update=ta_update.getText().trim();
			
			//更新name
			if(e_name.equals("")){
				
			}else{
				try{
					st=conn.createStatement();
					String sql="update bookinfo set name='"+e_name+"'"+" where bid='"+emp+"'";
					System.out.println(sql);
					st.executeUpdate(sql);
					
				}catch(SQLException sqle){
					sqle.printStackTrace();
				}
				
				JOptionPane.showMessageDialog(null,"更新成功",
							"Caution",JOptionPane.WARNING_MESSAGE);					
						return;				
			}
			//更新isbn
			if(e_isbn.equals("")){
				
			}else{
				try{
					st=conn.createStatement();
					String sql="update bookinfo set isbn='"+e_isbn+"'"+" where bid='"+emp+"'";
					System.out.println(sql);
					st.executeUpdate(sql);
					
				}catch(SQLException sqle){
					sqle.printStackTrace();
				}
				JOptionPane.showMessageDialog(null,"更新成功",
							"Caution",JOptionPane.WARNING_MESSAGE);					
						return;				
			}
			//更新price
			if(e_price.equals("")){
				
			}else{
				try{
					st=conn.createStatement();
					String sql="update bookinfo set price='"+e_price+"'"+" where bid='"+emp+"'";
					System.out.println(sql);
					st.executeUpdate(sql);
					
				}catch(SQLException sqle){
					sqle.printStackTrace();
				}
				JOptionPane.showMessageDialog(null,"更新成功",
							"Caution",JOptionPane.WARNING_MESSAGE);					
						return;				
			}
			//更新author
			if(e_author.equals("")){
				
			}else{
				try{
					st=conn.createStatement();
					String sql="update bookinfo set anthor='"+e_author+"'"+" where bid='"+emp+"'";
					System.out.println(sql);
					st.executeUpdate(sql);
					
				}catch(SQLException sqle){
					sqle.printStackTrace();
				}
				JOptionPane.showMessageDialog(null,"更新成功",
							"Caution",JOptionPane.WARNING_MESSAGE);					
						return;				
			}
			//更新publish
			if(e_publish.equals("")){
				
			}else{
				try{
					st=conn.createStatement();
					String sql="update bookinfo set pubnisher='"+e_publish+"'"+" where bid='"+emp+"'";
					System.out.println(sql);
					st.executeUpdate(sql);
					
				}catch(SQLException sqle){
					sqle.printStackTrace();
				}
				JOptionPane.showMessageDialog(null,"更新成功",
							"Caution",JOptionPane.WARNING_MESSAGE);					
						return;				
			}
			//更新简介
			if(e_update.equals("")){
				
			}else{
				try{
					st=conn.createStatement();
					String sql="update bookinfo set brief='"+e_update+"'"+" where bid='"+emp+"'";
					System.out.println(sql);
					st.executeUpdate(sql);
					
				}catch(SQLException sqle){
					sqle.printStackTrace();
				}
				JOptionPane.showMessageDialog(null,"更新成功",
							"Caution",JOptionPane.WARNING_MESSAGE);					
						return;								
			}
			t_update.setText(""); //清空关键字输入框					
		}
	}
	//重置按钮
	class action_reset implements ActionListener{
		public void actionPerformed(ActionEvent e){
			
			t_update.setText("");//清空输入编号框
			t_name.setText("");
	        t_isbn.setText("");
	        t_price.setText("");
	        t_author.setText("");
	        t_publish.setText("");
	        ta_update.setText("");//清空文本域
	        
	        JOptionPane.showMessageDialog(null,"清空成功!",
							"Caution",JOptionPane.WARNING_MESSAGE);					
						return;
						
		}
	}
	//
}

⌨️ 快捷键说明

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