gpt4 book ai didi

java - 将 SQL 数据库中的表值插入到 ArrayList

转载 作者:行者123 更新时间:2023-11-29 07:09:27 25 4
gpt4 key购买 nike

我正在尝试将我的数据库中的一些值插入到 ArrayList 中。我猜 showMeeting() 方法有问题。希望你们能理解从控制台获得的一些消息。

public ArrayList<String> showMeeting() {

ArrayList<String> output = new ArrayList<String>();
try {
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url + dbName, userName, password);
try {
Statement st = con.createStatement();
String meetID = "SELECT meetID FROM Meeting";
ResultSet rs = st.executeQuery(meetID);
while(rs.next()){
output.add(rs.toString());
}
} catch (SQLException s) {
System.out.println("Wrong sql-query");
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return output;
}
public static void main(String[] args) {
InteractWithDatabase2 test = new InteractWithDatabase2();
ArrayList<String> meetID = test.showMeeting();
String meetings = "";
for (int i = 0; i <meetID.size(); i++) {
meetings += meetID.get(i) + "\n";
}
System.out.println(meetings);

}

当我尝试在 Eclipse 中运行它时,我收到以下消息:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at no.ntnu.fp.model.InteractWithDatabase2.visMoter(InteractWithDatabase2.java:107) at no.ntnu.fp.model.InteractWithDatabase2.main(InteractWithDatabase2.java:127)

最佳答案

您的代码似乎没有任何问题,您只是在类路径中缺少 mysql 连接器 .jar。

可以从the download page下载或者通过 Maven:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>

关于java - 将 SQL 数据库中的表值插入到 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5473040/

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