将参数从列表<string>传输到preparedStatement

时间:2015-05-23 21:11:34

标签: java jdbc prepared-statement postgresql-9.4

我准备好了声明:

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 like

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

修改

如何创建fileContents,

我在大班中使用这个指令:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

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();
}

你还应该考虑整个&#39;更新&#39;循环进入事务以提高性能。