【发布时间】:2015-01-21 00:22:44
【问题描述】:
我正在尝试获取用户信息,然后将其显示到 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)
【问题讨论】:
-
JDBC 驱动程序不在程序类路径中
-
“我该如何解决这个问题?” 这些是您在尝试编写 GUI 之前应该弄清楚的事情类型。
-
这完全取决于您如何构建程序以及如何运行它。如果您使用的是 IDE,则需要确保 JDBC 驱动程序 .jar 文件包含在项目库类路径中。您还应该确保 IDE 正在构建并包含项目依赖项。
-
我正在使用 Eclipse 来编译这个。
标签: java mysql classpath classnotfoundexception