0

I built a function in java that searches in the database for 2 values and if they exist return the values;

  public List<Conniction> findPath(int one,int two){
    List<Conniction> list = new ArrayList<Conniction>();
    Connection c = null;
    String sql = "SELECT * FROM conniction WHERE oneid="+one+"&& twoid="+two;
    try {
        c = ConnectionHelper.getConnection();
        Statement s = c.createStatement();
        ResultSet rs = s.executeQuery(sql);
        ResultSetMetaData metaData = rs.getMetaData();
        int columncount = metaData.getColumnCount();
        //direct result
        if (columncount > 0) {
            System.out.println("Match one and two found!");
            while (rs.next()) {
                list.add(processRow(rs));
            }
        }
        else{
            String sql2 = "SELECT * FROM conniction WHERE oneid="+one;
            s = c.createStatement();
            rs = s.executeQuery(sql2);
             metaData = rs.getMetaData();
             columncount = metaData.getColumnCount();
                if (columncount > 0) {
                    System.out.println("One Match found!");
                    while (rs.next()) {
                        list.add(processRow(rs));
                    }
                }
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return list;

}

How do I check what the values of the parameters that are returned are, to check if its really the right answer for oneid and twoid. It always returns something different from what I have sent.

4

2 回答 2

0

您的代码有两个严重的问题使其无法正常工作:

  1. metaData.getColumnCount()(不出所料)返回数,而不是数。返回的列数是一个常数,具体取决于查询中选择的内容。在这种情况下, you select *, sometaData.getColumnCount()就是表中的列数conniction。在处理它之前,您不知道行集中有多少行
  2. 您可能想要or逻辑,而不是and,即
    "SELECT * FROM conniction WHERE oneid="+one+" OR twoid="+two
于 2012-06-26T16:50:18.810 回答
0

我不知道这是否是最好的解决方案,但你可以: * 给列表对象带参数 * 然后返回一个代码 * 检查代码并使用列表

您的方法将列表作为参数获取:

    public int findPath(List<Conniction> list, int one, int two) {
        ...
        if (columncount > 0) {
            System.out.println("Match one and two found!");
            while (rs.next()) {
                list.add(processRow(rs));
            }
            return 2;
        } else {
            String sql2 = "SELECT * FROM conniction WHERE oneid="+one;
            s = c.createStatement();
            rs = s.executeQuery(sql2);
            metaData = rs.getMetaData();
            columncount = metaData.getColumnCount();
                if (columncount > 0) {
                    System.out.println("One Match found!");
                    while (rs.next()) {
                        list.add(processRow(rs));
                    }
                    return 1;
                } 
        }
    ...

}

在您的代码中,您调用检查返回的方法:

    ...
    List<Conniction> connlist = new ArrayList<Conniction>();
    int foo = findPath(connlist , one, two);
    if (foo == 1) {
        /*One Match found!*/
        // do something with your connlist-Object;
    } else if (foo == 2) {
        /*Match one and two found!*/
        // do something with your connlist-Object;
    }
于 2012-06-26T16:40:11.510 回答