如何避免SQLException?

时间:2013-11-23 05:17:54

标签: java jdbc

当我尝试将ResultSet与String变量相等时,这段代码给了我SQLException。我不能将ResultSet部分放入字符串变量中吗?或代码中的任何错误? 而且它也没有从数据库中选择任何结果。 Pl帮忙!

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");

        } 
        }



}); 

它说的是:

线程中的异常" AWT-EventQueue-0" java.lang.RuntimeException:无法编译的源代码 - 未报告的异常java.sql.SQLException;必须被抓住或宣布被抛出     在javaproject.JavaProject $ 1.actionPerformed(JavaProject.java:65)     在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)     在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2341)     在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)     在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)     在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)     在java.awt.Component.processMouseEvent(Component.java:6505)     在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)     在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) 线程中的异常" AWT-EventQueue-0" java.lang.RuntimeException:无法编译的源代码 - 未报告的异常java.sql.SQLException;必须被抓住或宣布被抛出     在javaproject.JavaProject $ 1.actionPerformed(JavaProject.java:65)     在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)     在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2341)     在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)     在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)     在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)     在java.awt.Component.processMouseEvent(Component.java:6505)     在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)     在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) 建立成功(总时间:5秒)

1 个答案:

答案 0 :(得分:3)

首先,你不要与变量“相等”。你指定它。 (等于不是动词!!)

接下来,您误读了异常。它说:

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 ...

你不是“得到一个SQLException”。并且SQLException不会“出现”。

您实际拥有的是编译错误 ...您已指示IDE 忽略。不要那样做!在尝试运行应用程序之前修复编译错误。

编译错误正在发生,因为您正在调用代码中的一个或多个方法,这些方法被声明为抛出已检查的异常SQLException,但您的代码既没有捕获异常,也没有声明它被方法抛出这段代码发生的地方。

在这种特殊情况下,该方法是actionPerformed ...这需要符合您的匿名类正在实现的ActionListener接口中指定的签名。因此,您必须捕获异常。


现在我可以给你一些代码来复制并粘贴到你的程序中来修复它。但我认为如果你阅读Oracle Java Tutorial section on exceptions and exception handling会更好,你可以了解这里发生了什么,理解你需要做些什么来解决它