gpt4 book ai didi

java - MySql 数据库连接问题 - ClassNotFoundException

转载 作者:行者123 更新时间:2023-11-29 12:33:49 28 4
gpt4 key购买 nike

我正在尝试获取用户信息,然后将其显示到 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 驱动程序。为此,您可以使用此 link

然后在 eclipse 中

  1. 右键单击您的项目
  2. 点击属性
  3. 选择java构建路径
  4. 选择库
  5. 按添加外部 jar
  6. 选择您下载的jar文件
  7. 点击“确定”

这可能会解决您的问题。

关于java - MySql 数据库连接问题 - ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27085364/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com