我的class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”)语句传递了Class not found异常。是否有任何驱动器我必须下载以满足数据库连接。 Pl帮忙!
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");
}
}
});
错误
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)
答案 0 :(得分:0)
实际上是一个异常处理问题。您没有处理ClassNotFoundException
例外。
将Class.forName("sun.jdbc.odbc.jdbcOdbcDriver");
置于try
catch
块中,如下所示。
try {
Class.forName("sun.jdbc.odbc.jdbcOdbcDriver");
} catch (ClassNotFoundException cnfe) {
System.out.println(cnfe);
}