使用Spring JdbcTemplate插入数千行

时间:2015-02-26 22:39:30

标签: java spring spring-jdbc

我使用Spring JDBC插入从Excel文件(xlsx)读取的信息。首先,我将信息放入Vector(大小为70 000 !!)之后我使用studentJDBCTemplate.insertListStudents(students)方法从{ {1}}类将所有这个Student对象归于oracle数据库。   我的问题是,当我使用Oracle SQL开发人员执行此SQL请求(StudentJDBCTemplate)时,我只插入6041行,并且当我测试Vector的大小时,Eclipse控制台中没有异常被删除,我得到了70 000。   这是我的班级:

班级学生:实体

select count(*) from student

接口StudentDAO:包含所有方法将由StudentJDBCTemplate

实施
package com.tutorialspoint;

public class Student {
   private Integer age;
   private String name;
   private Integer id;

   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      return age;
   }

   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }

   public void setId(Integer id) {
      this.id = id;
   }
   public Integer getId() {
      return id;
   }
@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Student [age=");
    builder.append(age);
    builder.append(", name=");
    builder.append(name);
    builder.append("]");
    return builder.toString();
}

}

class StudentJDBCTemplate:包含所有CRUD方法

package com.tutorialspoint;

import java.util.List;

import javax.sql.DataSource;

public interface StudentDAO {
   /** 
    * This is the method to be used to initialize
    * database resources ie. connection.
    */
   public void setDataSource(DataSource ds);
   /** 
    * This is the method to be used to create
    * a record in the Student table.
    */
   public void create(String name, Integer age);
   /** 
    * This is the method to be used to insert
    * a list of record in the Student table.
    */
   public void insertListStudents(List<Student> listStudent);

}

class ExcelUtil:从xls和xlsx文件中读取

package com.tutorialspoint;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;    
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

public class StudentJDBCTemplate implements StudentDAO {
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplateObject;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplateObject = new JdbcTemplate(dataSource);
    }

    public void create(String name, Integer age) {
        String SQL = "insert into Student (id,name, age) values (SEQ_STUDENT.nextval,?, ?)";

        jdbcTemplateObject.update( SQL, name, age);
        System.out.println("Created Record Name = " + name + " Age = " + age);
        return;
    }

    @Override
    public void insertListStudents(List<Student> listStudent) {
        String sql = "insert into Student (id,name, age) values (SEQ_STUDENT.nextval,?, ?)";

        jdbcTemplateObject.batchUpdate(sql, new BatchPreparedStatementSetter() {

            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                Student student = listStudent.get(i);
                System.out.println("Student "+ i);
                ps.setString(1, student.getName());
                ps.setInt(2, student.getAge());

            }

            @Override
            public int getBatchSize() {
                return listStudent.size();
            }

          });
    }
}

类TestISNumber:测试学生从xlsx文件中读取它的年龄是否可以解析为双倍

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtil {

    public enum ExtentionFile{
        XLS,XLSX,NONE
    }

    //private Student student = new Student();

    public ExtentionFile checkExcelFile(String pathExcelFile) throws FileNotFoundException, IOException {

        File file = new File(pathExcelFile);
        if(file.isFile() && file.exists()){

            if(POIXMLDocument.hasOOXMLHeader(new BufferedInputStream(new FileInputStream(pathExcelFile)))){
                return ExtentionFile.XLSX;
            }

            if(POIFSFileSystem.hasPOIFSHeader(new BufferedInputStream(new FileInputStream(pathExcelFile)))){
                return ExtentionFile.XLS;
            }
        }
        return ExtentionFile.NONE;
    }

    public Vector<Student> readXlsxFile(String pathXlsxFile) throws IOException {
        Vector<Student>students = new Vector<Student>();
        Student student = new Student();
        XSSFWorkbook workbook = new XSSFWorkbook(pathXlsxFile);
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFRow row;
        Iterator<Row> rowIterator = sheet.iterator();
        while(rowIterator.hasNext()){
            row = (XSSFRow) rowIterator.next();
            Iterator<Cell>cellIterator = row.cellIterator();
            student = new Student();
            while(cellIterator.hasNext()){
                Cell cell = cellIterator.next();
                if(null !=cell && (cell.getCellType() == Cell.CELL_TYPE_STRING ||  cell.getCellType() == Cell.CELL_TYPE_NUMERIC)){
                    if(!TestISNumber.isNumber(cell.toString())){
                        student.setName(cell.toString());
                    }
                    else if(TestISNumber.isNumber(cell.toString())){
                        student.setAge((int)Double.parseDouble(cell.toString()));
                    }
                }
            }
            //System.out.println(student);
            students.add(student);
        }
        workbook.close();
        return students;    
    }

    public Vector<Student> readXlsFile(String pathXlsFile) throws IOException,InvalidFormatException  {

        Vector<Student> students = new Vector<Student>();
        Student student = new Student();
        FileInputStream inputStream = new FileInputStream(pathXlsFile);
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = workbook.getSheetAt(0);
        HSSFRow row;
        Iterator<Row> rowIterator = sheet.iterator();
        while(rowIterator.hasNext()){
            row = (HSSFRow) rowIterator.next();
            Iterator<Cell>cellIterator = row.cellIterator();
            student = new Student();
            while(cellIterator.hasNext()){
                Cell cell = cellIterator.next();
                if(null !=cell && (cell.getCellType() == Cell.CELL_TYPE_STRING ||  cell.getCellType() == Cell.CELL_TYPE_NUMERIC)){
                    if(!TestISNumber.isNumber(cell.toString())){
                        student.setName(cell.toString());
                    }
                    else if(TestISNumber.isNumber(cell.toString())){
                        student.setAge((int)Double.parseDouble(cell.toString()));
                    }
                }
            }
            //System.out.println(student);
            students.add(student);
        }
        workbook.close();
        inputStream.close();

        return students;
    }

    public Vector<Student> readFile(String pathFile) throws FileNotFoundException, IOException, InvalidFormatException{

        Vector<Student>students = new Vector<Student>();

        if(checkExcelFile(pathFile) == ExtentionFile.XLS){
            students = readXlsFile(pathFile);
        }

        if(checkExcelFile(pathFile) == ExtentionFile.XLSX){
            students = readXlsxFile(pathFile);
        }
        return students;
    }

    public static void main(String[] args) {
        ExcelUtil excelUtil = new ExcelUtil();
        Vector<Student> students = new Vector<Student>();
        try {
            students = excelUtil.readFile("students.xlsx");

        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Size of Students Vector: "+students.size());
        //System.out.println(Arrays.toString(students.toArray()));
        //System.out.println(Arrays.toString(new TreeSet<String>(students).toArray()));

    }

}

类MainApp:主类

package com.tutorialspoint;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestISNumber {

    //Test if Cell contain a String that can be parsed to double 
    public static boolean isNumber (String s){
        Pattern pattern = Pattern.compile("(.*^\\d.*)(\\d$)");
        Matcher matcher = pattern.matcher(s);
        if (matcher.matches()){
            return true; 
        } 
        return false; 
    }
    public static void main(String[] args) {
        System.out.println(isNumber("100"));
    }    
}

SQL脚本:

package com.tutorialspoint;

import java.io.IOException;
import java.util.Vector;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");

      StudentJDBCTemplate studentJDBCTemplate = 
      (StudentJDBCTemplate)context.getBean("studentJDBCTemplate");

      ExcelUtil excelUtil = (ExcelUtil) context.getBean("excelUtilStudent");

      try {
        Vector<Student> students = excelUtil.readXlsxFile("students.xlsx");
        /*for(Student student:students){
            studentJDBCTemplate.create(student.getName(), student.getAge());

        }*/
        studentJDBCTemplate.insertListStudents(students);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

   }
}

