将文件上载到数据库时设置属性的值

时间:2016-05-30 07:19:18

标签: java sqlite csv

我有.csv文件"推销员"将上传到sqlite数据库。此文件包含有关推销员的详细信息。

Salesman的示例行:

-----------
|code|name|
-----------
|0001|Jon|
-----------
|0002|Stu|   

销售员的DDL:

CREATE TABLE salesman (
code       INTEGER NOT NULL,
name       TEXT    NOT NULL,
isSelected INTEGER NOT NULL,
PRIMARY KEY (
    code
)
);

属性isSelected充当标签,表明已选择推销员,因此无法再次选择。

这是我从.CSV文件中获取销售员详细信息的代码:

public static ArrayList<Salesman> getSalesmanFromFile(String filePath){
    ArrayList<Salesman> salesmanList= new ArrayList<Salesman>();
    try {
        BufferedReader br = new BufferedReader(new FileReader(filePath));
        String line = null;
        String[] strSalesman = null;            
        try {
            while ((line=br.readLine()) != null){
                strSalesman = line.split(",");
                salesmanList.add(new Salesman(Integer.parseInt(strSalesman[0]), strSalesman[1]));
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
        String status = "Error"; //- Please review file selected. Make sure that format is followed.";
    }
    return salesmanList;
}

我的问题是,如何将方法中的第三个属性isSelected设置为0(未选中)?由于.csv文件不包含标记属性,我想将其放在上面的方法中(将在整个程序的开头使用)。

我想在方法int isSelected = 0中创建变量,但我不知道如何将其与BufferedReader合并。类似的东西:

BufferedReader br = new BufferedReader(new FileReader(filePath));
    String line = null;
    String[] strSalesman = null;            
    //int isSelected = 0;
    try {
        while ((line=br.readLine()) != null){
            strSalesman = line.split(",");
            salesmanList.add(new Salesman(Integer.parseInt(strSalesman[0]), strSalesman[1], isSelected?));
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

任何帮助将不胜感激!

0 个答案:

没有答案