【问题标题】:Getting the values from dynamically added rows and storing into database从动态添加的行中获取值并存储到数据库中
【发布时间】:2013-09-14 03:18:12
【问题描述】:

这是我的 jsp 代码,我通过使用带有名称细节和数量的 javascript 动态添加行,如何将这个动态添加的行值导入数据库,这里是通过创建对象的 bean 并在 servlet 端获取。

<form action="ClientBean.jsp" method="post">
 <TD>
  <input type="text" name="particulars" style="width:600px;height: 20px;">
 </TD> 
 <TD>
  <input type="text" name="amount" style="width:150px;height:20px; ">
 </TD>
</TR>
</table>
<INPUT type="button" value="Add Row" onclick="addRow('tbl_comercial')" />

这是将使用值动态创建行的 javascript 代码

function addRow(tbl_comercial){
  var table = document.getElementById(tbl_comercial);
  var rowCount = table.rows.length;
  var row = table.insertRow(rowCount);
  var cell1 = row.insertCell(0);
  var element1 = document.createElement("input");
  element1.type = "text";
  element1.name="particulars1[]";
  element1.size = 95;
  cell1.appendChild(element1);
  var cell2 = row.insertCell(1);
  var element2 = document.createElement("input");
  element2.type = "text";
  element2.name = "amount1[]";
  cell2.appendChild(element2);
}

这里是 Servlet 代码

if (uri.contains("/Qutetioninsertion.do")) {
  System.out.println("inside client");
  ClientBean rb = (ClientBean) request.getAttribute("reg");
  Qoutetion model = new Qoutetion();
  String result=model.client(rb);
  if(result.contains("success")){
    rd = request.getRequestDispatcher("qutetiongenarationform.jsp");
    request.setAttribute("successmsg", result);
    rd.forward(request, response);  
  }
  else{
    rd = request.getRequestDispatcher("loginerror.jsp");
    request.setAttribute("errormsg",result);
    rd.forward(request, response);
  }
}

这里是java代码

sql ="insert into commercial(particulars,amount) values(?,?)";
ps2 = con.prepareStatement(sql);
ps2.setString(1, rb.getParticulars());
ps2.setInt(2,rb.getAmount());
ps2.execute();
con.commit();

这是我的 bean 代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <jsp:useBean id="reg" class="com.uttara.reg.ClientBean" scope="request">
        <jsp:setProperty name="reg" property="*"/>
    </jsp:useBean>
    <jsp:forward page="Qutetioninsertion.do"/>
</body>
</html>

【问题讨论】:

  • 如何/在哪里检索表单值?
  • 通过 bean 和 servlet 并使用 sql 查询插入到表中
  • 你在哪里设置'reg'请求?
  • 我正在通过 javaBean 类设置 reg
  • 我看不到您如何提交表单。我缺少ClientBean 的代码以及如何将表单值转换为 bean。

标签: java javascript sql jsp jdbc


【解决方案1】:

您需要通过提交表单或 AJAX 调用将您的数据发送到服务器。提交表单的一种方法是 document.form.submit(假设您的文档中没有其他表单)。对于您发布的代码的 sn-ps,我无法更具体。然后在服务器端,从请求中检索参数并为每组参数(详细信息和数量)调用一次 JDBC 调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多