查询多结果子查询不使用jdbc

时间:2017-07-03 06:18:50

标签: java oracle jdbc

我一直在研究这个具体问题几天,找不到任何解决方案。因此,我在这里。

情况:

  • 表1列出了系统中的所有JBoss
  • Table2描绘了JBoss和JBoss之间的1:n关系,因为任何给定的JBoss都可以有一个或多个JBoss,它会“说话”。
  • 我有一个带有JDBC的Java servlet连接到数据库并发出查询和.jsp页面来显示结果

修改

我打算如何处理查询:我的servlet正在尝试显示连接到给定jboss的所有信息。用户从jsp页面的jboss名称列表中选择一个jboss。然后执行查询并发生错误。

EDIT2:

将查询更改为内部子选择为const bool = "101011"; const bool2 = "111011"; const and = (parseInt(bool,2) & parseInt(bool2,2)).toString(2); console.log("bool: "+bool+", bool2: "+bool2+", &: "+and);会在测试小程序时导致相同的错误

EDIT4:

尝试使用查询作为脚本创建视图,然后尝试使用where in从创建的视图中获取所有条目,但没有成功。

问题:

当我尝试使用servlet运行以下select * from vtest时,会产生ORA-01427。如果我在select中运行此语句,我会得到所需的结果。

Toad for Oracle

问题: 查询中是否存在我遗漏的内容?即使查询本身是正确的,select * from table1 where number in ( select jboss2 from table2 where jboss1 = ( select number from table1 where name = 'nam1')) 是否有可能无法处理多行的子查询?

表1:

jdbc

表2:

+--------+------+-----------+
| Number | Name | values... |
+--------+------+-----------+
| 000001 | nam1 | vals1     |
| 000002 | nam2 | vals2     |
| 000003 | nam3 | vals3     |
+--------+------+-----------+

结果Toad / Desired result:

+--------+--------+
| JBoss1 | JBoss2 |
+--------+--------+
| 000001 | 000002 |
| 000001 | 000003 |
| 000002 | 000003 |
+--------+--------+

EDIT3:

相关的Java类。省略了不相关的查询。

+--------+------+-----------+
| Number | Name | values... |
+--------+------+-----------+
| 000002 | nam2 | vals2     |
| 000003 | nam3 | vals3     |
+--------+------+-----------+

错误消息

class QuickInfoAction implements Action{

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ActionException {
        Connection conn = null;
        PreparedStatement prep = null;
        ResultSet rs = null;
        Map<String,String> queries = Queries.getInfoQueries(request);

        try {
            conn = DatabaseConnector.getConnection();
            Map<String, Result> res = new HashMap<String, Result>();
            for (Map.Entry<String, String> entry: queries.entrySet()) {
                prep = conn.prepareStatement(entry.getValue()); 
                rs = prep.executeQuery();

                while(rs.next()) {
                    res.put(entry.getKey(), ResultSupport.toResult(rs));
                }       
            }

            request.setAttribute("results", res);

        } catch (Exception e) {
            throw new ActionException(e.getStackTrace().toString());
        } finally {
            try {
                conn.close();
                prep.close();
                rs.close();
            } catch (Exception e) {
                throw new ActionException(e.getStackTrace().toString());
            }
        }

        return "results";
    }

}


public static Map<String, String> getInfoQueries(HttpServletRequest request) {
    String jboss_res = "select jboss.name, jboss.port, jboss.apache_nummer, jboss.bere_mandant_id, "
            + "maschine.name as maschine, maschine.ip_adresse "
            + "from jboss "
            + "inner join maschine on jboss.maschine_nummer = maschine.nummer "
            + "where jboss.name = '" + request.getParameter("jboss") + "'";

    String jboss_db = "select datenbank.nummer, datenbank.name, db_schema.name as schema "
            + "from datenbank "
            + "inner join db_schema on datenbank.db_schema_nummer = db_schema.nummer "
            + "where datenbank.nummer = ("
                + "select datenbank_nummer "
                + "from jboss_datenbank "
                + "where jboss_nummer = ("
                    + "select nummer "
                    + "from jboss "
                    + "where name = '" + request.getParameter("jboss") + "'))";

    String jboss_tux = "select tuxedo.*, datenbank.name as datenbank, db_schema.name as schema "
            + "from tuxedo, datenbank,db_schema "
            + "where tuxedo.nummer = ("
                + "select tuxedo_nummer "
                + "from jboss "
                + "where name = '" + request.getParameter("jboss") + "') "
            + "and datenbank.nummer = ("
                + "select datenbank_nummer "
                + "from tuxedo_datenbank "
                + "where tuxedo_nummer = tuxedo.nummer) "
            + "and db_schema.nummer = ("
                + "select db_schema_nummer "
                + "from datenbank "
                + "where nummer = ("
                    + "select datenbank_nummer "
                    + "from tuxedo_datenbank "
                    + "where tuxedo_nummer = tuxedo.nummer))";
    String jboss_corr = "select * from jboss where nummer in ("
                + "select jboss_nummer_2 from jboss_corr where jboss_nummer_1 in ("
                + "select nummer from jboss where name = '" + request.getParameter("jboss") + "'))";

    Map<String, String> queries = new HashMap<String,String>();
    queries.put("jboss", jboss_res);
    queries.put("datenbank", jboss_db);
    queries.put("tuxedo", jboss_tux);
    queries.put("corr", jboss_corr);

return queries;

3 个答案:

答案 0 :(得分:0)

似乎你也需要内部子选择

select * from table1 where number in (
  select jboss2 from table2 where jboss1 in (
    select number from table1 where name = 'nam1'))

答案 1 :(得分:0)

不确定为什么带有where-in的查询不适合您,但这里是一个查询,其中第二个子查询被连接替换:

select * from table1 where number in (
  select jboss2 from table2
  join table1 on table2.jboss1 = table1.number
    and table1.name = 'nam1'
)

答案 2 :(得分:0)

我找到了解决方案。另一个查询也返回了多个结果。此查询不是为此而设计的,因此失败了。这是我的疏忽。