java.sql.SQLException:数据类型不匹配将值从CSV插入到sqlite表中

时间:2016-06-21 09:32:28

标签: java sqlite csv

我使用sqlite创建了一个数据库sandapp.db,它最初有一个salesman表,其中包含以下列:

-----------
|code|name|
-----------

这些列的值来自.csv文件,将.csv文件中的数据插入sqlite db没有任何问题。这是工作代码:

public static String importSalesmanFile(String filepath){
    String insertQuery = "INSERT INTO SALESMAN VALUES (?,?)";
    String status = "";
    CSVReader reader = null; 
    Connection connection = null; 
    try {
        reader = new CSVReader(new FileReader(filepath), ',');
        connection = DBConnection.getConnection();
        Statement stmt = connection.createStatement();

        PreparedStatement pstmt = connection.prepareStatement(insertQuery);
        String[] rowData = null;

        int i = 0;
        while((rowData = reader.readNext()) != null){
            for (String data : rowData){
                pstmt.setString((i % 2) + 1, data);
                if (++i % 2 == 0)
                    pstmt.addBatch();
                if (i % 20 == 0)
                    pstmt.executeBatch();
            }
        }
        status = "Successfully uploaded";
    }   catch (Exception ex) {
        ex.printStackTrace();
    }finally { 
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex) {
                Logger.getLogger(SalesmanDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }           
    return status;        
}

然后我必须添加变量isSelected,其默认值为0,如果在程序中选择了Salesman,它将更改为1。所以我将上面的工作代码修改为此。所以我的新sandapp.db将是

---------------------
|code|name|isSelected|
---------------------

我更新了我的代码,但NetBeans会回复错误java.sql.SQLException: datatype mismatch

更新了回复错误的代码:

public static String importSalesmanFile(String filepath){
    String insertQuery = "INSERT INTO SALESMAN VALUES (?,?,?)";
    String status;
    CSVReader reader = null;
    Connection connection = null;
    PreparedStatement ps = null; 
    try {            
        reader = new CSVReader(new FileReader(filepath), ',');
        connection = DBConnection.getConnection();                        

        ps = connection.prepareStatement(insertQuery);
        String[] rowData = null;

        int i = 0;
        while((rowData = reader.readNext()) != null){
            for (String data : rowData){
                ps.setString((i % 3) + 1, data); //changed this
                if (++i % 3 == 0){//changed this
                    ps.addBatch();
                }
                if (i % 30 == 0){ //changed this
                    ps.executeBatch();
                }
            }
        }
        status = "Successfully Uploaded";
    }catch (Exception e){
        e.printStackTrace();
        System.err.print(e.getMessage());
        status = "Error - Upload Salesman file again.";
    }
    return status;
}

我更新了我的Salesman实体类:

public class Salesman {
private int code;
private String name;
private int isSelected = 0;        

public int getCode() {
    return code;
}

public void setCode(int code) {
    this.code = code;
}

public String getName() {
    return name;
}

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

public int getIsSelected() {
    return isSelected;
}

public void setIsSelected(int isSelected) {
    this.isSelected = 0;
}

}

我更新了我的getSalesman()方法:

public static ArrayList<Salesman> getSalesman(){
    ArrayList<Salesman> salesList = new ArrayList<Salesman>();
    Salesman salesman;
    String query = "SELECT * FROM salesman";
    try {
        Connection connection = DBConnection.getConnection();
        PreparedStatement ps = connection.prepareStatement(query);
        ResultSet r = ps.executeQuery();
        while(r.next()){
            salesman = new Salesman();

            salesman.setCode(r.getInt("code"));
            salesman.setName(r.getString("name"));
            salesman.setIsSelected(r.getInt("isSelected"));

            salesList.add(salesman);
        }
        ps.close();
        connection.close();
        r.close();
        return salesList;
    } catch (SQLException ex) {
        Logger.getLogger(SalesmanDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

最后,这是我的salesman创建表语句:

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

控制台显示此行是错误来源:

if (i % 30 == 0){
  ps.executeBatch(); //here
}

0 个答案:

没有答案