MySql数据库连接问题 - ClassNotFoundException

时间:2014-11-23 03:42:54

标签: java mysql classpath classnotfoundexception

我正在尝试获取用户信息,然后将其显示在Swing中的表上。目前,每次执行下面的代码时,我都会收到如下错误。

错误

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at me.sage.hopkins.gui.and.mysql.Test.initialize(Test.java:53)
at me.sage.hopkins.gui.and.mysql.Test.<init>(Test.java:41)
at me.sage.hopkins.gui.and.mysql.Test$1.run(Test.java:28)
at java.awt.event.InvocationEvent.dispatch(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 Source)
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)

代码

public class Test {

private JFrame frame;
private JTable table;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Test window = new Test();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Test() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    try{
        Class.forName("com.mysql.jdbc.Driver");
    //*Mysql Configurations*\\
    Connection connect = DriverManager.getConnection("jdbc:mysql://204.44.86.142/League_Stats","root","password");
    PreparedStatement sql = connect.prepareStatement("SELECT * FROM `Champion Data`");
    ResultSet rs = sql.executeQuery();
    rs.next();
    String ChampionName = rs.getString("Champion");
    String UserName = rs.getString("User Name");

    String[] columnNames = {"Username", "Champion"};
    Object[]data = {UserName, ChampionName};
    //*Draw Table*\\
    table = new JTable((Object[][]) data, columnNames);
    GridBagConstraints gbc_table = new GridBagConstraints();
    gbc_table.insets = new Insets(0, 0, 0, 5);
    gbc_table.fill = GridBagConstraints.BOTH;
    gbc_table.gridx = 2;
    gbc_table.gridy = 6;
    frame.getContentPane().add(table, gbc_table);


    }catch(Exception e){
        e.printStackTrace();
    }
}
}

新错误

这是我在介绍JDBC库后收到的新错误。

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[Ljava.lang.Object;
at me.sage.hopkins.gui.and.mysql.Test.initialize(Test.java:65)
at me.sage.hopkins.gui.and.mysql.Test.<init>(Test.java:41)
at me.sage.hopkins.gui.and.mysql.Test$1.run(Test.java:28)
at java.awt.event.InvocationEvent.dispatch(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 Source)
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)

1 个答案:

答案 0 :(得分:1)

下载jdbc驱动程序。为此,您可以使用此link

然后在eclipse中

  1. 右键单击您的项目
  2. 点击属性
  3. 选择java构建路径
  4. 选择图书馆
  5. 按add external jars
  6. 选择已下载的jar文件
  7. 点击确定
  8. 这可能会解决您的问题。