2013-04-22 71 views
4

您好,我正在嘗試檢索兩個結果集中的數據。然後,我有店在這兩個結果集到字符串數組,這個過程後,我有我的字符串數組存儲到另一個表如何使這個請你告訴我..如何使用java在mySql中的表列中存儲數組值的值

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"); 
} 

請告訴我,我應該怎麼我的陣列存儲Temp表?我的「溫度」表有列subject我想存儲myArraysubject列...請告訴我如何做到這一點。

回答

3

嘗試這種在RS獲得結果集後:

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

在這個表將存儲? – Kapil 2013-04-22 10:44:44

+1

我有我的臨時表中的「主題」列..我想存儲我的數組到這個列怎麼辦? – Kapil 2013-04-22 10:46:36

+0

查詢中提到的臨時表 – 2013-04-22 10:46:37

1

您只是想迭代ArrayList中。你有相應的主題列表。

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"); 
     } 
    } 
} 
0
PreparedStatement ps = 
    (PreparedStatement) con.prepareStatement("insert into temp values(?)"); 

j=0; 
while(rs.next()) 
{ 
    ps.setString(1,anArray[j]); 
    ps.executeUpdate(); 
    j++; 
} 
+1

但我想存儲我的數組到表列我已經寫下我的查詢如下是完美的嗎?我的表名是temp_testimonial,列名稱是我想要將myArray值存儲到「Subject」的主題 \t \t PreparedStatement st =(PreparedStatement)con.prepareStatement(「Insert into Testi_TestimonialTemp(subject)values(?)」); \t \t i = 0; \t \t而(rs.next()) \t \t { \t \t st.setString(1,anArray [I]); \t \t st.executeUpdate(); \t \t i ++; \t \t} \t \t \t \t \t} – Kapil 2013-04-22 11:00:16

+0

PreparedStatement的PS =(PreparedStatement的)con.prepareStatement( 「插入temp_testimonial值()?」); I = 0; while(rs.next()){st.setString(1,anArray [i]); st.executeUpdate();我++; }} wtite?在主題領域 – piyush 2013-04-22 11:52:54

相關問題