XML Bean配置:

--------------------------------------------------------
--  Fichier créé - jeudi-février-26-2015   
--------------------------------------------------------
--------------------------------------------------------
--  DDL for Sequence SEQ_STUDENT
--------------------------------------------------------

   CREATE SEQUENCE  "SPRINGJDBC"."SEQ_STUDENT"  MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 12181 CACHE 20 NOORDER  NOCYCLE ;

--------------------------------------------------------
--  DDL for Table STUDENT
--------------------------------------------------------

  CREATE TABLE "SPRINGJDBC"."STUDENT" 
   (    "ID" NUMBER(*,0), 
    "NAME" VARCHAR2(20 BYTE), 
    "AGE" NUMBER(*,0)
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS" ;
--------------------------------------------------------
--  DDL for Index SYS_C004040
--------------------------------------------------------

  CREATE UNIQUE INDEX "SPRINGJDBC"."SYS_C004040" ON "SPRINGJDBC"."STUDENT" ("ID");
--------------------------------------------------------
--  Constraints for Table STUDENT
--------------------------------------------------------

  ALTER TABLE "SPRINGJDBC"."STUDENT" MODIFY ("ID" NOT NULL ENABLE);

  ALTER TABLE "SPRINGJDBC"."STUDENT" MODIFY ("NAME" NOT NULL ENABLE);

  ALTER TABLE "SPRINGJDBC"."STUDENT" MODIFY ("AGE" NOT NULL ENABLE);

  ALTER TABLE "SPRINGJDBC"."STUDENT" ADD PRIMARY KEY ("ID");

PS:    我使用Spring-3.2.9.Release,Spring-JDBC-3.2.9.Release,ojdbc14,POI-3.11,oracle数据库10g express,Luna Eclipse和JavaSE 1.8(Oracle JDK)

1 个答案:

答案 0 :(得分:1)

我怀疑ExcelUtil

中的代码
        while(cellIterator.hasNext()){
            Cell cell = cellIterator.next();
            if(null !=cell && (cell.getCellType() == Cell.CELL_TYPE_STRING ||  cell.getCellType() == Cell.CELL_TYPE_NUMERIC)){
                if(!TestISNumber.isNumber(cell.toString())){
                    student.setName(cell.toString());
                }
                else if(TestISNumber.isNumber(cell.toString())){
                    student.setAge((int)Double.parseDouble(cell.toString()));
                }
            }
        }
        //System.out.println(student);
        students.add(student);

如果这个迭代器到达一个空单元格的行,或者一行包含两个包含字符串的单元格,该怎么办? Student上的任何一个或两个属性都可能保持为null,但无论如何都要将其添加到Vector。因此,它的大小将是您所期望的(70,000),但这并不意味着Oracle DB会喜欢所有这些。也许第6042行有问题。在调试器中,当有空字段时,可以添加断点以在添加Student时停止。