通过应用程序上传数据时的唯一键约束

时间:2014-03-17 06:57:57

标签: java sql oracle

我试图通过java web应用程序将60条数据记录上传到oracle数据库。虽然加载较少的记录可以说少于15条记录它正在加载并且它是成功的但是当我尝试加载超过15条记录时,我得到的错误就像唯一键约束一样。表中有一个复合键,但数据不同。它通过SQL开发人员的import语句上传所有记录,但不通过应用程序上传。

我尝试了此链接http://viralpatel.net/blogs/java-load-csv-file-to-database/

中提供的这段代码

任何人都建议我怎么做。

JSP页面

<%
                    String path=request.getParameter("file");
                    //String file_path="C:\\Users\\pallavi123\\Desktop\\fileupload\\"+path;
                    String file_path="C:\\fileupload\\"+path;
                    //String file_path="C:\\Users\\Etree\\Desktop\\fileupload\\"+path;
                    System.out.println("absolute_path: "+path);
                    CSVLoader loader = new CSVLoader(getCon());

                    loader.loadCSV(file_path, "CMPSS_FORMTABLE",false);
                %>

Java代码:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import au.com.bytecode.opencsv.CSVReader;

public class CSVLoader {


private static final String SQL_INSERT = "INSERT INTO ${table}(${keys}) VALUES(${values})";
private static final String TABLE_REGEX = "\\$\\{table\\}";
private static final String KEYS_REGEX = "\\$\\{keys\\}";
private static final String VALUES_REGEX = "\\$\\{values\\}";

private char seprator;
//static private Connection connection=null;
private JdbcTemplate connection;

public CSVLoader() {
JdbcTemplate connection=new DbConnection().getJdbctemplate();
this.connection = connection;
//Set default separator
this.seprator = ',';
}

public void loadCSV(String csvFile, String tableName,boolean truncateBeforeLoad) throws Exception {

CSVReader csvReader = null;
if(null == this.connection) {
    throw new Exception("Not a valid connection.");
}
try {       
    csvReader = new CSVReader(new FileReader(csvFile), this.seprator);
} catch (Exception e) {
    e.printStackTrace();
    throw new Exception("Error occured while executing file. "+ e.getMessage());
}

String[] headerRow = csvReader.readNext();

if (null == headerRow) {
    throw new FileNotFoundException("No columns defined in given CSV file. Please check the CSV file format.");
}

String questionmarks = StringUtils.repeat("?,", headerRow.length);
questionmarks = (String) questionmarks.subSequence(0, questionmarks.length() - 1);

String query = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
query = query
        .replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
query = query.replaceFirst(VALUES_REGEX, questionmarks);

System.out.println("Query: " + query);

String[] nextLine;
Connection con = null;
PreparedStatement ps = null;
try {
    con = (Connection) this.connection;
    con.setAutoCommit(false);
    ps = con.prepareStatement(query);

    if(truncateBeforeLoad) {
        //delete data from table before loading csv
        con.createStatement().execute("DELETE FROM " + tableName);
    }

    final int batchSize = 10000;
    int count = 0;
    Date date = null;
    while ((nextLine = csvReader.readNext()) != null) {

        if (null != nextLine) {
            int index = 1;
            for (String string : nextLine) {
                date = DateUtil.convertToDate(string);
                if (null != date) {
                    ps.setDate(index++, new java.sql.Date(date.getTime()));
                } else {
                    ps.setString(index++, string);
                }
            }
            ps.addBatch();
        }
        if (++count % batchSize == 0) {
            ps.executeBatch();
        }
    }
    ps.executeBatch(); // insert remaining records
    con.commit();
} catch (Exception e) {
    con.rollback();
    e.printStackTrace();
    throw new Exception("Error occured while loading data from file to database."+ e.getMessage());
} finally {
    if (null != ps)
        ps.close();
    if (null != con)
        con.close();

    csvReader.close();
}
}

public char getSeprator() {
return seprator;
}

public void setSeprator(char seprator) {
this.seprator = seprator;
}


}

0 个答案:

没有答案