我想将文本框值插入数据库。但是,只有第一个文本框才能插入数据库。无法将第二个文本框值插入数据库。 (而是插入一个空白列)。
下图是我的数据库中显示的数据。如您所见,只有第一个练习显示数字“ 5”。我希望两个练习都能够查看它。
JSP代码
<%
String[] id = request.getParameterValues("assignchkbox");
String userId = request.getParameter("UserID");
String arm = request.getParameter("Arm");
String armNumber = request.getParameter("ArmNumber");
String leg = request.getParameter("Leg");
String legNumber = request.getParameter("LegNumber");
out.println("<tr>");
out.println("<td style='width: 10%'>");
out.println(userId);
out.println("</td>");
out.println("<td style='width: 10%'>");
out.println(arm);
out.println("</td>");
out.println("<td style='width: 10%'>");
out.println(armNumber);
out.println("</td>");
out.println("<td style='width: 10%'>");
out.println(leg);
out.println("</td>");
out.println("<td style='width: 10%'>");
out.println(legNumber);
out.println("</td>");
out.println("</tr>");
int count =0;
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String connURL = "jdbc:mysql://99/hi?user=00&password=000";
conn = DriverManager.getConnection(connURL);
String sql="Insert into assign(UserID, Arm ,ArmNumber,Leg,LegNumber) Values(?,?,?,?,?)";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString (1,userId);
pstmt.setString (2,arm);
pstmt.setString (3,armNumber);
pstmt.setString (4,leg);
pstmt.setString (5,legNumber);
int rec=pstmt.executeUpdate();
if (rec==1)
count++;
%>
<form action="assign.jsp" method="post">
<label><%=count%> assigned!!</label>
<td style width="50%">
<p><input type="submit" value="Return" name="ReturnBtn" /></p>
</form>
<%
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
finally{
}
%>
手臂JSP代码
<table border='4' class="f">
<tr>
<th>ID</th>
<th>Arm Exercises</th>
<th>Count</th>
<th colspan='1'>✅</th>
</tr>
<%
String sql1="";
{
sql1 = "select * from exercise1";
PreparedStatement pstmt1=conn.prepareStatement(sql1);
ResultSet rs1 = pstmt1.executeQuery();
while(rs1.next()){
String id = rs1.getString("ID");
String arm = rs1.getString("Arm");
out.println("<tr>");
out.println("<td style = 'width: 3%'>");
out.println(id);
out.println("</td>");
out.println("<td style = 'width: 90%'>");
out.println(arm);
out.println("</td>");
%>
<td>
<input name="ArmNumber" id="ArmNumber" type="text" size="2"/>
</td>
<td style="width: 5%" class="k"><input class="mychkbox" type="checkbox"
value="<%=arm%>" form="multipleAssign" name="Arm" /></td>
<%
out.println("</tr>");
}
conn.close();
}
%>
</form>
</table>
</div>
</div>
答案 0 :(得分:0)
String[] id = request.getParameterValues("assignchkbox");
String userId = request.getParameter("UserID");
String arm[] = request.getParameterValues("Arm");
String armNumber = request.getParameter("ArmNumber");
String leg = request.getParameter("Leg");
String legNumber = request.getParameter("LegNumber");
out.println("<tr>");
out.println("<td style='width: 10%'>");
out.println(userId);
out.println("</td>");
out.println("<td style='width: 10%'>");
out.println(arm);
out.println("</td>");
out.println("<td style='width: 10%'>");
out.println(armNumber);
out.println("</td>");
out.println("<td style='width: 10%'>");
out.println(leg);
out.println("</td>");
out.println("<td style='width: 10%'>");
out.println(legNumber);
out.println("</td>");
out.println("</tr>");
int count =0;
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String connURL = "jdbc:mysql://99/hi?user=00&password=000";
conn = DriverManager.getConnection(connURL);
String sql="Insert into assign(UserID, Arm ,ArmNumber,Leg,LegNumber) Values(?,?,?,?,?)";
PreparedStatement pstmt=conn.prepareStatement(sql);
for(String chk:arm){
pstmt.setString (1,userId);
pstmt.setString (2,chk);
pstmt.setString (3,armNumber);
pstmt.setString (4,leg);
pstmt.setString (5,legNumber);
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
}catch(SQLException e){
e.printStackTrace();
}
尝试一下。