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

📄 jinhuo.java

📁 线程超市管理系统 采用线程管理的超市管系统
💻 JAVA
字号:
package zh;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Jinhuo  extends JPanel implements MouseListener,ActionListener{
 JPanel buttonsPane,tablePane,listPane,centerPane;
 JPanel pane;
 JPanel pane1,pane2,pane3,pane4,pane5;
 JTextField txt1,txt2,txt3,txt4,txt5;
 JButton button1,button2,button3,button4,button5,button6,button7;
 JScrollPane scroll;
 JTable table;Object  s1[];
 Vector rows[][]={};DefaultTableModel defaultModel = null;
 String []labels={"商品名称","条形码","商品价格","数量","厂商"};
 Connection con; Statement st;int i,x=0;
 String m;
 Thread1 thread;
  Jinhuo(){
	  try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			
		} catch (ClassNotFoundException  ee) {}
	  try{

    	    con=DriverManager.getConnection("jdbc:odbc:zz","","");
    	   st=con.createStatement(); 
    	           	   
    	   ResultSet rs=st.executeQuery("select * from jinhuo ");
    	    while(rs.next()){
    	    	
              x++;
    	     
//    	    table.repaint();
    	   }

    	       	   
    	   	        	         
    	   }catch(java.lang.Exception ee){}
    	
		setBorder(BorderFactory.createTitledBorder("进货管理"));
	  button1=new JButton("添加(A)");
	  button1.addActionListener(this);
	  button2=new JButton("修改(E)");
	  button2.addActionListener(this);
	  button3=new JButton("删除(D)");
	  button3.addActionListener(this);
	  button4=new JButton("报表(R)");
	  button4.setPreferredSize(new Dimension(100,15));
	  button4.addActionListener(this);
	  button5=new JButton("检查进货需求");
	  button5.addActionListener(this);
	  buttonsPane=new JPanel();
	  buttonsPane.setLayout(new GridLayout(1,5));
	  buttonsPane.add(button1);
	  buttonsPane.add(button2);
	  buttonsPane.add(button3);
	  buttonsPane.add(button4);
	  buttonsPane.add(button5);
	  
	  rows=new Vector[x][6];
	  defaultModel=new DefaultTableModel(rows,labels);
	  table=new JTable(defaultModel);
	  table.addMouseListener(this);
	  scroll=new JScrollPane(table);
	  

	  txt1=new JTextField(8);
	  txt2=new JTextField(8);
	  txt3=new JTextField(8);
	  txt4=new JTextField(8);
	  txt5=new JTextField(8);
	  pane1=new JPanel();
	  pane2=new JPanel();
	  pane3=new JPanel();
	  pane4=new JPanel();
	  pane5=new JPanel();
	  pane1.add(txt1);
	  pane2.add(txt2);
	  pane3.add(txt3);
	  pane4.add(txt4);
	  pane5.add(txt5);
	  
      listPane=new JPanel();  
      listPane.setLayout(new GridLayout(5,1));
      listPane.add(pane1);      
      listPane.add(pane2);
      listPane.add(pane3);
      listPane.add(pane4);
      listPane.add(pane5);
      
      centerPane=new JPanel();
	  centerPane.setLayout(new BorderLayout());
	  centerPane.add(scroll,BorderLayout.CENTER);
	  centerPane.add(buttonsPane,BorderLayout.SOUTH);
	  setLayout(new BorderLayout());
	  add(listPane,BorderLayout.EAST);
	 add(centerPane,BorderLayout.CENTER);
	 
	
//		 thread=new Thread1();
//		 thread.setPriority(Thread.MIN_PRIORITY);
//		 thread.start();	  
	  }	
  
  public void actionPerformed(ActionEvent e){
	  if(e.getSource()==button5){
		  thread=new Thread1();
			 thread.setPriority(Thread.MIN_PRIORITY);
		 thread.start();	  
		  try{
				int j=0;
	      	    con=DriverManager.getConnection("jdbc:odbc:zz","","");
	      	   st=con.createStatement(); 
	      	           	   
	      	   ResultSet rs=st.executeQuery("select * from jinhuo ");
	      	    while(rs.next()){
	      	    	table.repaint();  
	            
	      	    	String name1=rs.getString(2);
	      	    	int name2=rs.getInt(3);
	      	    	String name3=rs.getString(4);
	      	    	int  name4=rs.getInt(5);
	      	        String name5=rs.getString(6);
	      	     table.setValueAt(name1,j,0);
	      	     table.setValueAt(name2,j,1);
	      	     table.setValueAt(name3,j,2);
	      	     table.setValueAt(name4,j,3);
	      	     table.setValueAt(name5,j,4);
	      	    j++;

	      	   } 
		  }catch(java.lang.Exception ee){}
		}
	  else if(e.getSource()==button1){
			try {

				 con = DriverManager.getConnection("jdbc:odbc:zz","", "");
				 st = con.createStatement();
//				String s1="'"+txtemployee_ID.getText().trim()+"'";
				String s2="'"+txt1.getText().trim()+"'";
				String s3="'"+txt2.getText().trim()+"'";
				String s4="'"+txt3.getText().trim()+"'";
				String s5="'"+txt4.getText().trim()+"'";
				String s6="'"+txt5.getText().trim()+"'";
				
				String temp="INSERT  INTO jinhuo (商品名称,条形码,商品价格,数量,厂商) VALUES("+s2+","+s3+","+s4+","+s5+","+s6+")";
				
				st.executeUpdate(temp);
				 defaultModel.addRow(new Vector());
				
				 table.revalidate();

				con.close();
				
			} catch (java.lang.Exception ee) {}
		}
	  else if(e.getSource()==button2){
			try {
				

				 con = DriverManager.getConnection("jdbc:odbc:zz","", "");
				 st = con.createStatement();
//				 int s1=Integer.parseInt(txtemployee_ID.getText().trim());
					String s2="'"+txt1.getText().trim()+"'";
					String s3="'"+txt2.getText().trim()+"'";
					String s4="'"+txt3.getText().trim()+"'";
					String s5="'"+txt4.getText().trim()+"'";
					String s6="'"+txt5.getText().trim()+"'";
//					String s7="'"+txtemployee_leibie.getText().trim()+"'";
//					String tmp1="UPDATE jinhuo set 商品名称="+s2+"where ID="+i;
					String tmp2="UPDATE jinhuo set 条形码="+s3+"where 商品名称="+m;
					String tmp3="UPDATE jinhuo set 商品价格="+s4+"where 商品名称="+m;
					String tmp4="UPDATE jinhuo set 数量="+s5+"where 商品名称="+m;
					String tmp5="UPDATE jinhuo set 厂商="+s6+"where 商品名称="+m;
//					/String tmp6="UPDATE password set leibie="+s7+"where ID="+s1;
//					st.executeUpdate(tmp1);
					st.executeUpdate(tmp2);
					st.executeUpdate(tmp3);
					st.executeUpdate(tmp4);
					st.executeUpdate(tmp5);
//					st.executeUpdate(tmp6);
					con.close();
			}catch(java.lang.Exception ee){}
		}
	  else if(e.getSource()==button3){
		  txt1.setText(null);
		  txt2.setText(null);
		  txt3.setText(null);
		  txt4.setText(null);
		  txt5.setText(null);
			try {
//				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				 con = DriverManager.getConnection("jdbc:odbc:zz",
						"", "");
				 st = con.createStatement();
//				 int syh=Integer.parseInt(txtemployee_ID.getText());
				 st.executeUpdate("delete  from jinhuo where 商品名称="+m );
				 int rowcount = defaultModel.getRowCount()-1;//getRowCount返回行数,rowcount<0代表已经没有任何行了。
		            if(rowcount >= 0)
		            {
		                defaultModel.removeRow(rowcount);
		                defaultModel.setRowCount(rowcount);//删除行比较简单,只要用DefaultTableModel的removeRow()方法即可。删除
		                 //行完毕后必须重新设置列数,也就是使用DefaultTableModel的setRowCount()方法来设置。
		            }

				 con.close();
			}catch(java.lang.Exception ee){}
		}
  }
  public void mousePressed(MouseEvent e){}
  public void mouseReleased(MouseEvent e){}
  public void mouseExited(MouseEvent e){}
  public void mouseEntered(MouseEvent e){}
  public void mouseClicked(MouseEvent e){
	   i = table.getSelectedRow();
//	  i=i+1;
            m="'"+table.getValueAt(i, 0).toString().trim()+"'";
			ResultSet rs;

			String name1, name3, name5;int name2,name4;

			try {
				
				Connection Con = DriverManager.getConnection("jdbc:odbc:zz",
						"", "");
				Statement Stmt = Con.createStatement();
				rs = Stmt.executeQuery("select * from jinhuo where 商品名称=" +m);

				while (rs.next()) {
					

					    name1=rs.getString(2);
						name2 = rs.getInt(3);
						name3 = rs.getString(4);
						name4 = rs.getInt(5);
						name5 = rs.getString(6);
						String nam2=Integer.toString(name2);
						String nam4=Integer.toString(name4);
						txt1.setText(name1);
						txt2.setText(nam2);
						txt3.setText(name3);
						txt4.setText(nam4);
						txt5.setText(name5);
						

					
				}

				Con.close();


			} catch (java.lang.Exception ee) {}
      }
