如何将 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:46:58  来源:igfitidea点击:

How to insert Java ArrayList into a MySQL table?

javamysqljdbcarraylist

提问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 setStringfor 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 Listin your table.

我猜你很困惑,因为你List<String>被称为流派,你试图按原样插入它。这行不通。您需要插入List表中的每个元素。

Assuming you have declared and initialized IDvariable and that List<String> genreis 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();
}