0

My class.forName("sun.jdbc.odbc.JdbcOdbcDriver") statement passes Class not found exception. Are there any drives i have to download to satisfy the database connection. Pl help !

public void actionPerformed(ActionEvent e)

        {
        String unm = tf1.getText().toString();
        String pwd = new String(tf2.getPassword());

        try{

            Connection con;
            Statement stmt;
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String cn = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=E:/userlogin.mdb";
            con = DriverManager.getConnection(cn,"","");
            stmt = con.createStatement();
            String sql = "select * from userlogin.users where users.username ='"+unm+"' and users.pwd ='"+pwd+"' ";
            ResultSet rs = stmt.executeQuery(sql); 

            String position="";

            while(rs.next())
            {
            position = rs.getString(4);
            }                  


            if (position.equals("Salesman"))
                {
               tf1.setText("hello");
                }

            if (position.equals("Supervisor"))
                {
               frame.setVisible(false);
               //Supervisor.main(null);
                }


                stmt.close();
            }

        catch (HeadlessException err) {
           JOptionPane.showOptionDialog(null,
                            "HeadlessException !",
                            "Error !",
                            JOptionPane.OK_CANCEL_OPTION,
                            JOptionPane.INFORMATION_MESSAGE,
                            null,
                            new String[]{"Ok", "Cancel"}, // this is the array
                            "default");
        }   

        catch (ClassNotFoundException err) {
        JOptionPane.showOptionDialog(null,
                "ClassNotFoundException !",
                "Error !",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                new String[]{"Ok", "Cancel"}, // this is the array
                "default");

        } 
         catch (SQLException err) {
            JOptionPane.showOptionDialog(null,
                    err.getMessage(),
                    "Error !",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    new String[]{"Ok", "Cancel"}, // this is the array
                    "default");

        } 
        }



}); 

ERROR

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
    at oms.OMS$1.actionPerformed(OMS.java:57)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6527)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6292)
    at java.awt.Container.processEvent(Container.java:2235)
    at java.awt.Component.dispatchEventImpl(Component.java:4883)
    at java.awt.Container.dispatchEventImpl(Container.java:2293)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4877)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2279)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 5 seconds)
SpringLearner
  • 13,195
  • 20
  • 69
  • 111
sachithragt
  • 88
  • 1
  • 5
  • I have no experience with this, but this link might be useful - http://docs.oracle.com/javase/1.5.0/docs/guide/jdbc/getstart/bridge.doc.html (obtaining a driver) – Vineet Kosaraju Dec 02 '13 at 01:42
  • Have you checked that your jdbcodbc jar is in classpath ?? please post stack trace of the error along with your code. – Bilbo Baggins Dec 02 '13 at 02:01
  • Which java version? AFAIK JDBC ODBC is only available in Oracle Java, not included in other Java distributions like OpenJDK, and will be removed from Java 8. – Mark Rotteveel Dec 02 '13 at 15:09

1 Answers1

0

Actually its an exception handling issue. You did not handled the ClassNotFoundException exception.

Put your Class.forName("sun.jdbc.odbc.jdbcOdbcDriver"); in try catch block as shown below.

try {
    Class.forName("sun.jdbc.odbc.jdbcOdbcDriver");
} catch (ClassNotFoundException cnfe) {
    System.out.println(cnfe);
}
Tahir Yasin
  • 10,631
  • 5
  • 38
  • 58