JDBC Applet cannot run

Here is the code:

import java.awt.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.URLConnection;

public class LoginApplet extends Applet  implements ActionListener
{
      //Graphical User Interface
      //Labels, TextFields, Buttons and the TextArea
      private      Label              noLabel;
      private TextField      noTextField;
      private      Label              passLabel;
      private TextField      passTextField;
      private Button            clearButton, loginButton;
      
      //JDBC      
      private String id;                                          // User ID
      private String password = null;              // User Password
      private String sql = null;                           // SQL for the queries
      private String rst = null;                          // Results String based on the Column Name
      private StringBuffer buf;                                    // Buffer to store the data
      private      Connection con;                                          // Connection to the Datasource
      private Driver d;                                                            // Driver for the JDBC connection
      private Enumeration en;                                          // Driver
      private Statement stmt = null;                  // Statements for the SQL queries    
      private SQLWarning warning = null;      // SQL warning
      private ResultSet results = null;            // Results that were retrieved

      public void init()
      {
            resize(280,100);
            // Border Layout as overall
                  // NORTH
                        // Panel FlowLayout LEFT
                              // Panel GridLayout Labels
                              // Panel GridLayout TextFields
                        // Panel FlowLayout RIGHT
                              // Panel GridLayout Labels
                              // Panel GridLayout TextFields
                  // CENTER
                        // Panel FlowLayout CENTER
            
            Panel mainPanel = new Panel();
            mainPanel.setLayout(new BorderLayout());
            
            Panel topPanel = new Panel();
            topPanel.setLayout(new FlowLayout());
            
            Panel labelPanel = new Panel();
            labelPanel.setLayout(new GridLayout(2,0,10,5));
            
            Panel textfieldPanel = new Panel();
            textfieldPanel.setLayout(new GridLayout(2,0,10,5));
            
            Panel buttonPanel = new Panel();
            buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
            
            noLabel = new Label(" Staff / Admission No. : ");
            labelPanel.add(noLabel);
            noTextField = new TextField(10);
            textfieldPanel.add(noTextField);

            passLabel = new Label(" Password : ");
            labelPanel.add(passLabel);
            passTextField = new TextField(5);
            passTextField.setEchoChar('*');
            textfieldPanel.add(passTextField);

            loginButton = new Button("Login");
            buttonPanel.add(loginButton);
            loginButton.addActionListener(this);
            
            clearButton = new Button("Cancel");
            buttonPanel.add(clearButton);
            clearButton.addActionListener(this);
            
            topPanel.add(labelPanel);
            topPanel.add(textfieldPanel);
            mainPanel.add(topPanel, (BorderLayout.NORTH));
            mainPanel.add(buttonPanel, (BorderLayout.CENTER));
            add(mainPanel);
      }
      /*
      public void start()
      {
            LoginApplet applet = new LoginApplet();
            JFrame login = new JFrame("Login"); //set the frame name
            login.setContentPane(applet);
            login.setVisible(true);
      }
      */
      public void actionPerformed(ActionEvent ae)  
      {
            if (ae.getSource().equals(clearButton)) // button clicked is the cancel button
            {
                  noTextField.setText("");
                  passTextField.setText("");
            }
            else      // button clicked is the login button
            {
                  String no = noTextField.getText();
                  String pass = passTextField.getText();
                  //char pass[] = passTextField.getText();
                  if (no.equals("") == true)  // the staff/admission no textfield is empty
                  {
                        //loginTextArea.append("Please enter the Staff / Admission No.\n");
                        System.out.println("Please enter the Staff / Admission No.\n");
                        noTextField.requestFocus();
                  }
                  else if (pass.equals("") == true)  // the password textfield is empty
                  {
                        //loginTextArea.append("Please enter the Password\n");
                        System.out.println("Please enter the Password\n");
                        passTextField.requestFocus();
                  }
                  else  // Proceed to the next screen
                  {
                        //loginTextArea.append("Please wait.\n");
                        System.out.println("Please wait.\n");
                        LoginApplet database = new LoginApplet(no, pass);
                        // Check if the person exists in the database
                        if (database.checkDatabase() == true)
                        {
                              System.out.println("YES!");
                              //employee = new EmployeeApplet(database);      // create the Employee GUI
                              //employee.setVisible(true);                                    
                        }
                        else
                        { // The person does not exists in the database
                              System.out.println("Error! There is no such person!");
                              noTextField.setText("");
                              passTextField.setText("");
                              database.closeStatement();
                              database.closeConnection();
                              database.deregisterDriver();
                        }
                  }
            }
      }

      
      // Register the Driver that will be used
      // Get the Connection Required to the ODBC datasource
      // Check for any Connection Warnings
      // Create the Statement
      
