如何使用java在mySql的表列中存储数组值
Posted
技术标签:
【中文标题】如何使用java在mySql的表列中存储数组值【英文标题】:How to Store Array value in table column in mySql using java 【发布时间】:2013-04-15 04:24:44 【问题描述】:您好,我正在尝试检索两个结果集中的数据。然后我在两个结果集中都存储到字符串数组,在这个过程之后我必须将我的字符串数组存储到另一个表中,所以请告诉我..
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
表中?我的“Temp”表有subject
列我想将myArray
存储到subject
列...请告诉我该怎么做。
【问题讨论】:
【参考方案1】:PreparedStatement ps =
(PreparedStatement) con.prepareStatement("insert into temp values(?)");
j=0;
while(rs.next())
ps.setString(1,anArray[j]);
ps.executeUpdate();
j++;
【讨论】:
但我想将我的数组存储到表列中我已经编写了如下查询这样完美吗?我的表名称是 temp_testimonial,列名称是 subject 我想将 myArray 值存储到“Subject” PreparedStatement st = (PreparedStatement) con.prepareStatement("insert into Testi_TestimonialTemp(subject) values(?)");我=0; while(rs.next()) st.setString(1,anArray[i]); st.executeUpdate();我++; PreparedStatement ps = (PreparedStatement) con.prepareStatement("insert into temp_testimonial values(?)");我=0; while(rs.next()) st.setString(1,anArray[i]); st.executeUpdate();我++; 在主题领域【参考方案2】:您只需迭代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");
【讨论】:
【参考方案3】:在 rs 中得到结果集后试试这个:
PreparedStatement st = (PreparedStatement) con.prepareStatement("insert into temp values(?)");
i=0;
while(rs.next())
st.setString(1,anArray[i]);
st.executeUpdate();
i++;
【讨论】:
这将存储在哪个表中? 我的临时表中有“主题”列.. 我想将我的数组存储到该列中怎么办? PreparedStatement st = (PreparedStatement) con.prepareStatement("insert into Testi_TestimonialTemp(subject) values(?)"); 我有这张表,但我无法将 myArray 值存储到表列中 让我们continue this discussion in chat以上是关于如何使用java在mySql的表列中存储数组值的主要内容,如果未能解决你的问题,请参考以下文章
PL/SQL:如何运行存储在表列中的sql语句来获取sql语句的执行时间?
如何实现 OPENJSON 将 JSON 代码作为列中的文本。如何在 AZURE SQL Dataware House 中的表列上使用 OPENJSON?