将参数从 list<String> 转移到 preparedStatement

Transfer parameter into a preparedStatement from list<String>

我有这个准备声明:

String insertSQL = "(INSERT INTO visit(Doc_Number, Pat_Number,
               to_timestamp(Date DD/MM/YYYY), Price) VALUES (?,?,?,?))"
PreparedStatement pstvisit = conn.prepareStatement(insertSQL);

我正在尝试这样做:

for (List<String> row : fileContents){

                pstvisit.clearParameters();
                pstvisit.executeUpdate("INSERT INTO VISIT
                         (Doc_Number, Pat_Number, to_timestamp(Date DD/MM/YYYY), 
                         Price) VALUES (?, ?, ?, ?)");

FileContents 读取具有此格式的文件数据:

# Doc_Nmber, Pat_Number, Visit_Date, Price
26902,6574405,30/03/2011,315
26507,6392432,14/03/2010,322
35356,6574405,15/02/2011,475
35252,9062865,07/07/2011,140

如果我这样做:

System.out.println(行);

我得到与文件数据相同的出口。

我的问题是,如何将数据文件的数据传输到

preparedStatement 像

pstvisit.setInt(1, GetTheDoc_NumberOfTheCorrectRow)
pstvisit.setInt(2, GetThePat_NumberOfTheCorrectRow)

等等

编辑

如何创建文件内容,

我在main中使用了这个指令class:

public Exercise1UpdateOrInsertDataFromFile() {
        super();
        fileUtilities = new FileUtilities();
    }
public static void main(String[] args) {
        Exercise1UpdateOrInsertDataFromFile app = new Exercise1UpdateOrInsertDataFromFile();
        app.run();
    }

    private void run() {

        List<List<String>> fileContents = null;
        try {
            fileContents = fileUtilities.readFileFromClasspath("exercise1.data");
        } catch (FileNotFoundException e) {
            System.err.println("ERROR: File not found");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("ERROR: I/O error");
            e.printStackTrace();
        }
        if (fileContents == null) {
            return;
        }

而 fileUtilites 实现是:

public class FileUtilities {

    /**
     * Reads a comma separated file from the classpath.
     */
    public List<List<String>> readFileFromClasspath(String file)
            throws FileNotFoundException, IOException {
        InputStream is = getClass().getClassLoader().getResourceAsStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(is);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        return readFileFromBufferedReader(bufferedReader);
    }

    /**
     * Reads a comma separated file from the filesystem.
     */
    public List<List<String>> readFileFromFilesystem(String file)
            throws FileNotFoundException, IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        return readFileFromBufferedReader(bufferedReader);
    }

    /**
     * Reads a comma separacted file from a Reader.
     * <ul>
     * <li>Lines started with '#' are ignored.</li>
     * <li>Spaces before and after the comma are ignored.</li>
     * <li>Fields can be surrounded by quotes.
     */
    private List<List<String>> readFileFromBufferedReader(
            BufferedReader bufferedReader) throws FileNotFoundException,
            IOException {
        List<List<String>> fileRows = new ArrayList<List<String>>();
        String line = bufferedReader.readLine();
        while (line != null && line.length() > 0) {
            if (line.charAt(0) != '#') {
                List<String> rowValues = new ArrayList<String>();
                String[] tokens = line
                        .split(",(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))");
                for (String token : tokens) {
                    String processedToken = stripQutoes(token);
                    rowValues.add(processedToken);
                }
                fileRows.add(rowValues);
            }
            line = bufferedReader.readLine();
        }
        bufferedReader.close();
        return fileRows;
    }

    private String stripQutoes(String token) {
        String tokenWithoutSpaces = token.trim();
        if (tokenWithoutSpaces.length() > 0) {
            if (tokenWithoutSpaces.charAt(0) == '"'
                    && tokenWithoutSpaces
                            .charAt(tokenWithoutSpaces.length() - 1) == '"') {
                return tokenWithoutSpaces.substring(1,
                        tokenWithoutSpaces.length() - 1);
            }
        }
        return tokenWithoutSpaces;
    }
}

您可以使用String#split()row拆分为四个卡盘。然后将每个字符串转换成所需的格式。

for (String row : fileContents) {
    pstvisit.clearParameters();

    String[] rowData = row.split(",");

    int docNumber = Integer.valueOf(rowData[0]);
    int patNumber = Integer.valueOf(rowData[1]);
    String date = row[2];
    int price = Integer.valueOf(row[3]);

    pstvisit.setInt(1, docNumber);
    pstvisit.setInt(2, patNumber);
    pstvisit.setString(3, date);
    pstvisit.setInt(4, price);

    pstvisit.executeUpdate();
}

您还应该考虑将上面的整个 'update' 循环放入事务中以提高性能。