473,396 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.sql.*;
  5.  
  6.  
  7. public class AppletCust extends JApplet implements ActionListener
  8. {
  9.     public static JTextField display;
  10.  
  11.  
  12.     public static JTextField textFirstName, textSecondName, textPhoneNum, textEmailAdd, textStreet, textHouseNum, textCity, textCounty, textNationality, textDOB, textPlaceOfEdu;
  13.  
  14.     public void init()
  15.     {
  16.  
  17.         Container contentPane = getContentPane();
  18.         contentPane.setLayout(new BorderLayout(12,12));
  19.  
  20.         JPanel centerPanel = new JPanel(new GridLayout(1, 2));
  21.         JLabel prompt = new JLabel("Display Messages:");
  22.         centerPanel.add(prompt);
  23.         display = new JTextField("Please Fill in All Details to register");
  24.         display.setEditable(false);
  25.         centerPanel.add(display);
  26.  
  27.         contentPane.add(centerPanel, BorderLayout.NORTH);
  28.  
  29.  
  30.         JPanel customerPanel = new JPanel(new GridLayout(11, 2));            
  31.             addLabel(customerPanel, "First Name ");
  32.             textFirstName = new JTextField("");
  33.             customerPanel.add(textFirstName);
  34.             addLabel(customerPanel, "Second Name ");
  35.             textSecondName = new JTextField("");
  36.             customerPanel.add(textSecondName);
  37.             addLabel(customerPanel, "Phone Number ");
  38.             textPhoneNum = new JTextField("");
  39.             customerPanel.add(textPhoneNum);
  40.             addLabel(customerPanel, "Email Address ");
  41.             textEmailAdd = new JTextField("");
  42.             customerPanel.add(textEmailAdd);
  43.             addLabel(customerPanel, "Street Name ");
  44.             textStreet = new JTextField("");
  45.             customerPanel.add(textStreet);
  46.             addLabel(customerPanel, "House number ");
  47.             textHouseNum = new JTextField("");
  48.             customerPanel.add(textHouseNum);
  49.             addLabel(customerPanel, "City ");
  50.             textCity = new JTextField("");
  51.             customerPanel.add(textCity);
  52.             addLabel(customerPanel, "County ");
  53.             textCounty = new JTextField("");
  54.             customerPanel.add(textCounty);
  55.             addLabel(customerPanel, "Nationality ");
  56.             textNationality = new JTextField("");
  57.             customerPanel.add(textNationality);
  58.             addLabel(customerPanel, "Date of Birth ");
  59.             textDOB = new JTextField("");
  60.             customerPanel.add(textDOB);
  61.             addLabel(customerPanel, "Place of Education ");
  62.             textPlaceOfEdu = new JTextField("");
  63.             customerPanel.add(textPlaceOfEdu);
  64.  
  65.  
  66.  
  67.             contentPane.add(customerPanel, BorderLayout.CENTER);
  68.  
  69.         JPanel buttonPanel = new JPanel();
  70.  
  71.         JButton mercury = new JButton("Register");
  72.         mercury.addActionListener(this);
  73.         buttonPanel.add(mercury);
  74.  
  75.         contentPane.add(buttonPanel, BorderLayout.SOUTH);
  76.  
  77.     }
  78.  
  79.  
  80.  
  81.  
  82.      private void addLabel(Container panel, String labelText)
  83.     {
  84.         JLabel label = new JLabel(labelText);
  85.         panel.add(label);
  86.     }
  87.  
  88.     public void actionPerformed(ActionEvent event)
  89.     {
  90.         String command = event.getActionCommand();
  91.  
  92.         if(command.equals("Register"))
  93.         createCustomerRecord();
  94.  
  95.     } 
  96.  
  97.         public void createCustomerRecord()
  98.     {
  99.  
  100.  
  101.         String custFirstName = textFirstName.getText();
  102.         String custSecondName = textSecondName.getText();
  103.         String custPhoneNum = textPhoneNum.getText();
  104.         String custEmailAdd = textEmailAdd.getText();
  105.         String custStreet = textStreet.getText();
  106.         String custHouseNum = textHouseNum.getText();
  107.         String custCity = textCity.getText();
  108.         String custCounty = textCounty.getText();
  109.         String custNationality = textNationality.getText();
  110.         String custDOB = textDOB.getText();
  111.         String custPlaceOfEdu = textPlaceOfEdu.getText();
  112.         redisplay();
  113.         display.setText("Registered: ");
  114.         Database(custFirstName, custSecondName, custPhoneNum, custEmailAdd, custStreet, custHouseNum, custCity, custCounty, custNationality, custDOB, custPlaceOfEdu);  
  115.  
  116.  
  117.     }   
  118.  
  119.         public void redisplay()
  120.     {
  121.         textFirstName.setText("");
  122.         textFirstName.setEditable(false);
  123.         textSecondName.setText("");
  124.         textSecondName.setEditable(false);
  125.         textPhoneNum.setText("");
  126.         textPhoneNum.setEditable(false);
  127.         textEmailAdd.setText("");
  128.         textEmailAdd.setEditable(false);
  129.         textStreet.setText("");
  130.         textStreet.setEditable(false);
  131.         textHouseNum.setText("");
  132.         textHouseNum.setEditable(false);
  133.         textCity.setText("");
  134.         textCity.setEditable(false);
  135.         textCounty.setText("");
  136.         textCounty.setEditable(false);
  137.         textNationality.setText("");
  138.         textNationality.setEditable(false);
  139.         textDOB.setText("");
  140.         textDOB.setEditable(false);
  141.         textPlaceOfEdu.setText("");
  142.         textPlaceOfEdu.setEditable(false);
  143.     }
  144.  
  145.  
  146.  
  147.     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)
  148.     {
  149.     Customer C1 = new Customer(custFirstName, custSecondName, custPhoneNum, custEmailAdd, custStreet, custHouseNum, custCity, custCounty, custNationality, custDOB, custPlaceOfEdu);
  150.     Database D1 =  new Database(C1);
  151.  
  152.  
  153.     display.setText("Registered:" + custFirstName);
  154.  
  155.  
  156.     }
  157.  
  158.     public void Database (Customer cust)
  159.     {
  160.  
  161.     Connection con; // The connection to the database.
  162.  
  163.     // The following code can throw errors, so they must be caught.
  164.     try{
  165.  
  166.     // First, tell Java what driver to use and where to find it.
  167.  
  168.     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  169.     // Next, create a connection to your data source.
  170.     // Specify that you are using the ODBC-JDBC Bridge.
  171.     // And specify the data source from ODBC.
  172.  
  173.     con = DriverManager.getConnection("jdbc:odbc:eProofreadersDB");
  174.  
  175.     // Create an SQL statement.
  176.     Statement stmt = con.createStatement();
  177.  
  178.     // Execute some SQL to create a table in your database.
  179.     // If the table already exists, an exception is thrown!
  180.  
  181.     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 + "')");
  182.     // Catch any exceptions that are thrown.
  183.     }
  184.  
  185.  
  186.     catch(ClassNotFoundException e){
  187.  
  188.     System.out.println(e.toString());
  189.     }
  190.     catch(SQLException e){
  191.  
  192.     System.out.println(e.toString());
  193.  
  194.     }
  195.     }
  196. }
