MySql Database Connection Issues - ClassNotFoundEx

2019-09-08 04:56发布

问题:

I am attempting to fetch User Information, and then display it onto a Table in Swing. Currently I am getting an error as follows each time I execute the piece of code below.

Error

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)

Code

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();
    }
}
}

New Error

This is the new error I recieved after introducing the JDBC Library.

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:

Download the jdbc driver. For that you can use this link

Then in eclipse

  1. Right click your project
  2. click on properties
  3. Select java build path
  4. Select libraries
  5. press add external jars
  6. select the jar file you have downloaded
  7. click ok

This might fix your problem.