日期:2014-05-17 浏览次数:20761 次
public class Entry extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] id=request.getParameterValues("id"); String[] name=request.getParameterValues("name"); String[] cid=request.getParameterValues("cid"); String[] cname=request.getParameterValues("cname"); String[] time=request.getParameterValues("time"); String[] ps=request.getParameterValues("ps"); String[] es=request.getParameterValues("es");//这个是得到每一列的值数组. int[] a=new int[ps.length];//因为数据库这2个字段是int类型的 int[] b=new int[es.length];//这个是把得到的字符型数组转为整型数组 for(int u=0;u<ps.length;u++){ a[u]=Integer.parseInt(ps[u]); b[u]=Integer.parseInt(es[u]); System.out.println(a[u]); System.out.println(b[u]); } response.setContentType("text/html"); response.setCharacterEncoding("gbk"); ConnSqlServer co=new ConnSqlServer(); Connection ct=co.connDb(); int i=-1; try{ //遍历每一列的数组然后插入数据库 for(int y=0;i<a.length;y++){ String mid=id[y]; String mname=name[y]; String mcid=cid[y]; String mcname=cname[y]; String mtime=time[y]; int mps=a[y]; int mes=a[y]; System.out.println("1123"); i= ct.createStatement().executeUpdate("insert grades(stuId,stuName,classId,className,semester,pscores,escores) " + "values('"+mid+"','"+mname+"','"+mcid+"','"+mcname+"','"+mtime+"','"+mps+"','"+mes+"')"); System.out.println("dddtt"); } }catch(Exception e){ e.printStackTrace(); }finally{ co.close(); } if(i!=-1) { response.sendRedirect("http://localhost:8080/proj/sucessful.jsp"); } }