日期:2014-05-17  浏览次数:20761 次

一个小问题..关于多行记录的插入
我有一个登录页面



这个页面提交给一个servlet处理
Java code

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");
        }
                    
            
        }




运行时它报个数据类型转换的错误.我觉得我已经把它转换了.为什么还报.
严重: Servlet.service() for servlet Entry threw exception
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.parseInt(Integer.java:497)
at ccsu.cxl.Entry.doPost(Entry.java:26)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalin