0

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)
4
  • 2
    The JDBC driver is not within the programs class path Commented Nov 23, 2014 at 3:44
  • "How do I fix that?" These are the types of things you should have figured out before attempting to write a GUI. Commented Nov 23, 2014 at 3:46
  • The depends entirely on how you are building the program and how you are running it. If you are using an IDE, you need to make sure the JDBC driver .jar file is included within the projects library classpath. You should also ensure that the IDE is building and including the project dependencies. Commented Nov 23, 2014 at 3:47
  • I am using Eclipse to compile this. Commented Nov 23, 2014 at 3:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I added the external Jar and now I am getting a different error. Which is under "New Error"
I added it to the full post. Under "New Error" at the bottom of the post.
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[Ljava.lang.Object;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.