How to store array value in table column in mySql using java

Hi, I am trying to get data in two result sets. then I have a repository in both result sets in a String Array, after this process I have to save the String array in another table, as you so kindly tell me.

try { Class.forName("com.mysql.jdbc.Driver"); Connection con = (Connection) DriverManager. getConnection("jdbc:mysql://localhost:3306/lportal", "root", "ubuntu123"); PreparedStatement stmt = (PreparedStatement) con. prepareStatement("SELECT * FROM Testi_Testimonial where subject = ?"); stmt.setString(1, search); stmt.execute(); rs = (ResultSet) stmt.getResultSet(); while (rs.next()) { anArray[i] = rs.getString("subject"); System.out.println(anArray[i]); i++; count++; } PreparedStatement stmt1 = (PreparedStatement) con. prepareStatement("SELECT * FROM Testi_Testimonial where subject != ?"); stmt1.setString(1, search); stmt1.execute(); rs1 = (ResultSet) stmt1.getResultSet(); while (rs1.next()) { anArray[i] = rs1.getString("subject"); System.out.println(anArray[i]); i++; count++; } } catch (Exception e) { e.printStackTrace(); System.out.println("problem in connection"); } 

Please tell me how can I save my array in a Temp table? my table "Temp" has a subject column. I want to save myArray column to subject ... kindly tell me how to do this.

+4
source share
3 answers

try this after getting the results in rs:

  PreparedStatement st = (PreparedStatement) con.prepareStatement("insert into temp values(?)"); i=0; while(rs.next()) { st.setString(1,anArray[i]); st.executeUpdate(); i++; } 
+3
source

You just iterate over the List array. You have received the subject list as appropriate.

 public class TestArrayList { public static void main(String[] args) { /*create two arrayList*/ List<String> tempOneList = new ArrayList<String>(); try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vijay", "root", "root"); Statement st = con.createStatement(); ResultSet res = st.executeQuery("SELECT * FROM subjecttable"); while (res.next()) { tempOneList.add(res.getString("subject")); } ResultSet resOne = st.executeQuery("SELECT * FROM subjecttabletwo"); while (resOne.next()) { tempOneList.add(resOne.getString("subject")); } System.out.println("temp/List>>--" + tempOneList.size()); for(int i=0;i<tempOneList.size();i++){ System.out.println(tempOneList.get(i)); } } catch (Exception e) { e.printStackTrace(); System.out.println("problem in connection"); } } } 
+1
source
 PreparedStatement ps = (PreparedStatement) con.prepareStatement("insert into temp values(?)"); j=0; while(rs.next()) { ps.setString(1,anArray[j]); ps.executeUpdate(); j++; } 
0
source

All Articles