2013-11-23 95 views
-4

當我嘗試將ResultSet等同於String變量時,此代碼塊向我提供了SQLException。我不能將ResultSet部分獲取到字符串變量中嗎?或代碼中的任何錯誤? 而且它不會從數據庫中選擇任何結果。 Pl幫助!如何避免SQLException?

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中;必須被捕獲或聲明爲拋出: 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) 在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 在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) 在java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) 在java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) 在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.d java.awt.EventQueue $ 3(EventQueue.java:104) (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) 在java.awt.EventQueue中的$ 4.run(EventQueue.java:686) 在java.awt.EventQueue中的$ 4.run(EventQueue.java:684) 在java.security.AccessController.doPrivileged(本地方法) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java :76) 在java.awt.EventQueue.dispatchEvent(EventQueue.java:683) 在java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) 在java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163 ) 在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) 在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) 在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) 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) 在java.awt.Component.processEvent(Component.java:6270) 在java.awt.Container.processEvent(容器。 java:2229) at java.awt.Compone nt.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) 在java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) 在java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 在java.awt.Container.dispatchEventImpl( Container.java:2273) 在java.awt.Window.dispatchEventImpl(Window.java:2719) 在java.awt.Component.dispatchEvent(Component.java:4687) 在java.awt.EventQueue.dispatchEventImpl(EventQueue中。 java:713) at java.awt.EventQueue.access $ 000(EventQueue.java:104)0123在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) a噸java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) 在java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) 在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) 在java的.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) 在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) 在java.awt.EventDispatchThread.run(EventDispatchThread.java:97) 生成成功(總時間:5秒)

+0

我強烈建議[Spring JDBCTemplate](http://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-framework-reference/html/jdbc.html) – Michael

+0

除了這個問題,你不可能在代碼中使用if(position ==「Salesman」)'。研究如何在Java中進行字符串比較。 –

+0

您的代碼無法編譯,你必須激活以'抓(ERR的SQLException)部分{'... – home

回答

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,這樣你實際上可以理解什麼是怎麼回事,和瞭解你需要做的,解決它。