我有一个类似
的数组[ BARIUM SWALLOW UPPER GI , 600.0 , 600.0 , BONE SCAN , 1250.0 , 1250.0 , BARIUM SWALLOW , 850.0 , 850.0 ]
这里我必须根据数组大小将前3个值保存在1行中,然后将下3个保存在下一行中,并且此数组不固定,可能会增加或减少。
我试过了:
String actions= req.getParameter("tvalues");
final Pattern pattern = Pattern.compile(",");
final String[] result = pattern.split(actions);
System.out.println(Arrays.toString(result));
List<String> testinfo = Arrays.asList(actions.split(","));
for (j=0;j<testinfo.size();j++) {
System.out.println(testinfo.get(j));
}
答案 0 :(得分:3)
因为我们有列表对象,所以迭代它每次迭代它将得到三个参数名称no,no1,如果你将这些参数发送到writeToDb函数。在该函数中它将更新到数据库..
list = Arrays.asList(str.split(","));
for (int i = 0; i < list.size(); i += 3) {
String name = list.get(i);
String no = list.get(i + 1);
String no1 = list.get(i + 2);
System.out.println(name + no + no1);
writeToDb(name, no, no1);
}
创建一个wirteToDb函数
public static void writeToDb(String name, String no, String no1) {
//connect to database
String insertTableSQL = "INSERT INTO DBUSER"
+ "(USERNAME, no, no1) VALUES" + "(?,?,?)";
PreparedStatement preparedStatement = dbConnection
.prepareStatement(insertTableSQL);
preparedStatement.setString(1, name);
preparedStatement.setString(2, no);
preparedStatement.setString(3, no1);
preparedStatement.executeUpdate();
}
}