[英]How to get column count in SQL before executing the query? i.e. way other than ResultSetMetaData
I have a sql query. 我有一个SQL查询。 Because of too many BO's are involved, it's forming very big query containing more than 1000 columns.
由于涉及的BO过多,因此它形成了包含1000多个列的非常大的查询。 So I need some way with which I can find column count before hand and then apply some logic to handle 1000 column error.
因此,我需要某种方式可以事先找到列数,然后应用一些逻辑来处理1000列错误。
May be parsing a query to get column count would help. 可能解析查询以获取列数会有所帮助。 But not getting how to implement it.
但是没有得到实现的方法。
Note: tried below code already, it's giving me result, but i dont want to execute query, that's costly. 注意:已经在下面的代码中尝试过,它给了我结果,但是我不想执行查询,这很昂贵。
PreparedStatement pstmt;
try {
pstmt = getConnection().prepareStatement(sql);
ResultSetMetaData meta = pstmt.getMetaData();
System.out.println(meta.getColumnCount());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If it is a plain old select query like : 如果它是一个普通的旧选择查询,例如:
select col1,col2 as xyz, col3,col4... from ... where ...
you can do something like this: 您可以执行以下操作:
String select="select";//"SELECT" if yours is in caps
String from="from";//"FROM" if yours is in caps
String cols = sql.substring(sql.indexOf(select)+select.length(), sql.indexOf(from));
int colCount = cols.split(",").length;
If your query contains functions like fn(col,'blah blah')
you need to write some extra code to ID such functions and process things appropriately. 如果您的查询包含
fn(col,'blah blah')
类的函数,则需要编写一些额外的代码来标识此类函数并适当地进行处理。
I guess your SQL query using lot of wildcards after SELECT statement right? 我猜您的SQL查询在SELECT语句后使用了很多通配符,对吗? If no wildcard used, you can parse SQL query part between SELECT and FROM to get column numbers.I feel some disturbance in the force if i hear you have such big results of unknown attributes.
如果不使用通配符,则可以在SELECT和FROM之间解析SQL查询部分以获取列号。如果我听到您对未知属性有如此大的结果,我会感到有些不安。 If we can say that SQL table is data source, we can say that query result is data source too.
如果我们可以说SQL表是数据源,那么我们也可以说查询结果也是数据源。 In case of SQL table we have defined very specifically what columns(attributes) it contain.
对于SQL表,我们已经非常明确地定义了它包含哪些列(属性)。 If u create SQL query without specification of columns you have data source of unknown entity.
如果您创建不带列说明的SQL查询,则您具有未知实体的数据源。 Are you trying to create some BI system?
您是否要创建一些BI系统? If yes you should consider to get metadata of table and create specific query based on this informations.
如果是,则应考虑获取表的元数据并根据此信息创建特定的查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.