方法中的java返回字符串[],字符串[]。长度不正确

方法中的java返回字符串[],字符串[]。长度不正确,java,Java,我想要的是将数据插入数组字符串[],然后打印数组值。 返回的字符串[]类型方法为 public String[] getRequirementDocIDofProject(String testprojectName) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException { String req_doc_ids[] = null; S

我想要的是将数据插入数组字符串[],然后打印数组值。 返回的字符串[]类型方法为

public String[] getRequirementDocIDofProject(String testprojectName)
        throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    String req_doc_ids[] = null;
    String str_sqlQuery = "select * from req_specs INNER JOIN nodes_hierarchy nh " + 
            "on nh.id=req_specs.testproject_id  " + 
            "INNER JOIN requirements reqs " + 
            "on req_specs.id =reqs.srs_id where nh.name='" + testprojectName + "'";
    int count = 0;
    int n = 0;
    initDB();
    resultSet = statement.executeQuery(str_sqlQuery);
    while (resultSet.next()){
        count = Integer.parseInt(resultSet.getString(1));
    }
    req_doc_ids = new String[count];

    resultSet = statement.executeQuery(str_sqlQuery);
    while (resultSet.next()) {
        req_doc_ids[n] = resultSet.getString("req_doc_id");
        System.out.println("REQID=" + req_doc_ids[n]);
        n++;
    }
    close();
    System.out.println("n==" + n);
    return req_doc_ids;
}
调用方法代码为

DBConnection dbcon = new DBConnection();
String req_doc_ids[] = dbcon.getRequirementDocIDofProject("XXXX");
System.out.println(req_doc_ids.length);
控制台中的打印消息是
REQUID=TECH-6104
REQID=TECH-6686
REQID=TECH-5391
REQID=TECH-5965
REQID=TECH-6530
REQID=TECH-6729
REQID=TECH-7082
REQID=TECH-7107
REQID=TECH-7184
n==9
7166


为什么req_doc_ids.length的值是7166而不是9来自结果集的第1列-它是最后一行中的值

while(resultSet.next()){
    count=Integer.parseInt(resultSet.getString(1));
}
相反,你的意思可能是:

while(resultSet.next()){
    count++;
}

请注意,这是创建阵列的一种不必要的低效方法。使用
列表
;或者,使用结果集API上的方法直接获取行数。

7166来自结果集的第1列-它是最后一行中的值

while(resultSet.next()){
    count=Integer.parseInt(resultSet.getString(1));
}
相反,你的意思可能是:

while(resultSet.next()){
    count++;
}

请注意,这是创建阵列的一种不必要的低效方法。使用
列表
;或者,使用结果集API上的方法直接获取行数。

Andy已经澄清了您的主要问题,此答案只是帮助您使用当前代码的扩展