//  public Class getColumnClass(int c) {
//	    return table.getValueAt(0, c).getClass();
//	}
//public void start(){
//	if(thread==null){
//		thread=new Thread(this);
//		thread.start();
//	}
//}
  class Thread1 extends Thread{
		public void run() {
			// TODO Auto-generated method stub
			
			
			while(true){
				int j=0;

				try{
					
					Connection con=DriverManager.getConnection("jdbc:odbc:zz","","");
		  	   Statement st=con.createStatement(); 
		      	           	   
		      	   ResultSet rs=st.executeQuery("select * from jinhuo ");
		      	    while(rs.next()){
		      	    	
		      	    	
		      	    	String name1=rs.getString(2);
		      	    	int name2=rs.getInt(3);
		      	    	String name3=rs.getString(4);
		      	    	int  name4=rs.getInt(5);
		      	        String name5=rs.getString(6);
		      	     table.setValueAt(name1,j,0);
		      	     table.setValueAt(name2,j,1);
		      	     table.setValueAt(name3,j,2);
		      	     table.setValueAt(name4,j,3);
		      	     table.setValueAt(name5,j,4);
		      	    j++;
		      	  
		      	   } 	   
		      	   	      	         
		      	   }catch(java.lang.Exception ee){} 
		      	   table.repaint();
		      	   try{
		      		   Thread.sleep(300);
		      	   }
		      	   catch(InterruptedException exp){}
			  }
		  }
	}

}




⌨️ 快捷键说明

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