从excel读取并插入数据库时​​获取日期的空值

时间:2014-09-18 09:20:32

标签: java mysql excel

实际上我正在读取excel表中的值并将它们插入到数据库中,但是获取日期字段的空值。我从excel表中解析日期时收到错误。

我正在使用像#34; 19/02/2014"在excel表中。

//连接声明,余额,dealerid,信用

 private  Connection con;
 static int z1=0;
   static  int z=0;
 static int Balance;
      int Dealerid;
  int Credit;
  int Productid;
  Long DealerPhno;
  String date;
      java.sql.Date dte ;



      public String updateActivate(String file1) throws ClassNotFoundException, ParseException {


                           try{

         date=row.getCell(0).getStringCellValue().trim();
        System.out.println("date is :"+date);
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");    
        java.util.Date jud= sdf.parse(date);

        dte=new java.sql.Date(jud.getTime());
        System.out.println("dte1:"+dte);
        }
        catch(Exception ex){

        }
    try{
        date=String.valueOf(row.getCell(0).getNumericCellValue()).trim();
        System.out.println("date is :"+date);
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");    
        java.util.Date jud= sdf.parse(date);

        dte=new java.sql.Date(jud.getTime());
        System.out.println("dte:"+dte);
    }
    catch(Exception ex){

    }
   //------------------------------------------------------------------------------          
        //Getting values from cell 
    try{
         Dealerid=(int)(row.getCell(1).getNumericCellValue());
    }
    catch(Exception ex){

    }
    try{
         Dealerid=Integer.parseInt(row.getCell(1).getStringCellValue());
    }
    catch(Exception ex){

    }
    try{
        Productid=(int)(row.getCell(2).getNumericCellValue());
    }
 catch(Exception ex){

    }
    try{
         Productid=Integer.parseInt(row.getCell(2).getStringCellValue());
     }
  catch(Exception ex){

     }
    try{
        DealerPhno=Long.valueOf((long) (row.getCell(3).getNumericCellValue()));
    }
    catch(Exception ex){

    }
    try{
         DealerPhno=Long.parseLong(row.getCell(3).getStringCellValue());
     }
     catch(Exception ex){

     }

      String sql = "insert into activation_phonum values(?,?,?,?)";

      System.out.println("dte is"+dte);
      System.out.println("dealerid is"+Dealerid);
     PreparedStatement  pstm1 =  con.prepareStatement(sql);
        pstm1.setDate(1, dte);
        pstm1.setInt(2, Dealerid);

        pstm1.setInt(3, Productid);
        pstm1.setLong(4, DealerPhno);

        boolean execute= pstm1.execute();




    }
           con.commit();



    return "export";
    }
//-----------------------------------------End of Export to activate1 table ---------------------    -----------------------------------    
catch(SQLException ex){
    System.out.println(ex);
}

    catch(IOException ioe){
    System.out.println(ioe);
}               
    return "notexport";

}



        }


    }
}

0 个答案:

没有答案