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

📄 delete.java

📁 人员管理系统
💻 JAVA
字号:
//delete.java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;


class delete extends JFrame {
   private JButton  queren,qingchu, querenshanchu,fangqi;
   private JLabel sno,no,name,sex,address,polity,health,compact;
   private JTextField snot,namet,not,sext,addresst,polityt,healtht,compactt;
     // JDBC driver and database URL
   static final String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
   static final String DATABASE_URL = "jdbc:odbc:RSGLDSN";
   // declare Connection and Statement for accessing 
   // and querying database
   private Connection connection;
   private Statement statement;
   String sqlString ;
      // set up GUI
   public delete()
   {
      super( "Delete records from students" );
      
      initialize(); //connect database
      
      sno=new JLabel("请输入要删除的工号:");
      snot=new JTextField(20);
      
      no=new JLabel("  工   号:               ");
      not=new JTextField(20);
      name=new JLabel("姓   名:              ");
      sex=new JLabel("性   别:  ");
      address=new JLabel("地   址:                " );
      polity=new JLabel("政治面貌:                 ");
      health=new JLabel("生理状况:                ");
      compact=new JLabel("合   同:                     ");
      
      not=new JTextField(20);
	  namet=new JTextField(20);
	  sext=new JTextField(20);
	  addresst=new JTextField(20);
	  polityt=new JTextField(20);
	  healtht=new JTextField(20);
	  compactt=new JTextField(20);
	  
	  
     queren=new JButton("确定");
		qingchu=new JButton("清除");
		querenshanchu=new JButton("确认删除");
		fangqi=new JButton("放弃");
      
       JPanel j1=new JPanel();
		JPanel j2=new JPanel();
	 JPanel j3=new JPanel();	
	 JPanel j4=new JPanel();
	   JPanel j5=new JPanel();
		
		j4.add(sno);
		j4.add(snot);
		j5.add(queren);
		j5.add(qingchu);
		
		j2.add(no);
		j2.add(not);
		j2.add(name);
		j2.add(namet);
		j2.add(sex);
		j2.add(sext);
		j2.add(address);
		j2.add(addresst);
		j2.add(polity);
		j2.add(polityt);
		j2.add(health);
		j2.add(healtht);
		j2.add(compact);
		j2.add(compactt);
      
      	j3.add(querenshanchu);
      	j3.add(fangqi);
      	
      	Container container=getContentPane();
      	container.setLayout(new BorderLayout());
		j1.setLayout(new BorderLayout());
		j2.setLayout(new FlowLayout());
		j3.setLayout(new FlowLayout());
		j4.setLayout(new FlowLayout());
		j5.setLayout(new FlowLayout());
		
		j1.add(j4,BorderLayout.CENTER);
		j1.add(j5,BorderLayout.SOUTH);
		
		container.add(j1,BorderLayout.NORTH);
		container.add(j2,BorderLayout.CENTER);
		container.add(j3,BorderLayout.SOUTH);
		
	  // register listener to call openFile when firstButton1 pressed
      queren.addActionListener(

         // anonymous inner class to handle firstButton2 event
         new ActionListener() {

            // call DisplayRecord() when button pressed
            public void actionPerformed( ActionEvent event )
            {
               //initialize();
               DisplayRecord();
            }

         } // end anonymous inner class

      ); // end call to addActionListener

     
    qingchu.addActionListener(

         // anonymous inner class to handle secondButton1 event
         new ActionListener() {

            // call clearFields when button pressed
            public void actionPerformed( ActionEvent event )
            {
               snot.setText("");
               not.setText("");
               namet.setText("");
               not.setText("");
               sext.setText("");
               addresst.setText("");
               polityt.setText("");
               healtht.setText("");
               compactt.setText("");
            }

         } // end anonymous inner class

      ); 
            
      querenshanchu.addActionListener(

         // anonymous inner class to handle firstButton2 event
         new ActionListener() {

            // call UpdateRecord when button pressed
            public void actionPerformed( ActionEvent event )
            {
               UpdateRecord();
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      fangqi.addActionListener(

         // anonymous inner class to handle secondButton2 event
         new ActionListener() {

            // call addRecord when button pressed
            public void actionPerformed( ActionEvent event )
            {
               snot.setText("");
               not.setText("");
               namet.setText("");
               not.setText("");
               sext.setText("");
               addresst.setText("");
               polityt.setText("");
               healtht.setText("");
               compactt.setText("");
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      // register window listener to handle window closing event
      addWindowListener(

         // anonymous inner class to handle windowClosing event
         new WindowAdapter() {

            // add current record in GUI to file, then close file
            public void windowClosing( WindowEvent event )
            {
               
               terminate();//close connectin
            }

         } // end anonymous inner class

      ); // end call to addWindowListener

      setSize(380,350);
		setVisible(true);

   } // end  constructor

  //disconnect database
   public  void initialize()
   {
      try {
         
         
         Class.forName( JDBC_DRIVER );

         // establish connection to database
         connection = DriverManager.getConnection( DATABASE_URL,"sa",null );

         // create Statement for querying database
         statement = connection.createStatement();
      }
      catch ( SQLException sqlException ) {
         JOptionPane.showMessageDialog( null, sqlException.getMessage(), 
            "Database Error", JOptionPane.ERROR_MESSAGE );
         
         System.exit( 1 );
      }
      
      // detect problems loading database driver
      catch ( ClassNotFoundException classNotFound ) {
         JOptionPane.showMessageDialog( null, classNotFound.getMessage(), 
            "Driver Not Found", JOptionPane.ERROR_MESSAGE );            

         System.exit( 1 );
      }
   } // end method openFile

   //  close database
   public  void terminate() 
   {
       try {
            statement.close();
            connection.close();            
         }
         
         // handle exceptions closing statement and connection
         catch ( SQLException sqlException ) {
            JOptionPane.showMessageDialog( null, 
               sqlException.getMessage(), "Database Error", 
               JOptionPane.ERROR_MESSAGE );
         
            System.exit( 1 );
         }
   } // end method 
   
   public void DisplayRecord()
   {
   	if ( ! snot.getText().equals( "" ) ) {
         
         try {
                       
            String sqlString = "select * from people "+
                             " where no='"+snot.getText() + "'";
                                
            ResultSet resultSet =statement.executeQuery( sqlString)  ;
            
            if ( resultSet.next() ) {
                not.setText(resultSet.getString( 1 )) ;
                namet.setText(resultSet.getString( 2 )) ;
                sext.setText(resultSet.getString( 3 )) ;
                addresst.setText(resultSet.getString( 4 ));
                polityt.setText(resultSet.getString( 5 )) ;
                healtht.setText(resultSet.getString( 6 ));
                compactt.setText(resultSet.getString( 7 ));
                
            }    
            else
            {
               snot.setText("");
               not.setText("");
               namet.setText("");
               not.setText("");
               sext.setText("");
               addresst.setText("");
               polityt.setText("");
               healtht.setText("");
               compactt.setText("");
                JOptionPane.showMessageDialog( this, 
                  "Not fund this record!", "Find Result", 
                    JOptionPane.INFORMATION_MESSAGE ); 
            }     
                             
         } // end try

         // process invalid age number  format
         catch ( NumberFormatException formatException ) {
            JOptionPane.showMessageDialog( this,
               "Bad age number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
         }

         // process exceptions from file output
         catch (SQLException ee)
		{   System.out.println(ee);	} 
		}  
		//end of if sno field value is not empty
		else 
            JOptionPane.showMessageDialog( this,
               "Bad sno number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
     	
   }
   
   // Delete record of student
   public void UpdateRecord()
   {
      
         
      // if sno field value is not empty
      if ( ! snot.getText().equals( "" ) ) {

      
         // delete record from student
         try {
           
            String sqlString = "delete from people  "+
                               "where no='"+snot.getText() + "'";
								
            int result = statement.executeUpdate(sqlString);
            if (result!=0) {
                JOptionPane.showMessageDialog( this, 
                "Deleted sucess!", "Delete Result", 
                 JOptionPane.INFORMATION_MESSAGE ); 
            }
                 
         } // end try

         // process invalid age  format
         catch ( NumberFormatException formatException ) {
            JOptionPane.showMessageDialog( this,
               "Bad age number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
         }

         // process exceptions for sql
         catch (SQLException ee)
				{ System.out.println(ee);	} 
		}  //end of if sno field value is not empty
		
		else 
            JOptionPane.showMessageDialog( this,
               "Bad sno number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
        

   
   } // end method updateRecord

   public static void main( String args[] )
   {
      delete d1=new delete();
      
   }

} // end class 

⌨️ 快捷键说明

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