How to insert Java ArrayList into a MySQL table? -


could kindly tell me how can insert values of arraylist mysql table single string?

e.g, insert "crime, drama, action" column "genres" of table "featuredfilms_info".

this part of code:

int = 0; list<string> genre = new arraylist<string>(); . . . elements elms1 = doc.select("div.infobar");  elements links1 = elms1.select("a[href]"); (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 featuredfilms_info (movieid, genres)" + "values (?, ?)";      preparedstatement preparedstmt = conn.preparestatement(query);      preparedstmt.setstring (1, id);      preparedstmt.setstring (2, genre);    .    .    . } 

my problem cannot use setstring genre since not of type string. i'm bit confused since @ beginning defined genre string after search found in java dynamic arrays have use arraylist.

can explain me in detail should , why?

just join strings before inserting.

 string comma="";  stringbuilder allgenres = new stringbuilder();  (string g: genre) {     allgenres.append(comma);     allgenres.append(g);     comma = ", ";  }   string query = "insert 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 enough, above solution gives more freedom how join genres.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -