java stmt.executeQuery如何执行复杂sql语句

fd1985318 2017-04-21 10:54:54
各位大神,我有如下需求:

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;
}
...全文
512 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
某程序员 2017-04-28
  • 打赏
  • 举报
回复
用prepareStatement安全也看起来舒服点。
fd1985318 2017-04-28
  • 打赏
  • 举报
回复
引用 2 楼 nn34625 的回复:
你这个不是复杂SQL语句这么简单了吧,你这都让人家执行两条不一样的SQL了...汗...
这个不复杂吧
旗木五五开 2017-04-22
  • 打赏
  • 举报
回复
你这个不是复杂SQL语句这么简单了吧,你这都让人家执行两条不一样的SQL了...汗...
fd1985318 2017-04-21
  • 打赏
  • 举报
回复
自己顶一下,让高手们看到

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