當我嘗試將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秒)
我強烈建議[Spring JDBCTemplate](http://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-framework-reference/html/jdbc.html) – Michael
除了這個問題,你不可能在代碼中使用if(position ==「Salesman」)'。研究如何在Java中進行字符串比較。 –
您的代碼無法編譯,你必須激活以'抓(ERR的SQLException)部分{'... – home