無線&移動互聯網技術研發

          換位思考·····
          posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          java.sql.SQLException: '@P0' 附近有語法錯誤

          Posted on 2009-12-10 11:00 Gavin.lee 閱讀(3576) 評論(1)  編輯  收藏 所屬分類: JDBC
          SQL語句:
          String sql = "select top ? * from bbs_posts where p_fid=? order by p_addtime desc";
                  prepStmt 
          = conn.prepareStatement(sql);
                  prepStmt.setInt(
          1, xxx);
                  prepStmt.setInt(
          2, xx);
                  prepStmt.executeQuery()

          預編譯:
          Exception:
          java.sql.SQLException: '@P0' 附近有語法錯誤。
              at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:
          368)
              at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:
          2816)
              at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:
          2254)
              at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:
          631)
              at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:
          477)
              at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:
          777)
              at yixun.wap.db.DBConnection.executeQuery(DBConnection.java:
          41)
              at yixun.wap.bbs.dao.ForumDAO.get500WANforum(ForumDAO.java:
          282)
              at yixun.wap.bbs.BbsCache.get500WANforumCache(BbsCache.java:
          91)
              at yixun.wap.bbs.service.ForumBO.get500WANforum(ForumBO.java:
          143)
              at _jsp._page._bbs._space._second__jsp._jspService(_second__jsp.java:
          83)

          原因:sql不支持為select top ? 預編譯,換成動態拼接
          String sql = "select top %s * from bbs_posts where p_fid=? order by p_addtime desc";
                  sql 
          = String.format(sql, num);

          評論

          # re: java.sql.SQLException: '@P0' 附近有語法錯誤  回復  更多評論   

          2014-02-01 22:24 by 單獨
          你這樣在?號加個括號就行的了.
          String sql = "select top (?) * from bbs_posts where p_fid=(? )order by p_addtime desc";
          主站蜘蛛池模板: 师宗县| 景宁| 宜兰县| 赣州市| 无极县| 大竹县| 阜宁县| 旌德县| 望谟县| 丰原市| 南京市| 招远市| 江西省| 资兴市| 衡水市| 曲阜市| 四川省| 开阳县| 双流县| 普宁市| 如东县| 苏尼特左旗| 清苑县| 鸡泽县| 武定县| 武隆县| 台州市| 承德市| 雷波县| 阜阳市| 边坝县| 淮阳县| 清水河县| 吴旗县| 山阳县| 晋州市| 兴国县| 乐昌市| 武汉市| 安福县| 阳春市|