      public LoginApplet(String id, String password)
      {
        this.id = id;
            this.password = password;
            registerDriver();
            getConnection();
            getConnectionWarnings();
      }
      
      // Check the database for the particular staff or student
      public boolean checkDatabase()
      {
            int rows=-1;
            stmt = this.createStatement();   // create statement for the query
            results = this.executeQuery("select \"Employee ID\" from " + "\"" + "Employee Brief" + "\"" +  " ;");
            
            while (rows != 0)
            {
                  try
                  {
                        results.last();
                        rows = results.getRow();
                        results.first();
                              if ((results.getString("Employee ID")).equals(/*this.getId()*/id) == true)
                              {
                                    return true;
                              }
                        rows = rows - 1;
                  }
                  catch(SQLException ex)
                  {
                        System.out.println("SQLException: " +ex);
                  }       
            }
                  return false;
      }

      // Get the Driver Used
      public Driver getDriver()
      {
            return d;
      }
            
      // Register the Driver to be Used
      public void registerDriver()
      {
            // REGISTER DRIVER
            try
            {
          d = (Driver)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
                  //loginTextArea.append("JDBC-ODBC Driver registered successfully.\n");
                  System.out.println("JDBC-ODBC Driver registered successfully.\n");
            }
                        
            catch (Exception e)
            {
          System.out.println(e);
                  //loginTextArea.append(e.toString());
            }
      }
      
      //Deregister the Driver Used
      public void deregisterDriver()
      {
        // DEREGISTER DRIVER
            try
            {
          DriverManager.deregisterDriver(getDriver());
                  //loginTextArea.append("JDBC-ODBC Driver deregistered successfully.\n");         
                  System.out.println("JDBC-ODBC Driver deregistered successfully.\n");         
            }
            
            catch (Exception e)
            {
          System.out.println(e);
                  //loginTextArea.append(e.toString());
            }
      }
      
      // Get the Connection Warnings
      public void getConnectionWarnings()
      {
                  // GET CONNECTION WARNINGS
                  try
                  {
                warning = con.getWarnings();
                        
                if (warning == null)
                        {
              System.out.println("No Warnings");
                              //loginTextArea.append("No Warnings.\n");
              return;
                      }

                while (warning != null)
                        {
                              //loginTextArea.append("Warnings." + warning);
                              System.out.println("Warnings." + warning);
              warning = warning.getNextWarning();
                }
                  }
                  
                  catch (Exception e)
                  {
                System.out.println(e);
                        //loginTextArea.append(e.toString());
                  }
      }

      // Get the Connection
      public void getConnection()
      {
            // GET CONNECTION
            try
            {
          con = DriverManager.getConnection("jdbc:odbc:http//cvesc-labpc09:8080/doc/temp",id,password);
                  //loginTextArea.append("Connection established successfully. - Datasource: temp\n");
                  System.out.println("Connection established successfully. - Datasource: temp\n");
            }
            
            catch(Exception e)
            {
          System.out.println(e);
                  //loginTextArea.append(e.toString());
            }
      }
      
      // Close Connection
      public void closeConnection()
      {
            // CLOSE CONNECTION
            try
            {
          con.close();
                  System.out.println("Connection closed successfully. - Datasource: temp\n");
            }
            catch (Exception e)
            {
          System.out.println(e);
            }
      }
      
      // Create Statement
      public Statement createStatement()
      {
             // CREATE STATEMENT
            try
            {
          stmt = con.createStatement();
                  System.out.println("Statement created successfully.");
            }
            
            catch (Exception e)
            {
          System.out.println(e);      
            }
            return stmt;
      }
      
      // Close Statement
      public void closeStatement()
      {
            // CLOSE STATEMENT
            try
            {
          stmt.close();
                  System.out.println("Statement closed successfully.");
            }
            catch (Exception e)
            {
          System.out.println(e);
            }
      }
      
      // Execute Query based on the SQL statement
      public ResultSet executeQuery(String sql)
      {
             // EXECUTE QUERY
            try
            {
          results = stmt.executeQuery(sql);
            }
            
            catch (Exception e)
            {
          System.out.println(e);
            }
            return results;
      }
}



well i have a problem when using the appletviewer to test the applet.

Starting appletviewer for C:\LoginApplet.html
load: LoginApplet.class can't be instantiated.
java.lang.InstantiationException: LoginApplet
      at java.lang.Class.newInstance0(Native Method)
      at java.lang.Class.newInstance(Class.java:239)
      at sun.applet.AppletPanel.createApplet(AppletPanel.java:532)
      at sun.applet.AppletPanel.runLoader(AppletPanel.java:468)
      at sun.applet.AppletPanel.run(Compiled Code)
      at java.lang.Thread.run(Thread.java:479)



