Applet to insert data to my database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robbietherobber
    New Member
    • Apr 2010
    • 1

    Applet to insert data to my database

    I am only begining to work with Java and I have hit a point which i just cant understand.

    I have a very simple applet which is suppossed to send data back to an access database.
    I have the same thing in an application and it uses the same odbc link and sends info back to the access database but i cant get the applet to do the same.

    so I must be missing something but i cant see it.

    I will add the applet and the two java code pages

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.sql.*;
    
    
    public class AppletCust extends JApplet implements ActionListener
    {
        public static JTextField display;
        
        
        public static JTextField textFirstName, textSecondName, textPhoneNum, textEmailAdd, textStreet, textHouseNum, textCity, textCounty, textNationality, textDOB, textPlaceOfEdu;
        
        public void init()
        {
           
            Container contentPane = getContentPane();
            contentPane.setLayout(new BorderLayout(12,12));
            
            JPanel centerPanel = new JPanel(new GridLayout(1, 2));
            JLabel prompt = new JLabel("Display Messages:");
            centerPanel.add(prompt);
            display = new JTextField("Please Fill in All Details to register");
            display.setEditable(false);
            centerPanel.add(display);
            
            contentPane.add(centerPanel, BorderLayout.NORTH);
    
            
            JPanel customerPanel = new JPanel(new GridLayout(11, 2));            
                addLabel(customerPanel, "First Name ");
                textFirstName = new JTextField("");
                customerPanel.add(textFirstName);
                addLabel(customerPanel, "Second Name ");
                textSecondName = new JTextField("");
                customerPanel.add(textSecondName);
                addLabel(customerPanel, "Phone Number ");
                textPhoneNum = new JTextField("");
                customerPanel.add(textPhoneNum);
                addLabel(customerPanel, "Email Address ");
                textEmailAdd = new JTextField("");
                customerPanel.add(textEmailAdd);
                addLabel(customerPanel, "Street Name ");
                textStreet = new JTextField("");
                customerPanel.add(textStreet);
                addLabel(customerPanel, "House number ");
                textHouseNum = new JTextField("");
                customerPanel.add(textHouseNum);
                addLabel(customerPanel, "City ");
                textCity = new JTextField("");
                customerPanel.add(textCity);
                addLabel(customerPanel, "County ");
                textCounty = new JTextField("");
                customerPanel.add(textCounty);
                addLabel(customerPanel, "Nationality ");
                textNationality = new JTextField("");
                customerPanel.add(textNationality);
                addLabel(customerPanel, "Date of Birth ");
                textDOB = new JTextField("");
                customerPanel.add(textDOB);
                addLabel(customerPanel, "Place of Education ");
                textPlaceOfEdu = new JTextField("");
                customerPanel.add(textPlaceOfEdu);
                
    
    
                contentPane.add(customerPanel, BorderLayout.CENTER);
    
            JPanel buttonPanel = new JPanel();
            
            JButton mercury = new JButton("Register");
            mercury.addActionListener(this);
            buttonPanel.add(mercury);
            
            contentPane.add(buttonPanel, BorderLayout.SOUTH);
    
        }
    
        
        
        
         private void addLabel(Container panel, String labelText)
        {
            JLabel label = new JLabel(labelText);
            panel.add(label);
        }
        
        public void actionPerformed(ActionEvent event)
        {
            String command = event.getActionCommand();
         
            if(command.equals("Register"))
            createCustomerRecord();
    
        } 
    
            public void createCustomerRecord()
        {
    
                  
            String custFirstName = textFirstName.getText();
            String custSecondName = textSecondName.getText();
            String custPhoneNum = textPhoneNum.getText();
            String custEmailAdd = textEmailAdd.getText();
            String custStreet = textStreet.getText();
            String custHouseNum = textHouseNum.getText();
            String custCity = textCity.getText();
            String custCounty = textCounty.getText();
            String custNationality = textNationality.getText();
            String custDOB = textDOB.getText();
            String custPlaceOfEdu = textPlaceOfEdu.getText();
            redisplay();
            display.setText("Registered: ");
            Database(custFirstName, custSecondName, custPhoneNum, custEmailAdd, custStreet, custHouseNum, custCity, custCounty, custNationality, custDOB, custPlaceOfEdu);  
            
            
        }   
    
            public void redisplay()
        {
            textFirstName.setText("");
            textFirstName.setEditable(false);
            textSecondName.setText("");
            textSecondName.setEditable(false);
            textPhoneNum.setText("");
            textPhoneNum.setEditable(false);
            textEmailAdd.setText("");
            textEmailAdd.setEditable(false);
            textStreet.setText("");
            textStreet.setEditable(false);
            textHouseNum.setText("");
            textHouseNum.setEditable(false);
            textCity.setText("");
            textCity.setEditable(false);
            textCounty.setText("");
            textCounty.setEditable(false);
            textNationality.setText("");
            textNationality.setEditable(false);
            textDOB.setText("");
            textDOB.setEditable(false);
            textPlaceOfEdu.setText("");
            textPlaceOfEdu.setEditable(false);
        }
        
        
        
        public void Database(String custFirstName, String custSecondName, String custPhoneNum, String custEmailAdd, String custStreet, String custHouseNum, String custCity, String custCounty, String custNationality, String custDOB, String custPlaceOfEdu)
        {
        Customer C1 = new Customer(custFirstName, custSecondName, custPhoneNum, custEmailAdd, custStreet, custHouseNum, custCity, custCounty, custNationality, custDOB, custPlaceOfEdu);
        Database D1 =  new Database(C1);
    
        
        display.setText("Registered:" + custFirstName);
        
    
        }
    
        public void Database (Customer cust)
        {
    
        Connection con; // The connection to the database.
        
        // The following code can throw errors, so they must be caught.
        try{
        
        // First, tell Java what driver to use and where to find it.
    
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        // Next, create a connection to your data source.
        // Specify that you are using the ODBC-JDBC Bridge.
        // And specify the data source from ODBC.
    
        con = DriverManager.getConnection("jdbc:odbc:eProofreadersDB");
        
        // Create an SQL statement.
        Statement stmt = con.createStatement();
        
        // Execute some SQL to create a table in your database.
        // If the table already exists, an exception is thrown!
        
        stmt.executeUpdate("INSERT INTO CUSTOMER VALUES ('" + cust.custFirstName + "', '" + cust.custSecondName + "', '" + cust.custPhoneNum + "', '" + cust.custEmailAdd + "', '" + cust.custStreet + "', '" + cust.custHouseNum + "', '" + cust.custCity + "','" + cust.custCounty + "', '" + cust.custNationality + "', '" + cust.custDOB + "', '" + cust.custPlaceOfEdu + "')");
        // Catch any exceptions that are thrown.
        }
    
        
        catch(ClassNotFoundException e){
        
        System.out.println(e.toString());
        }
        catch(SQLException e){
        
        System.out.println(e.toString());
        
        }
        }
    }
    the customer class
    Code:
    import java.util.Random;
    
    public class Customer
    
    {
        /* public String custName, bankName, address1, address2;
        public int dateOfBirth, accountNo, PIN; */
        
        public String custFirstName, custSecondName, custPhoneNum, custEmailAdd, custStreet, custHouseNum, custCity, custCounty, custNationality, custDOB, custPlaceOfEdu;
    
    /**
      * Create a Customer with default settings for detail information.
    */
      /*  Customer()
        {
            this.custFirstName = "None"; 
            this.custSecondName  ="none";
            this.custPhoneNum = "0000000";
            this.custEmailAdd = "None";
            this.custStreet = "None"; 
            this.custHouseNum = "None";
            this.custCity = "None";
            this.custCounty = "None";
            this.custNationality = "None";
            this.custDOB = "None";
            this.custPlaceOfEdu  = "None";
            
        }*/
        /**
         * Create a customer with given data.
         */
        Customer(String custFirstName, String custSecondName, String custPhoneNum, String custEmailAdd, String custStreet, String custHouseNum, String custCity, String custCounty, String custNationality, String custDOB, String custPlaceOfEdu)
        {
            this.custFirstName = custFirstName; 
            this.custSecondName  = custSecondName;
            this.custPhoneNum = custPhoneNum;
            this.custEmailAdd = custEmailAdd;
            this.custStreet = custStreet; 
            this.custHouseNum = custHouseNum;
            this.custCity = custCity;
            this.custCounty = custCounty;
            this.custNationality = custNationality;
            this.custDOB = custDOB;
            this.custPlaceOfEdu  = custPlaceOfEdu;
            
    
        }
    
        /**
         * Output a representation of this object.
         */
        public void listAll ()    // redefined from "Object"
        {
            System.out.println("Customer Name: " + custFirstName + " " + custSecondName );
            
        }
        
    
        
        
    }

    the database class
    Code:
    import java.sql.*;
    
    public class Database
    
    {
    
    Database (Customer cust)
    
    
    
    {
    
    
    Connection con; // The connection to the database.
    
    // The following code can throw errors, so they must be caught.
    try{
    
    // First, tell Java what driver to use and where to find it.
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // For Applet
    //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    
    // Next, create a connection to your data source.
    // Specify that you are using the ODBC-JDBC Bridge.
    // And specify the data source from ODBC.
    con = DriverManager.getConnection("jdbc:odbc:eProofreadersDB");
    
    // Create an SQL statement.
    Statement stmt = con.createStatement();
    
    // Execute some SQL to create a table in your database.
    // If the table already exists, an exception is thrown!
    
    
    
    
    stmt.executeUpdate("INSERT INTO CUSTOMER VALUES ('" + cust.custFirstName + "', '" + cust.custSecondName + "', '" + cust.custPhoneNum + "', '" + cust.custEmailAdd + "', '" + cust.custStreet + "', '" + cust.custHouseNum + "', '" + cust.custCity + "','" + cust.custCounty + "', '" + cust.custNationality + "', '" + cust.custDOB + "', '" + cust.custPlaceOfEdu + "')");
     
    
    // Catch any exceptions that are thrown.
    }
    catch(ClassNotFoundException e){
    
    System.out.println(e.toString());
    }
    catch(SQLException e){
    
    System.out.println(e.toString());
    
    }
    }
    
    }
    Last edited by Niheel; Apr 16 '10, 02:37 AM. Reason: consolidate question details
  • Fr33dan
    New Member
    • Oct 2008
    • 57

    #2
    Now what exactly is your issue? Does the code throw an exception, does the data just not make it back to the server, or is the information displayed on the applet incorrect?

    If possible try and figure out where the problem is so that we don't have to figure out all of the code just to find the issue.

    Comment

    Working...