2016-03-05 100 views
0

我正在使用eclipse IDE在jsp上工作。我的项目基于员工时间管理系统。因此,我需要将员工每天工作多少小时以及相应的项目名称。如果我的表格包含一条记录,它将采集数据并妥善保存。但是我的代码不适用于多个记录。它只需要一个记录。但我的要求是将多个记录保存在表中。请告诉我如何做到这一点。我想通过jsp将我的表数据保存在mysql数据库中

**my form(user.php)** 
<form method="post" action="addhours.jsp"> 
    <table class="table table-bordered table-striped"> 
     <thead> 
      <tr> 
       <th>Project</th> 
       <th><input type="text" name="date" value="<%=d.format(cal.getTime())%>" readonly></th> 
       <th><input type="text" name="date" value="<%=d.format(cal2.getTime())%>" readonly></th> 
       <th><input type="text" name="date" value="<%=d.format(cal3.getTime())%>" readonly></th> 
       <th><input type="text" name="date" value="<%=d.format(cal4.getTime())%>" readonly></th> 
       <th><input type="text" name="date" value="<%=d.format(cal5.getTime())%>" readonly></th> 
      </tr> 
     </thead> 
     <tbody> 
      <% while (rs.next()) { %> 
      <tr> 
       <th><input type="text" name="project_name" value="<% out.println(rs.getString("project_name")); %>" readonly></th> 
       <td> 
        <input type="text" name="day1" class="form-control"> 
       </td> 
      </tr> 
      <% } %> 
     </tbody>   
    </table> 
    <div class="panel-heading no-collapse text-center" style="font-size:14px"> 
     <input type="submit" name="submit" value="Save"> 
    </div> 
</form> 


**code to save the data(addhours.php)** 
<% 
String project_id = null; 
Class.forName("com.mysql.jdbc.Driver"); 
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root",""); 
Statement st = con.createStatement(); 
String hours = request.getParameter("day1"); 
String userid = (String)session.getAttribute("userid"); 
String project_name = request.getParameter("project_name"); 
out.println(project_name); 
String date = request.getParameter("date"); 

session.setAttribute("hours", hours); 
out.println(hours); 

ResultSet rs; 
rs = st.executeQuery("select * from project_task where project_name = '"+project_name+"'"); 
if (rs.next()) 
    project_id=rs.getString("project_id"); 

if (hours != null && hours != "") { 
    int i=st.executeUpdate("insert into workhours(userid,hours,project_id,date) values ('"+userid+"','"+hours+"','"+project_id+"','"+date+"')"); 
    out.println("saved"); 
} 
%> 
+0

的你有什么期望时,你的'project_name'和'day1'领域均具有相同的名称? – m02ph3u5

+0

我期待日期字段,即day1将与相关的表标题数据一起保存,即project_name应save.only第一条记录被保存,但它没有从第二条记录中获取数据 – roja

+0

您必须为字段创建数组在你的while循环中。例如:'name =“project_name []”' - 看看这个:http://www.thefutureoftheweb.com/blog/use-arrays-with-html-form-inputs – m02ph3u5

回答

0

使用while loop代替if声明

rs=st.executeQuery("select * from project_task where project_name='"+project_name+"'"); 
while(rs.next()) 
project_id=rs.getString("project_id"); 
if(hours!=null && hours!=""){ 
int i=st.executeUpdate("insert into workhours(userid,hours,project_id,date) values ('"+userid+"','"+hours+"','"+project_id+"','"+date+"')"); 
     out.println("saved"); 
    } 
+0

但是当我用,而我得到这个错误 – roja

+0

javax.servlet.ServletException:java.sql.SQLException:ResultSet关闭后不允许操作 – roja

+0

这表示ResultSet已关闭,并且您正在执行查询 –

相关问题