java stmt.executeQuery如何执行复杂sql语句
各位大神,我有如下需求:
String query="CREATE TABLE [#temp](\n" +
"\t ColoumName [varchar](50)\n" +
") ";
if(KeyColumnMap.containsKey(Coloum))
{
String Values=KeyColumnMap.get(Coloum).toString();
String[] Value=Values.split(",");
query+=" insert into #temp values";
for (String s:Value)
{
query+="("+s+"),\r\n";
}
query=query.trim();
query=query.substring(0,query.length()-1)+";";
query+="select "+ColoumStr+" from "+cSCESourceCode+" a with(nolock) join #temp b on a."+Coloum+"=b.ColoumName ";
ResultSet rs =Odshelp.selectQuery(query);
自己构造一个复杂的sql语句,最后想利用stmt.executeQuery(sql);直接把结果集查询出来,结果报错"该语句没有返回结果集。"
以下是我的数据库执行方法
public RowSet selectQuery(String sql) throws SQLException {
// Connection con = null;
DruidPooledConnection con = null;
ResultSet rs = null;
Statement stmt = null;
// 使用sun的默认RowSet实现
CachedRowSetImpl rowset = new CachedRowSetImpl();
try {
con= DbPoolConnection.getInstance(PrefixCdrDb);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
//这里就是填充离线集
rowset.populate(rs);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
}
return rowset;
}