我尝试在提交时在同一个数据库中插入多行(每行有很多字段),但无法这样做。我成功地插入了单行,但是当它出现多行时,我卡住了。
这是一个jsp页面,您可以在其中输入动态添加行数。每行没有单独的提交按钮,而是单个提交按钮以插入所有行。
1>的 projmgrsuccessful.jsp
<form name="allocate-project-task" action="AddProjectTaskDAO" method="get" enctype="multipart/form-data">
<h4>Another popup</h4>
<input type="hidden" name="proj_emp_comt" value="">
<input type="hidden" name="pid" value="<c:out value="${pid}" />">
<label>Enter the no of employee involved in project</label>
<input type="text" id="textbox1" />
<input type="button" value="Add" id="add" />
<table border="1" id="pt-tabledata">
</table>
<center><input type="submit" name="alloc_time" value="Allocate"><a href="#" id="closea">Close</a></center>
</form>
<script>
$("#add").click(function() {
$('#pt-tabledata').append('<tr><td>Username</td><td>Work description</td> <td>Allocate time</td><td>Allocate work and time</td></tr>');
for (var i = 0; i < $("#textbox1").val(); i++) {
$newdiv2 = $('<tr id="pt-ip-row" style="display:block;"><td><select name="uname"><c:forEach items="${ual}" var="u"><option value="<c:out value="${u.uname}"></c:out>"><c:out value="${u.uname}"></c:out></option> </c:forEach> </select></td><td><textarea rows="5" cols="15" name="pdesc_emp"></textarea></td><td><input type="text" name="pestd_time_alloc" /></td><td></td><td><a href="#" id="close-pt-row" onclick="return hide();">Close</a></td> </tr>').after("#add");
$('#pt-tabledata').append($newdiv2);
}
});
</script>
这是java代码片段,我处理数据,如连接到数据库并将其插入数据库表。
2&GT;的 AddProjectTaskDAO.java
package com.pms.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AddProjectTaskDAO
*/
@WebServlet("/AddProjectTaskDAO")
public class AddProjectTaskDAO extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddProjectTaskDAO() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
Connection conn=null;
String url="jdbc:mysql://localhost:3306/";
String dbName="projectmanagement";
String driver="com.mysql.jdbc.Driver";
String dbUsername="root";
String dbPassword="root";
try{
String Projid=" ";
String Projuname=" ";
String Projdesc_emp=" ";
String Projestd_time_alloc=" ";
String Projtask_emp_comment=" ";
String Pid[] = request.getParameterValues("pid");
String Uname[] = request.getParameterValues("uname");
String Pdesc_emp[] = request.getParameterValues("pdesc_emp");
String Pestd_time_alloc[] = request.getParameterValues("pestd_time_alloc");
String Projtask_emp_cmnt[] = request.getParameterValues("ptask_emp_cmnt");
//String Utype = request.getParameter("utype");
for(int i=0; i<100; i++){
Projid=Pid[i]+" "+ 1;
Projuname=Uname[i]+" "+ 1;
Projdesc_emp=Pdesc_emp[i]+" "+ 1;
Projestd_time_alloc=Pestd_time_alloc[i]+" "+ 1;
Projtask_emp_comment=Projtask_emp_cmnt[i]+" "+ 1;
}
Class.forName(driver);
conn = DriverManager.getConnection(url+dbName,dbUsername, dbPassword);
Statement st=conn.createStatement();
int i = st.executeUpdate("insert into projtaskallocate (pid,uname,ptdesc_emp,ptestd_time_alloc,ptask_emp_cmnt) values ('" +Projid+ "','" +Projuname+"','" +Projdesc_emp+ "','" +Projestd_time_alloc+ "','" +Projtask_emp_comment+ "')");
String msg=" ";
if(i!=0){
UserPMDAO dd = new UserPMDAO();
request.setAttribute("ual", dd.getUsers());
ProjectDAO pp = new ProjectDAO();
request.setAttribute("pal", pp.getProjects());
String nextJSP = "/projmgrsuccessful.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);
}
else
{
msg="Failed to Add Project";
pw.print("<font size='6' color=blue>" + msg + "</font>");
}
pw.close();
//pst.close();
st.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
这里的问题是,当我点击提交按钮时,只插入第一行,但我的要求是插入所有行。
请帮助,如果我出错了,请指导我。
答案 0 :(得分:0)
您可以使用Batch语句使用jdbc
,
for (int i=0:i<100;i++) {
String query = "insert into projtaskallocate (pid,uname,ptdesc_emp,ptestd_time_alloc,ptask_emp_cmnt) values ('" +Projid+ "','" +Projuname+"','" +Projdesc_emp+ "','" +Projestd_time_alloc+ "','" +Projtask_emp_comment+ "')";
statement.addBatch(query);
}
statement.executeBatch();
statement.close();
connection.close();
希望这会有所帮助!!
答案 1 :(得分:0)
问题出在你的for循环中。从参数数组中读取的值将在每次迭代时被覆盖,变量将只包含最后一个元素值。使用以下代码。
conn = DriverManager.getConnection(url+dbName,dbUsername, dbPassword);
String query = "insert into projtaskallocate (pid,uname,ptdesc_emp,ptestd_time_alloc,ptask_emp_cmnt) values (?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(query);
for(int i=0; i<100; i++){
Projid=Pid[i]+" "+ 1;
Projuname=Uname[i]+" "+ 1;
--
--
pstmt.setString(1, Projid);
pstmt.setString(2, Projuname);
--
--
pstmt.addBatch();
}
pstmt.executeBatch();
http://www.mkyong.com/jdbc/jdbc-preparedstatement-example-batch-update/