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

📄 addcustomerframe.java

📁 邮局订报系统.
💻 JAVA
字号:
//AddcustomerFrame.java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;

import UI.customerUI;

class AddcustomerFrame extends JFrame {
   private customerUI userInterface;
   private JButton clearButton, writeButton;
   // JDBC driver and database URL
   static final String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
   static final String DATABASE_URL = "jdbc:odbc:PostofficeDSN";
   // declare Connection and Statement for accessing 
   // and querying database
   private Connection connection;
   private Statement statement;
   String sqlString ;
   //set up column names
   String names[] = { "客户姓名","电话","地址","邮编","客户代码"};
   String names1[] = { "报纸编号","报纸名称","报纸单价","版面","出版单位"};
   String names2[] = { "客户代码","报纸编号","份数","订阅日期","订报时间"};
   
   
   // set up GUI
   int k=0;
   public AddcustomerFrame(int k)
   {
      super( "Add a record " );
      //
      this.k=k;
      //
      initialize();  //connect to database
      // create instance of reusable user interface
      if(k==0)userInterface = new customerUI( names );  // four textfields
      if(k==1)userInterface = new customerUI( names1 );
      if(k==2)userInterface = new customerUI( names2 );
      
      getContentPane().add( userInterface, BorderLayout.CENTER );
      
      // configure button doTask1 for use in this program
      writeButton = userInterface.getDoTask1Button();
      writeButton.setText( "保存" );

      // register listener to call addRecord when button pressed
      writeButton.addActionListener(

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

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

         } // end anonymous inner class

      ); // end call to addActionListener

      // configure button doTask2 for use in this program
      clearButton = userInterface.getDoTask2Button();
      clearButton.setText( "清除" );
      
      // register listener to call userInterface clearFields() when button pressed
      clearButton.addActionListener(

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

            // call userInterface clearFields() when button pressed
            public void actionPerformed( ActionEvent event )
            {
               userInterface.clearFields();
            }

         } // 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 databse
            }

         } // end anonymous inner class

      ); // end call to addWindowListener

      setSize( 300, 200 );
      setVisible( true );

   } // end of  constructor

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

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

         // 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 

   // add record to file
   public void addRecord()
   {
      
      String fieldValues[] = userInterface.getFieldValues();
      
     
      // if sno field value is not empty
      if ( ! fieldValues[ customerUI.客户姓名 ].equals( "" ) ) {

      
         // output values to student
         try {
            int numberAge = Integer.parseInt(
            fieldValues[ customerUI.邮编 ] );
            //define string for sql insert  statement 
            String sqlInsert = "INSERT INTO customer " +
								 "VALUES ('" + 
								fieldValues[0] + "', '" +
								fieldValues[1]  +"', '"+
								fieldValues[2]+ "', "
								+numberAge+",'"+fieldValues[4] + "')";
            int result = statement.executeUpdate(sqlInsert);
            //
            
            
            //
            if (result!=0) {
                userInterface.clearFields();
               JOptionPane.showMessageDialog( this, 
                "Inserted sucess!", "Insert Result", 
                 JOptionPane.INFORMATION_MESSAGE ); 
            }
                 
         } // end try

         // process invalid age number 
         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  //if sno field value is  empty
            JOptionPane.showMessageDialog( this,
               "Bad sno number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
        

   
   } // end method addRecord


} // end AddStudentFrame class 

⌨️ 快捷键说明

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