Besides when i try to run on a browser
the applet makes a connection back to the server that it was from.
It states a unsuitable driver.
I am using sun.jdbc.odbc.JdbcOdbcDriver
well i have already did the part on the ODBC source and create a entry in it.


Email me at opabc@rocketmail.com
Please help!
LVL 1
opabcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vladi21Commented:
which browser u use? post exception stack trace
0
Ravindra76Commented:

Hi Opabc,

Applet won't access jdbc-odbc bridge ( Type 1 jdbc driver ) . You have to sign them.

First i will give you type of JDBC drivers.

Only Type 3 and Type 4 will allow applets to access database.

There are four types of driver that Java can use to connect to a database directly:

1.The JDBC-ODBC bridge provides JDBC access via most ODBC drivers.

2.A native-API partly-Java driver converts JDBC calls into calls on the client
  API for Oracle, Sybase, Informix, DB2, or other databases.

3.A net-protocol pure-Java driver translates JDBC calls into a database-independent
   net protocol which is then translated to a proprietary database vendor network protocol
  by a server.

4.A native-protocol pure-Java driver converts JDBC calls into a proprietary database vendor
 network protocol.


A Type 3 driver can connect directly from the client to the Server and a two-tier
 type 4 driver could also do this if it connects directly across the network to the database
 server because neither of these types of driver have any dependence on software installed
 on the client.

Other types of driver require the applet to be signed in order to access local propritary
 code to get to the database.


None of these specifically require RMI, but you will need to check what support
 fetures your server has for JDBC connections.


Signing Applets:

Code signing resources:

Creating Signed, Persistent Java Applets
http://www.ddj.com/articles/1999/9902/9902h/9902h.htm 

http://www.javasoft.com/products/jdk/1.1/docs/guide/security/index.html 
http://java.sun.com/security/signExample/index.html 
http://www.verisign.com/library/guide/developer/signing/index.html 
http://www.suitable.com/Doc_CodeSigning.shtml 
http://www.securingjava.com/appdx-c/ 
http://tactika.com/realhome/javaht/java-s1.html 
http://www.fastlane.net/~tlandry/javafaq.txt 

MS:
http://www.thawte.com/support/developer/ms.html 
http://www.developer.com/journal/techworkshop/curr.html 
http://www.verisign.com/library/guide/developer/authenticode/index.html 
http://msdn.microsoft.com/library/psdk/crypto/cryptotools_6cdv.htm 
http://msdn.microsoft.com/library/psdk/crypto/portaltool_3u3p.htm 

NN:
http://developer.netscape.com/docs/manuals/signedobj/ 
http://developer.netscape.com/docs/manuals/signedobj/javadoc/Package-netscape_security.html 
http://developer.netscape.com/docs/manuals/signedobj/targets/contents.htm 
http://developer.netscape.com/support/faqs/objfaq.html 
http://developer.netscape.com/docs/manuals/deploymt/4_5PREFS.HTM 

NN: Bypass the need for a certificate
Netscape provides a way to accept a codebase as trusted (then a certificate is not needed). This can be useful during development or in a private Intranet. In the Netscape Users directory, there is a file called prefs.js. Adding the line user_pref("signed.applets.codebase_principal_support", true);

will enable JAR file without a certificate to request privileges on your machine. If you agree, it will be possible for an Applet to lauch a program, write a file on your hard disk or print on the printer. You will still have to ask for privileges in your program using the Netscape capabilites classes.
Another way is to lower general security setting to more allow more freedom when running applets locally. Add or modify the following entries in the prefs.js: user_pref("unsigned.applets.low_security_for_local_classes", true);
user_pref("signed.applets.local_classes_have_30_powers", true);
user_pref("signed.applets.low_security_for_local_classes", true);
user_pref("signed.applets.verbose_security_exception", true);


Then you don't need to asked for privileges for local classes.
When adding or modifying the file prefs.js, Netscape must not be running because your modification will be overwritten. So shut down Netscape, edit the prefs.js and then restart Netscape.
 

Note:

All the above information is combination of

Experts JOD,Vladi21,sqmos

BEst of luck

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ravindra76Commented:
Hi,

ANother problem,

 you are constructing applet with parameter constructor.

Since your class extends Applet ( Base class ).

Base class will ne constructed before derived class.

Base class has no constructor taking
Applet(String,String).

So you can't instantiate applet like that.

IF you use parameter less constructor
in your class also,
Before init,Applet constructor will be executed .

Best of luck
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.