您的代码中可能有几处改进

  • 宁愿使用而不是
    语句
    ,这是不安全的,可能会受到Jon Skeet已经提到的SQL注入攻击

  • 为什么要运行两次db查询,这可能是一个繁重的查询,只是为了找出正确初始化字符串数组的记录数

  • 使用
    List
    存储获得的任意行数,最后将列表转换为数组,如下面的代码所示

  • 去掉这么多不需要的变量和这么多行代码,使代码看起来清晰明了

  • 你可以试着把你的方法改成这个

    public String[] getRequirementDocIDofProject(String testprojectName)
            throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
        List<String> reqDocIdList = new ArrayList<String>();
        String str_sqlQuery = "select * from req_specs INNER JOIN nodes_hierarchy nh " + 
                "on nh.id=req_specs.testproject_id  " + 
                "INNER JOIN requirements reqs " + 
                "on req_specs.id =reqs.srs_id where nh.name='" + testprojectName + "'";
    
        initDB();
        resultSet = statement.executeQuery(str_sqlQuery);
        while (resultSet.next()){
            System.out.println("REQID=" + resultSet.getString("req_doc_id"));
            reqDocIdList.add(resultSet.getString("req_doc_id"));
        }
        close();
        System.out.println("n==" + reqDocIdList.size());
        return reqDocIdList.toArray(new String[reqDocIdList.size()]);
    }
    
    public String[]getRequirementDocIDofProject(String testprojectName)
    抛出SQLException、InstanceionException、IllegaAccessException、ClassNotFoundException{
    List reqDocIdList=新建ArrayList();
    String str_sqlQuery=“select*from req_specs internal JOIN nodes_hierarchy nh”+
    “on nh.id=req_specs.testproject_id”+
    “内部连接要求”+
    “on req_specs.id=reqs.srs_id,其中nh.name=”+testprojectName+”;
    initDB();
    resultSet=statement.executeQuery(str_sqlQuery);
    while(resultSet.next()){
    System.out.println(“REQID=“+resultSet.getString”(“req_doc_id”);
    添加(resultSet.getString(“req_doc_id”);
    }
    close();
    System.out.println(“n==”+reqDocIdList.size());
    返回reqDocIdList.toArray(新字符串[reqDocIdList.size()]);
    }
    
    Andy已经澄清了您的主要问题,此答案只是帮助您使用当前代码的扩展

    您的代码中可能有几处改进

  • 宁愿使用而不是
    语句
    ,这是不安全的,可能会受到Jon Skeet已经提到的SQL注入攻击

  • 为什么要运行两次db查询,这可能是一个繁重的查询,只是为了找出正确初始化字符串数组的记录数

  • 使用
    List
    存储获得的任意行数,最后将列表转换为数组,如下面的代码所示

  • 去掉这么多不需要的变量和这么多行代码,使代码看起来清晰明了

  • 你可以试着把你的方法改成这个

    public String[] getRequirementDocIDofProject(String testprojectName)
            throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
        List<String> reqDocIdList = new ArrayList<String>();
        String str_sqlQuery = "select * from req_specs INNER JOIN nodes_hierarchy nh " + 
                "on nh.id=req_specs.testproject_id  " + 
                "INNER JOIN requirements reqs " + 
                "on req_specs.id =reqs.srs_id where nh.name='" + testprojectName + "'";
    
        initDB();
        resultSet = statement.executeQuery(str_sqlQuery);
        while (resultSet.next()){
            System.out.println("REQID=" + resultSet.getString("req_doc_id"));
            reqDocIdList.add(resultSet.getString("req_doc_id"));
        }
        close();
        System.out.println("n==" + reqDocIdList.size());
        return reqDocIdList.toArray(new String[reqDocIdList.size()]);
    }
    
    public String[]getRequirementDocIDofProject(String testprojectName)
    抛出SQLException、InstanceionException、IllegaAccessException、ClassNotFoundException{
    List reqDocIdList=新建ArrayList();
    String str_sqlQuery=“select*from req_specs internal JOIN nodes_hierarchy nh”+
    “on nh.id=req_specs.testproject_id”+
    “内部连接要求”+
    “on req_specs.id=reqs.srs_id,其中nh.name=”+testprojectName+”;
    initDB();
    resultSet=statement.executeQuery(str_sqlQuery);
    while(resultSet.next()){
    System.out.println(“REQID=“+resultSet.getString”(“req_doc_id”);
    添加(resultSet.getString(“req_doc_id”);
    }
    close();
    System.out.println(“n==”+reqDocIdList.size());
    返回reqDocIdList.toArray(新字符串[reqDocIdList.size()]);
    }
    
    你有
    req\u doc\u id=newstring[count]
    那么问题是为什么
    count
    7166?7166来自结果集的第1列-它是最后一行的值。req_doc_ids=new String[count]这里的count值是多少?请注意:当前代码容易受到SQL注入攻击。请通过
    PreparedStatement
    了解参数化SQL,而不是将值(
    testprojectname
    )直接放入查询中那么问题是为什么
    count
    7166?7166来自结果集的第1列-它是最后一行的值。req_doc_ids=new String[count]这里的count值是多少?请注意:当前代码容易受到SQL注入攻击。请通过
    PreparedStatement
    了解参数化SQL,而不是将值(
    testprojectname
    )直接放入查询中。