-2

This block of code gives me SQLException when i try to equal ResultSet to a String variable. can't i get a ResultSet part into a String Variable ? or any error in the code ? And also it doesn't select any result from the database. Pl help !

b1.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e)
    {

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

        try{

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

            String position;

            rs.next();                
            position = rs.getString(1);

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

             if (rs == null)
                    {
                        JOptionPane.showOptionDialog(null,
                            "Incorrect Username or Password !",
                            "Error !",
                            JOptionPane.OK_CANCEL_OPTION,
                            JOptionPane.INFORMATION_MESSAGE,
                            null,
                            new String[]{"Ok", "Cancel"}, // this is the array
                            "default");
                    }

                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,
                    "SQLException !",
                    "Error !",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    new String[]{"Ok", "Cancel"}, // this is the array
                    "default");

        } 
        }



}); 

what it says is :

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - unreported exception java.sql.SQLException; must be caught or declared to be thrown at javaproject.JavaProject$1.actionPerformed(JavaProject.java:65) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 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:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:713) at java.awt.EventQueue.access$000(EventQueue.java:104) at java.awt.EventQueue$3.run(EventQueue.java:672) at java.awt.EventQueue$3.run(EventQueue.java:670) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:686) at java.awt.EventQueue$4.run(EventQueue.java:684) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:683) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - unreported exception java.sql.SQLException; must be caught or declared to be thrown at javaproject.JavaProject$1.actionPerformed(JavaProject.java:65) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 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:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:713) at java.awt.EventQueue.access$000(EventQueue.java:104) at java.awt.EventQueue$3.run(EventQueue.java:672) at java.awt.EventQueue$3.run(EventQueue.java:670) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:686) at java.awt.EventQueue$4.run(EventQueue.java:684) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:683) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) BUILD SUCCESSFUL (total time: 5 seconds)

sachithragt
  • 108
  • 1
  • 6

1 Answers1

3

First, you don't "equal" something to a variable. You assign it. (Equal is not a verb!!)

Next, you have misread the exception. It says:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: 
    Uncompilable source code - unreported exception java.sql.SQLException; 
    must be caught or declared to be thrown at ...

You are not "getting an SQLException". And SQLException is not "appearing".

What you actually have is a compilation error ... which you have instructed your IDE to ignore. Don't do that! Fix the compilation errors before you attempt to run your application.

And the compilation error is happening because you are calling a method or methods in your code that are declared as throwing the checked exception SQLException, but your code neither catches the exception or declares it as being thrown by the method in which this code occurs.

In this particular case, that method is actionPerformed ... which is required to conform to the signature specified in the ActionListener interface that your anonymous class is implementing. Therefore, you have to catch the exception.


Now I could give you some code to copy and paste into your program to fix it. But I think it would better if you read the Oracle Java Tutorial section on exceptions and exception handling so that you could actually understand what is going on here, and understand what you need to do to fix it.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216