Hi Friend,
Try the following code:
1)student.jsp:
<%@page import="java.sql.*"%>
<script>
function hello(){
var i=0,j=0;
var total=document.form.count.value;
var lsize=document.getElementById("lotSize").value;
var l=Number(total)/Number(lsize);
arr = new Array();
arr1=new Array();
for(i=0;i<lsize;i++)
{
arr.push(Math.floor(Math.random()*10) + 1);
}
for(j=0;j<arr.length;j++){
for(i=0;i<l;i++)
{
arr1.push(arr[j]);
}
}
var labels=document.getElementsByName("lot");
for(j=0;j<total;j++){
labels[j].value=arr1[j];
}
}
</script>
Enter LOT size you want:<input type="text" id="lotSize"><input type="button" value="Save" onclick="hello();"><br>
<form name="form" action="insertLot.jsp">
<table border="1">
<tr><th>Roll No</th><th>Age</th><th>Gender</th><th>Location</th><th>Location</th></tr>
<%Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:
mysql://localhost:3306/test","root","root";);
Statement st = con.createStatement();
ResultSet rs=st.executeQuery("select * from student");
int i=0;
while(rs.next()){
%>
<tr><td><input type="text" value="<%=rs.getString("rollNo")%>" name="roll"></td><td><input type="text" value="<%=rs.getString("age")%>" name="age"></td><td><input type="text" value="<%=rs.getString("gender")%>" name="gender"></td><td><input type="text" value="<%=rs.getString("address")%>" name="address"></td><td><input type="text" name="lot"></td>
</tr>
<%
i++;
}
%>
<input type="hidden" value="<%=i%>" name="count">
</table>
<input type="submit" value="Submit">
</form>
2)insertLot.jsp:
<%@page import="java.sql.*"%>
<%
String roll[]=request.getParameterValues("roll");
String lot[]=request.getParameterValues("lot");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:
mysql://localhost:3306/test","root","root";);
Statement st = con.createStatement();
for(int i=0;i<roll.length;i++){
String rollNo=roll[i];
String lotNo=lot[i];
st.executeUpdate("update student set lotNo='"+lotNo+"' where rollNo='"+rollNo+"'");
out.println("Updated Successfully");
}
%>
Thanks