3

您好,我正在尝试检索两个结果集中的数据。然后我在两个结果集中都存储到字符串数组,在这个过程之后我必须将我的字符串数组存储到另一个表中,所以请告诉我..

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我想存储myArray到列的subject列...请告诉我该怎么做。

4

3 回答 3

3

在 rs 中获得结果集后试试这个:

   PreparedStatement st = (PreparedStatement) con.prepareStatement("insert into temp values(?)");
    i=0;
   while(rs.next())
    {
      st.setString(1,anArray[i]);
      st.executeUpdate();
      i++;
    }
于 2013-04-22T10:14:35.970 回答
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");
        }
    }
}
于 2013-04-22T10:47:59.783 回答
0
PreparedStatement ps = 
    (PreparedStatement) con.prepareStatement("insert into temp values(?)");

j=0;
while(rs.next())
{
    ps.setString(1,anArray[j]);
    ps.executeUpdate();
    j++;
}
于 2013-04-22T10:52:32.647 回答