ODBC:JDBC + MS-Access

robinad
robinad used Ask the Experts™
on
This is my java-applet:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.sql.*;

public class SignIn extends Applet
   implements ActionListener {

   private String uid;
   private String passwd;
   private String msg = " ";
   private TextField id, pass;
   private Button login;
   private Connection conn;
   private PreparedStatement pstmt;
   private ResultSet rst;

   public void init() {
      Label idp = new Label("User ID:", Label.RIGHT);
      Label passp = new Label("PIN:", Label.RIGHT);
      id = new TextField(9);
      pass = new TextField(9);
      pass.setEchoChar('*');
      login = new Button("Login");

      // Add user ID and password to the applet.
      add(idp);
      add(id);
      add(passp);
      add(pass);
      add(login);


     // Register to receive action events.
     id.addActionListener(this);
     pass.addActionListener(this);
     login.addActionListener(this);
   }

   // This will select ID_Number and PIN from Customer table.
   private boolean connectToDb() {
      boolean success = false;

      try
      {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        conn = DriverManager.getConnection("jdbc:odbc:angie", uid, passwd);                                           success = true;
      }
      catch (ClassNotFoundException e)
      {
        e.printStackTrace();
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
      return success;
     }  // End of connectToDb().

        public boolean validateUser()
        {
          uid = id.getText();
          passwd = pass.getText();
          boolean success = false;

          try
          {
            pstmt = conn.prepareStatement("select ID_Number, PIN from Customer");
            rst = pstmt.executeQuery();

            while (rst.next())
            {
              String dbId = rst.getString(1);
              String dbPswd = rst.getString(2);

              if(uid.equals(dbId) && passwd.equals(dbPswd))
                success = true;
            }
       }
       catch (SQLException e)
       {
         e.printStackTrace();
       }
       return success;
       }

   // User pressed Login.
   public void actionPerformed(ActionEvent ae) {
      String str = ae.getActionCommand();
         if(str.equals("Login")) {
             if (connectToDb() == true && validateUser() == true) {
               msg = "You have successfully logged in.";
             }
             else {
               msg = "You have entered an invalid User ID/PIN.";
             }
             repaint();
         }
      }

    // Prints the string to the screen.
    public void paint (Graphics g) {
       g.drawString(msg, 6, 100);
    }
}


When I start it, I get these errors on the Java-console:

at sun.applet.AppletSecurity.checkPackageAccess   (AppletSecurity.java:166)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:109)
at java.lang.ClassLoader.loadClass(ClassLoader.java:262)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:322)
at java.lang.Class.forName0(Native method)
at java.lang.Class.forName(Class.java:130)
at SignIn.connectToDb(SignIn.java:58)
at SignIn.actionPerformed(SignIn.java:103)
at java.awt.Button.processActionEvent(Button.java:381)
at java.awt.Button.processEvent(Button.java:350)
at java.awt.Component.dispatchEventImpl(Component.java:3526)
at java.awt.Component.dispatchEvent(EventQueue.java:3367)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:130)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)

Can someone help me with this one.

PS: the name of the Access-db is = angie.mdb
    the name of the table is angie1


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
There are security restrictions when the 'untrusted' applet tries to access to local disk. To overcome this problem, you have to make your untrusted applet to be a signed applet. There is a guideline on doing this in the following urls:
http://java.sun.com/products/plugin/1.2/docs/signed.html
http://java.sun.com/security/signExample12/

Hope it helps....

Author

Commented:
Thansk for the info.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial