Link to home
Start Free TrialLog in
Avatar of opabc
opabc

asked on

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!
Avatar of vladi21
vladi21

which browser u use? post exception stack trace
ASKER CERTIFIED SOLUTION
Avatar of Ravindra76
Ravindra76

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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