这是我在运行程序时遇到的运行时错误(我将MS Access用作数据库)。
SQl Exception
java.sql.SQLException: No suitable driver found for jdbc:odbc:Driver={Microsoft
Access Driver (*.mdb)};DBQ=PayRoll.mdb;DriverID=22;READONLY=true)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at clsConnection.setConnection(clsConnection.java:35)
at LoginFrame.login(LoginFrame.java:122)
at LoginFrame.actionPerformed(LoginFrame.java:106)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
SQl Exception
java.sql.SQLException: No suitable driver found for jdbc:odbc:Driver={Microsoft
Access Driver (*.mdb)};DBQ=PayRoll.mdb;DriverID=22;READONLY=true)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at clsConnection.setConnection(clsConnection.java:35)
at LoginFrame.login(LoginFrame.java:122)
at LoginFrame.actionPerformed(LoginFrame.java:106)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我有“MakeDB.ini”文件,其中包含以下代码:
jdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver
jdbc.url=jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=PayRoll.mdb;DriverID=22;READONLY=true)
jdbc.username=""
jdbc.password=""
在这种情况下,数据源(odbc)中应该是“DATA SOURCE NAME”
这是我的代码:LoginFrame.java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
import java.util.Date;
public class LoginFrame extends JFrame implements ActionListener {
static JFrame frame;
private String username;
private String password;
private static JFrame loginFrame;
private static JPanel panel1;
private static JPanel panel2;
private static JPanel panel3;
private JButton loginBtn;
private JButton exitBtn;
int dialogtype = JOptionPane.PLAIN_MESSAGE;
String dialogmessage;
String dialogs;
private JLabel nameLbl;
private JLabel userLbl;
private JLabel passwordLbl;
private static JTextField userTxt;
private static JPasswordField passwordTxt;
public String loginname;
public String loginpass;
// class Veriables
clsConnection connect = new clsConnection();
//Connection variable
Connection conn;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
static Date td = new Date();
public LoginFrame()
{
panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
nameLbl = new JLabel("WELCOME TO GEC'S PAYROLL SYSTEM");
panel2 = new JPanel();
panel2.setLayout(new GridLayout(2,2));
userLbl = new JLabel("Username :");
userTxt = new JTextField(20);
passwordLbl = new JLabel("Password :");
passwordTxt = new JPasswordField(20);
panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
loginBtn = new JButton("Login", new ImageIcon("images/key.gif"));
loginBtn.addActionListener(this);
exitBtn = new JButton("Exit", new ImageIcon("images/Keys.gif"));
exitBtn.addActionListener(this);
panel1.add(nameLbl);
panel1.setOpaque(true);
panel2.add(userLbl);
panel2.add(userTxt);
panel2.add(passwordLbl);
panel2.add(passwordTxt);
panel2.setOpaque(true);
panel3.add(loginBtn);
panel3.add(exitBtn);
panel3.setOpaque(true);
frame = new JFrame("PayRoll User Login...");
frame.setSize(300,200);
Container pane = frame.getContentPane();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
//pane.setLayout(new GridLayout(3,1));
pane.add(panel1);
pane.add(panel2);
pane.add(panel3);
frame.setLocation((screen.width - 500)/2,((screen.height-350)/2));
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if(source.equals(loginBtn))
{
login();
}
else if(source.equals(exitBtn))
{
System.exit(0);
}
}
public void login()
{
loginname = userTxt.getText().trim();
loginpass = passwordTxt.getText().trim();
try {
conn = connect.setConnection(conn,"","");
}
catch(Exception e)
{
}
try{
Statement stmt = conn.createStatement();
String query = "SELECT * FROM Login WHERE USERNAME='" + loginname +
"'AND PASSWORD='"+loginpass+"'";
ResultSet rs = stmt.executeQuery(query);
boolean recordfound = rs.next();
if (recordfound)
{
dialogmessage = "Welcome - " +loginname;
dialogtype = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog((Component)null, dialogmessage, dialogs, dialogtype);
userTxt.setText("");
passwordTxt.setText("");
frame.setVisible(false);
frame.dispose();
MainMenu menu = new MainMenu(loginname,td);
}
else
{
dialogmessage = "Login Failed!";
JOptionPane.showMessageDialog(null, "INVALID ID OR PASSWORD!",
"WARNING!!",JOptionPane.WARNING_MESSAGE);
userTxt.setText("");
passwordTxt.setText("");
}
conn.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"GENERAL EXCEPTION", "WARNING!!!",JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args)
{
LoginFrame frame1 = new LoginFrame();
frame1.addNotify();
frame1.pack();
}
}
clsConnection.java
import java.io.IOException;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JDialog;
import java.io.*;
import java.util.*;
import java.net.*;
public class clsConnection {
String url = "";
String username = "";
String password = "";
public Connection setConnection(Connection conn, String username, String password )
{
try
{
Properties props = new Properties();
String fileName = "MakeDB.ini";
FileInputStream in = new FileInputStream(fileName);
props.load(in);
String drivers = props.getProperty("jdbc.drivers");
if(drivers != null)
System.setProperty("jdbc.drivers", drivers);
url = props.getProperty("jdbc.url");
username = props.getProperty("jdbc.username");
password = props.getProperty("jdbc.password");
conn = DriverManager.getConnection(url,username,password);
}catch(SQLException e)
{
System.err.println("SQl Exception");
e.printStackTrace();
}
catch(IOException e)
{
System.out.println("\nIO Exception");
}
catch (Exception e)
{
System.out.println("\nAnother Error");
}
return conn;
}
}
如何在控制面板中连接到数据库>>管理>>数据源odbc>>在这种情况下将是什么dsn