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

📄 adddinggou.java

📁 邮局订报系统.
💻 JAVA
字号:
//adddinggou.java
//AdddinggouFrame.java
// number of text fields in GUI  


   // constants representing text fields in GUI
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;


class adddinggou extends JFrame {
   private dinggou 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[] = { "客户姓名","电话","地址","邮编","客户代码"," 报纸编号","份数"};
   // set up GUI
   public adddinggou()
   {
      super( "Add a record of subscription" );
      initialize();  //connect to database
      // create instance of reusable user interface
      userInterface = new dinggou( names );  // four textfields
      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[ dinggou.客户代码 ].equals( "" )&&!fieldValues[ dinggou.报纸编号 ].equals( "" ) ) {

      
         // output values to student
         try {
            int numberAge = Integer.parseInt(
            fieldValues[ dinggou.客户代码 ] );
            //define string for sql insert  statement 
            String sqlInsert = "INSERT INTO customer " +
								 "VALUES ('" + 
								fieldValues[0] + "', '" +
								fieldValues[1]  +"', '"+
								fieldValues[2]+ "', "
								+fieldValues[3]+",'"+fieldValues[4] + "')";
            int result1 = statement.executeUpdate(sqlInsert);

             
sqlInsert = "INSERT INTO subscription " +
								 "VALUES ('" + 
								fieldValues[4] + "', '" +
								fieldValues[5]  +"', "+
								fieldValues[6]+ "')";
            int result2 = statement.executeUpdate(sqlInsert);  
              
             if (result1!=0&&result2!=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 code 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  number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
        

   
   } // end method addRecord



} // end AddStudentFrame class 

⌨️ 快捷键说明

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