如何将 Java ArrayList 插入 MySQL 表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24534630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert Java ArrayList into a MySQL table?
提问by mOna
Could someone kindly tell me how can I insert the values of Arraylist into MySQL table as a single string?
有人可以告诉我如何将 Arraylist 的值作为单个字符串插入 MySQL 表中吗?
E.g, insert "crime, drama, Action" into column "genres" of a table "featuredfilms_INFO".
例如,将“犯罪、戏剧、动作”插入表“featuredfilms_INFO”的“流派”列中。
This is a part of my code:
这是我的代码的一部分:
int i = 0;
List<String> genre = new ArrayList<String>();
.
.
.
Elements elms1 = doc.select("div.infobar");
Elements links1 = elms1.select("a[href]");
for (Element link1 : links1){
if (link1.attr("href").contains("/genre/")) {
genre.add(links1.get(i).text());
i++;
}
}
System.out.println("movie genres:" + genre);
.
.
try {
String query = "INSERT into featuredfilms_INFO (movieId, genres)" + "VALUES (?, ?)";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString (1, ID);
preparedStmt.setString (2, genre);
.
.
.
}
My problem is that I cannot use setString
for genresince it is not of type string. I'm a bit confused since at the beginning I defined genre as string but after some search I found that in Java for dynamic arrays I have to use ArrayList
.
我的问题是我不能setString
用于流派,因为它不是字符串类型。我有点困惑,因为一开始我将流派定义为字符串,但经过一番搜索后,我发现在 Java 中,对于动态数组,我必须使用ArrayList
.
Can someone explain me in detail what should I do and why?
有人可以详细解释我应该做什么以及为什么吗?
采纳答案by Erich Kitzmueller
Just join all the strings before inserting.
只需在插入之前加入所有字符串。
String comma="";
StringBuilder allGenres = new StringBuilder();
for (String g: genre) {
allGenres.append(comma);
allGenres.append(g);
comma = ", ";
}
String query = "INSERT into featuredfilms_INFO (movieId, genres)" + "VALUES (?, ?)";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString (1, ID);
preparedStmt.setString (2, allGenres.toString());
Maybe even
甚至会
preparedStmt.setString (2, genre.toString());
would even be good enough, but the above solution gives you more freedom how to join the genres.
甚至会足够好,但上述解决方案让您更自由地加入流派。
回答by Luiggi Mendoza
I guess you're confused because your List<String>
is called genre and you try to insert it as is. This won't work. You need to insert every element from the List
in your table.
我猜你很困惑,因为你List<String>
被称为流派,你试图按原样插入它。这行不通。您需要插入List
表中的每个元素。
Assuming you have declared and initialized ID
variable and that List<String> genre
is filled with the right values, then you should only traverse the list and insert every genre:
假设您已经声明并初始化了ID
变量并且List<String> genre
填充了正确的值,那么您应该只遍历列表并插入每个流派:
String query = "INSERT into featuredfilms_INFO (movieId, genres)" + "VALUES (?, ?)";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString (1, ID);
for (String realGenre : genre) {
preparedStmt.setString (2, realGenre);
preparedStmt.executeUpdate();
}