字符串无法转换为DAO接收器

时间:2019-03-25 12:50:48

标签: java servlets jdbc dao

我正在尝试使用列表对象执行批处理插入操作,但是在插入时我无法将String转换为DAO。迭代器循环中的接收方。

我试图列出列表对象,当时它正在从列表中打印值。但是,当我使用泛型为普通列表时,它显示错误,并且我找不到要插入的任何解决方案 通过这种方法,我正在读取excel文件并将其存储到列表中

public List collect(Receiver rec)
    {
        //ReadFromExcel rd = new ReadFromExcel();
        List<String> up = new ArrayList<String>();
        //List<String> details = rd.reader();
        //System.out.println(details);
        try( InputStream fileToRead = new FileInputStream(new File(rec.getFilePath()))) 
        { 
            XSSFWorkbook  wb = new XSSFWorkbook(fileToRead);


            wb.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
            XSSFSheet sheet = wb.getSheetAt(0);
            DataFormatter fmt = new DataFormatter();
            String data ="";
            for(int sn = 0;sn<wb.getNumberOfSheets()-2;sn++)
            {
                sheet = wb.getSheetAt(sn);
                for(int rn =sheet.getFirstRowNum();rn<=sheet.getLastRowNum();rn++)
                {
                    Row row = sheet.getRow(rn);
                    if(row == null)
                    {
                        System.out.println("no data in row ");

                    }
                    else
                    {
                        for(int cn=0;cn<row.getLastCellNum();cn++)
                        {
                            Cell cell = row.getCell(cn);
                            if(cell == null)
                            {
//                                System.out.println("no data in cell ");
//                                data = data + " " + "|";

                            }
                            else
                            {
                                String cellStr = fmt.formatCellValue(cell);
                                data = data + cellStr + "|";
                            }
                        }
                    }
                }
            }
            up = Arrays.asList(data.split("\\|"));


           // System.out.println(details);             
                }
                catch (FileNotFoundException ex) 
                {
                Logger.getLogger(BImplementation.class.getName()).log(Level.SEVERE, null, ex);
                } 
            catch (IOException ex) 
            {
                Logger.getLogger(BImplementation.class.getName()).log(Level.SEVERE, null, ex);
            } 

        Iterator iter = up.iterator();
        while(iter.hasNext())
        {
            System.out.println(iter.next());
        }

        String row="";
        Receiver info = null;
        String cid = "";
        String cname = "";
        String address = "";
        String mid = "";
        boolean b = false;


        List<Receiver> res = new ArrayList<Receiver>();
        int c = 0;
        try
        {
            String str = Arrays.toString(up.toArray()); 
           //System.out.println(str);
            String s = "";
            s = s + str.substring(1,str.length());
          // System.out.println("S:"+s);
            StringTokenizer sttoken = new StringTokenizer(s,"|");
            int count = sttoken.countTokens();
            while(sttoken.hasMoreTokens())
            {  
                if(sttoken.nextToken() != null)
                {
//                    System.out.print(sttoken.nextToken());
                    cid = sttoken.nextToken();
                    cname = sttoken.nextToken();
                    address = sttoken.nextToken();
                    mid = sttoken.nextToken();


                info = new Receiver(cid,cname,address,mid);
                res.add(info);
               System.out.println("cid :"+cid+ "  cname : "+cname +"  address : "+address+" mid : "+mid);
                c = res.size();
            // System.out.println(c);
                }
                else
                {
                    break;

                }
            }
            System.out.println(count);
           // System.out.println("s");



        } 
            catch(NoSuchElementException ex)
            {
                System.out.println("No Such Element Found Exception" +ex);
            }
        return up;

        }

使用这种方法,我试图插入数据库

public boolean insert(List res) 
    {
        String sqlQuery = "insert into records(c_id) values (?)";
        DBConnection connector = new DBConnection();

        boolean flag = false;

//        Iterator itr=res.iterator();
//        while(it.hasNext())
//        {
//            System.out.println(it.next());
//        }
         try( Connection con = connector.getConnection();)
         {
             con.setAutoCommit(false);        
             PreparedStatement pstmt = con.prepareStatement(sqlQuery);

              Iterator it = res.iterator();
              while(it.hasNext())
              {
                Receiver rs =(Receiver) it.next();
                pstmt.setString(1,rs.getcID());            
                pstmt.setString(2,rs.getcName());
                pstmt.setString(3,rs.getAddress());
                pstmt.setString(4,rs.getMailID());
                pstmt.addBatch();  


              }      
              int [] numUpdates=pstmt.executeBatch();
              for (int i=0; i < numUpdates.length; i++) 
              {
                if (numUpdates[i] == -2)
                {
                  System.out.println("Execution " + i +": unknown number of rows updated");
                  flag=false;
                }
                else
                {
                  System.out.println("Execution " + i + "successful: " + numUpdates[i] + " rows updated");
                  flag=true;
                }
              }
              con.commit();
            } catch(BatchUpdateException b) 
            {
              System.out.println(b);
              flag=false;
            }       
            catch (SQLException ex) 
            { 
                Logger.getLogger(BImplementation.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println(ex);
                flag=false;
            }
return flag;      
        }

我想使用JDBC批量插入功能将列表对象插入数据库。

1 个答案:

答案 0 :(得分:0)

您的方法 collect(Receiver rec)返回名为 up 的字符串列表。

return up;

但是(如果您确实使用了 collect 方法将List传递到 insert(List res)方法中),则希望此列表包含Receiver对象。这是不正确的,因为它collect(..)返回字符串列表。

当您尝试投射 Receiver rs =(Receiver)it.next();

时,这会导致错误

您需要查看并修复代码,以便传递Receiver对象的列表而不是字符串。

我真的建议您在使用List类的任何地方开始使用泛型。在这种情况下,编译器会立即向您显示所有数据类型错误。