java.sql.sqlexception:当前语句的至少一个参数未初始化

时间:2015-08-30 11:10:23

标签: java sql jdbc

我编写了一个java程序,它应该逐行读取文本文件,提取某些信息并将其存储到数据库表中。该程序适用于较小的文本文件,但是当涉及到较大的文件(特定为12 Mb)时,我正在处理此错误消息:java.sql.sqlexception:当前语句的至少一个参数是初始化。

这是发生错误的方法:

public void ExtractBoard() throws IOException, SQLException {
    int rows = 0;
    String sqlInser = "INSERT INTO APP.SUBSCRIBERBR (subID, boardno)"+"VALUES (?,?)";
    PreparedStatement  stmt = conn.prepareStatement(sqlInser, Statement.RETURN_GENERATED_KEYS);
    String[] AryFiles2 = rf.OpenFile();
    int i,j;
    for (i=0; i<AryFiles2.length; i++) {
        if (AryFiles2[i].contains("ADD VSBR")) {
            String[][] parts = this.LineParts();
            Scanner in = new Scanner(parts[i][0]).useDelimiter("[^0-9]+");
            int intst = in.nextInt();
            String a= String.valueOf(intst);
            stmt.setString(1, a);
            for (j=0; j<parts[1].length; j++) {
                if (parts[i][j].contains("MN")) {
                    Scanner inn = new Scanner(parts[i][j]).useDelimiter("[^0-9]+");
                    int intstr = inn.nextInt();
                    String m= String.valueOf(intstr); 
                    stmt.setString(2,m);
                }
            }
        }
        rows = stmt.executeUpdate();
    }
}

你能帮我弄清楚我做错了吗?

1 个答案:

答案 0 :(得分:0)

参数的设置可能无法访问,请检查它们是否为空:

public void ExtractBoard() throws IOException, SQLException {
    int rows = 0;
    String sqlInser = "INSERT INTO APP.SUBSCRIBERBR (subID, boardno)"+"VALUES (?,?)";
    PreparedStatement  stmt = conn.prepareStatement(sqlInser, Statement.RETURN_GENERATED_KEYS);
    String[] AryFiles2 = rf.OpenFile();
    int i,j;
    String p1=null , p2=null;
    for (i=0; i<AryFiles2.length; i++) {
        if (AryFiles2[i].contains("ADD VSBR")) {
            String[][] parts = this.LineParts();
            Scanner in = new Scanner(parts[i][0]).useDelimiter("[^0-9]+");
            int intst = in.nextInt();
            p1 = String.valueOf(intst);
            for (j=0; j<parts[1].length; j++) {
               if (parts[i][j].contains("MN")) {
                   Scanner inn = new Scanner(parts[i][j]).useDelimiter("[^0-9]+");
                   int intstr = inn.nextInt();
                   p2 = String.valueOf(intstr); 

               }
           }
       }
       if(p1 != null && p2 != null){
           stmt.setString(1, p1);
           stmt.setString(2,p2);
           rows += stmt.executeUpdate();
       }
       else{
           \\throw an exception or do what you want
       }
   }
}