循环中的Java Prepared Statement无法正常工作

时间:2014-04-26 12:12:32

标签: java mysql excel prepared-statement

我尝试使用java prepared语句从excel文件中读取。我是java的新手。我必须从excel文件中获取数据,并相应地在一些处理中填充mysql中的四个表。我的代码如下。没有抛出异常,但在读取第一行后,它会截断while循环

     try{
        String line;
        BufferedReader br=new BufferedReader(new FileReader(filePath));
        String reg="NOTHING",sub="NOTHING";
        String data[]=null;
        PreparedStatement pst=null;
        int line_Cnt=0;
        while((line=br.readLine())!=null)
        {

                //chekcing and terminating first line headers
                if(line_Cnt==0)
                {
                    line_Cnt=1;
                    continue;
                }
                //checking for an empty line
                    data=line.split(",");


                if(data[9].toString().equals("Grand Total"))
                {
                    sub="NOTHING";
                    try{
                        qry="insert into tbl_report(studReg,grandGrade,grandObtained,grandMax,creditsAwarded,gradeAverage) values(?,?,?,?,?,?)";
                        pst=cn.prepareStatement(qry);
                        pst.setString(1,data[6].toString());
                        pst.setString(2,data[12].toString());
                        pst.setInt(3,Integer.parseInt(data[13].toString()));
                        pst.setInt(4,Integer.parseInt(data[14].toString()));
                        pst.setInt(5,0);
                        pst.setFloat(6,0.0f);
                        pst.executeUpdate();
                    }
                    catch(Exception e)
                    {System.out.println(e.getMessage());}
                }
                else if(data[9].toString().equals("Credits Awarded")){
                    qry="update tbl_report set creditsAwarded=? where studeReg=?";
                    pst=cn.prepareStatement(qry);
                    pst.setInt(1,Integer.parseInt(data[14].toString()));
                    pst.setString(2,data[6].toString());
                    pst.executeUpdate();
                }
                else if(data[9].toString().equals("Grade Point Average"))
                {
                    qry="update tbl_report set gradeAverage=? where studReg=?";
                    pst=cn.prepareStatement(qry);
                    pst.setFloat(1,Float.parseFloat(data[14].toString()));
                    pst.setString(2,data[6].toString());
                    pst.executeUpdate();
                    reg="NOTHING";
                }
                else{
                    if(!reg.equals(data[6].toString()))
                    {
                        qry="insert into tbl_student(studReg,studentName,studentSl,studentDate,studentProg,studentMonth,studentSem,studentExam) values (?,?,?,?,?,?,?,?)";
                        pst=cn.prepareStatement(qry);
                        pst.setString(1,data[6].toString());
                        pst.setString(2,data[5].toString());
                        pst.setString(3,data[0].toString());
                        pst.setString(4,data[1].toString());
                        pst.setString(5,data[2].toString());
                        pst.setString(6,data[3].toString());
                        pst.setString(7,data[4].toString());
                        pst.setString(8,data[7].toString());
                        pst.executeUpdate();
                        reg=data[6].toString();
                    }


                    if(!(sub.equals(data[10].toString())))
                    {

                        qry="insert into tbl_subject(studReg,courseCode,courseName,ciaObtained,ciaMax,eseObtained,eseMax,totalObtained,totalMax) values(?,?,?,?,?,?,?,?,?)";
                        pst=cn.prepareStatement(qry);
                        pst.setString(1,data[6].toString());
                        pst.setString(2,data[10].toString());
                        pst.setString(3,data[11].toString());
                        pst.setInt(4,0);
                        pst.setInt(5,0);
                        pst.setInt(6,0);
                        pst.setInt(7,0);
                        pst.setInt(8,0);
                        pst.setInt(9,0);
                        pst.executeUpdate(qry);
                        System.out.println("Reg:\t" + reg + "   Data: " + data[6].toString());
                        qry="insert into tbl_result(studReg,courseCode) values(?,?)";
                        pst=cn.prepareStatement(qry);
                        pst.setString(1,data[6].toString());
                        pst.setString(2,data[10].toString());
                        pst.executeUpdate();
                        sub=data[10].toString();
                    }

                    if(sub.equals(data[10].toString()))
                    {
                        if(data[9].toString().equals("CIA"))
                        {
                            qry="update tbl_result set ciaObtained=?,ciaMax=? where studReg=?";
                            pst=cn.prepareStatement(qry);                                
                            pst.setInt(1, Integer.parseInt(data[13].toString()));
                            pst.setInt(2,Integer.parseInt(data[14].toString()));
                            pst.setString(3,data[6].toString());
                            pst.executeUpdate();
                        }
                        else if(data[9].toString().equals("ESE"))
                        {
                            qry="update tbl_result set eseObtained=?,eseMax=? where studReg=?";
                            pst=cn.prepareStatement(qry);                                
                            pst.setInt(1, Integer.parseInt(data[13].toString()));
                            pst.setInt(2,Integer.parseInt(data[14].toString()));
                            pst.setString(3,data[6].toString());
                            pst.executeUpdate();
                        }
                        else if(data[9].toString().equals("Total"))
                        {
                            qry="update tbl_result set totalObtained=?,totalMax=? where studReg=?";
                            pst=cn.prepareStatement(qry);                                
                            pst.setInt(1, Integer.parseInt(data[13].toString()));
                            pst.setInt(2,Integer.parseInt(data[14].toString()));
                            pst.setString(3,data[6].toString());
                            pst.executeUpdate();
                            qry="update tbl_subject set studCredits=?,studGrade=? where studReg=?";
                            pst=cn.prepareStatement(qry);
                            pst.setString(3,data[6].toString());
                            pst.setString(2,data[12].toString());
                            pst.setInt(1,Integer.parseInt(data[8].toString()));

                        }
                    }
                }

            }
        }catch(Exception e){
            lblError.setText("Error: Cannot read excel file");
            lblError.setToolTipText(e.getMessage());
        }

请有人帮助我.....

0 个答案:

没有答案