the customer class
Expand|Select|Wrap|Line Numbers
  1. import java.util.Random;
  2.  
  3. public class Customer
  4.  
  5. {
  6.     /* public String custName, bankName, address1, address2;
  7.     public int dateOfBirth, accountNo, PIN; */
  8.  
  9.     public String custFirstName, custSecondName, custPhoneNum, custEmailAdd, custStreet, custHouseNum, custCity, custCounty, custNationality, custDOB, custPlaceOfEdu;
  10.  
  11. /**
  12.   * Create a Customer with default settings for detail information.
  13. */
  14.   /*  Customer()
  15.     {
  16.         this.custFirstName = "None"; 
  17.         this.custSecondName  ="none";
  18.         this.custPhoneNum = "0000000";
  19.         this.custEmailAdd = "None";
  20.         this.custStreet = "None"; 
  21.         this.custHouseNum = "None";
  22.         this.custCity = "None";
  23.         this.custCounty = "None";
  24.         this.custNationality = "None";
  25.         this.custDOB = "None";
  26.         this.custPlaceOfEdu  = "None";
  27.  
  28.     }*/
  29.     /**
  30.      * Create a customer with given data.
  31.      */
  32.     Customer(String custFirstName, String custSecondName, String custPhoneNum, String custEmailAdd, String custStreet, String custHouseNum, String custCity, String custCounty, String custNationality, String custDOB, String custPlaceOfEdu)
  33.     {
  34.         this.custFirstName = custFirstName; 
  35.         this.custSecondName  = custSecondName;
  36.         this.custPhoneNum = custPhoneNum;
  37.         this.custEmailAdd = custEmailAdd;
  38.         this.custStreet = custStreet; 
  39.         this.custHouseNum = custHouseNum;
  40.         this.custCity = custCity;
  41.         this.custCounty = custCounty;
  42.         this.custNationality = custNationality;
  43.         this.custDOB = custDOB;
  44.         this.custPlaceOfEdu  = custPlaceOfEdu;
  45.  
  46.  
  47.     }
  48.  
  49.     /**
  50.      * Output a representation of this object.
  51.      */
  52.     public void listAll ()    // redefined from "Object"
  53.     {
  54.         System.out.println("Customer Name: " + custFirstName + " " + custSecondName );
  55.  
  56.     }
  57.  
  58.  
  59.  
  60.  
  61. }

the database class
Expand|Select|Wrap|Line Numbers
  1. import java.sql.*;
  2.  
  3. public class Database
  4.  
  5. {
  6.  
  7. Database (Customer cust)
  8.  
  9.  
  10.  
  11. {
  12.  
  13.  
  14. Connection con; // The connection to the database.
  15.  
  16. // The following code can throw errors, so they must be caught.
  17. try{
  18.  
  19. // First, tell Java what driver to use and where to find it.
  20. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // For Applet
  21. //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  22.  
  23. // Next, create a connection to your data source.
  24. // Specify that you are using the ODBC-JDBC Bridge.
  25. // And specify the data source from ODBC.
  26. con = DriverManager.getConnection("jdbc:odbc:eProofreadersDB");
  27.  
  28. // Create an SQL statement.
  29. Statement stmt = con.createStatement();
  30.  
  31. // Execute some SQL to create a table in your database.
  32. // If the table already exists, an exception is thrown!
  33.  
  34.  
  35.  
  36.  
  37. 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 + "')");
  38.  
  39.  
  40. // Catch any exceptions that are thrown.
  41. }
  42. catch(ClassNotFoundException e){
  43.  
  44. System.out.println(e.toString());
  45. }
  46. catch(SQLException e){
  47.  
  48. System.out.println(e.toString());
  49.  
  50. }
  51. }
  52.  
  53. }
Apr 14 '10 #1
1 10381
Fr33dan
57
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.
Apr 15 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Santo Santis | last post by:
How can I make an .exe file that can insert data automatically in a SQLServer database, I can do it in C, but how can I connect to SQLServer and execute a query. All data that I have to insert...
0
by: Dinu | last post by:
Hi All I am trying to insert data within a transaction, into a Db2 database from asp.net. I have created a System DSN using ISeries Access Driver(32 bit) for Windows. I am then connecting...
1
by: Pratik Gupte | last post by:
I have created a database in .mdf format, but I am unable to insert data into its tables. Can anybody help how to insert data using ASP.Net 2.0 using SQL Server 2005 Express edition in windows...
6
by: VitaminB | last post by:
Hi Guys, I try to insert data from a form into a mysql database, but it did not work... There is no error, but the data did not pass thru the table. This is the short script:
3
by: andrewtayloruk | last post by:
I'm a newbie when it come to things php and i'm having a bit of trouble. I'm trying to insert data from an html form into a mysql database and can't get it to work. Just a few bits about my...
0
by: danishce | last post by:
Hello, I want to insert data directly into my windows form data grid and load a combobox(userid) in the 1st column of data grid,a textbox(password) in 2nd column of datagrid. The code for insert...
1
by: Doc11 | last post by:
I'm trying to allow users insert data into a database using the form view. But when I click the insert button I get this error: Server Error in '/Customer Database' Application....
2
by: Newbie19 | last post by:
I am creating a project that would insert data into a SQL database when the User placed the information into the VB application. Then at a click of a button, it would add this data into the...
1
by: dipalichavan82 | last post by:
i m using asp.net vs2005.i have gridview on page.it displays datatable values.i have insert button on page.i want to insert data in textbox into database.i m using disconnected archi. protected...
0
by: akshalika | last post by:
I am new to Biztalk. In my project we need to connect oracle database and insert data into oracle table using BizTalk project. I use WCF Adapter pack(SP2). I create biztalk project then using Consume...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.