为什么在我设置参数时我的查询没有执行

时间:2015-03-04 05:42:13

标签: java sql-server spring parameter-passing

我有一个java文件,它与后端db交互并为我检索行,

我的java与db交互的代码部分如下,

 try {
        con = DBconnection.getConnection();
        System.out.println("in get_camplist");
        String brand = request.getParameter("brand_id");
        System.out.println(brand);
        int bid = Integer.parseInt(brand);
        System.out.println(bid);
        String start_date = request.getParameter("s_date");
        String end_date = request.getParameter("e_date");
        System.out.println(start_date);

        prepStmt = con.prepareStatement(dbquery.getContentTable);
        prepStmt.setInt(1, bid);
        prepStmt.setString(2, start_date);
        prepStmt.setString(3, end_date);
        rs = prepStmt.executeQuery();

        tblist = new ArrayList<Cont_foc_tab>();
        Cont_foc_tab tb = null;
        while (rs.next()) {
            tb = new Cont_foc_tab(rs.getString("video_Name"), rs.getLong("views"), rs.getLong("likes"), rs.getLong("comments"), rs.getLong("shares"), rs.getLong("engagement"));
            tblist.add(tb);
        }

dbquery.getContentTable查询如下:

 public static String getContentTable = "select  a.vidid as     video_id,b.Vid_Name as video_Name,b.Vid_URL as url,  " +
"             a.Views as views,a.likes as likes,a.comments as     comments,a.shares as shares,a.Engagement as engagement  " +
 "             from ((select VidID as vidid,SUM(Yt_Views) as     Views,SUM(Yt_Likes) as likes,  " +
 "             SUM(Yt_Comments) as comments,SUM(Yt_Shares) as     shares,round(((cast((SUM(Yt_Likes) + SUM(Yt_Shares) + SUM(Yt_Comments)) as     float)/SUM(Yt_Views))*100),2) as Engagement  " +
"             from  SocMet_VidMetrics_Fact  " +
"             where VidID in (select distinct VidID from     SocMet_VidMetrics_Fact where ProdID= ? ) and   " +
"             (DateKey>=(select DateKey from DimDate where Date= ? )  " +
"             and DateKey<=(select DateKey from DimDate where Date= ?     ))  " +
"             group by VidID) a left join SocMet_VidDetails_Dim b on     a.VidID=b.VidID) " +
"             where a.vidid in     ('-wK47AqI7P0','_8PdkNMNrBo','cjHJCF0seMQ','7aCUq-cPGbQ','5hSUbGStxhY','HPXHV6iHzIk','BLbxRvjDzd0','kvD0h-Ce52c','CcWnfduAS_E','PeHbEGKyBiY','A5HKiUe4KaU','bGcoQN-crzg','BMGMi2Webd0','YSVL4FvFhvw','vZgIbWTEoMA','OFCnDURIPQg'); ";

当我尝试通过在sql server management studio中传递相同的值来执行相同的查询时,它工作正常并在我尝试动态传递参数时返回行,它不会返回任何行,

sql management studio中的SQL查询

select  a.vidid as video_id,b.Vid_Name as video_Name,b.Vid_URL as url, 
         a.Views as views,a.likes as likes,a.comments as comments,a.shares as shares,a.Engagement as engagement 
         from ((select VidID as vidid,SUM(Yt_Views) as Views,SUM(Yt_Likes) as likes, 
         SUM(Yt_Comments) as comments,SUM(Yt_Shares) as shares,round(((cast((SUM(Yt_Likes) + SUM(Yt_Shares) + SUM(Yt_Comments)) as float)/SUM(Yt_Views))*100),2) as Engagement 
         from  SocMet_VidMetrics_Fact 
         where VidID in (select distinct VidID from SocMet_VidMetrics_Fact where ProdID= 20 ) and  
         (DateKey>=(select DateKey from DimDate where Date='01/01/2012' ) 
         and DateKey<=(select DateKey from DimDate where Date='07/31/2014' )) 
         group by VidID) a left join SocMet_VidDetails_Dim b on a.VidID=b.VidID)
         where a.vidid in ('-wK47AqI7P0','_8PdkNMNrBo','cjHJCF0seMQ','7aCUq-cPGbQ','5hSUbGStxhY','HPXHV6iHzIk','BLbxRvjDzd0','kvD0h-Ce52c','CcWnfduAS_E','PeHbEGKyBiY','A5HKiUe4KaU','bGcoQN-crzg','BMGMi2Webd0','YSVL4FvFhvw','vZgIbWTEoMA','OFCnDURIPQg');

1 个答案:

答案 0 :(得分:0)

尝试使用此类TO_DATE('<your_date_string>', 'yyyy/mm/dd')之类的内容。你有数据类型作为日期,你比较为字符串。

另外,我建议您使用log4jdbc connectionspy库,它将帮助您调试实际触发的代码查询。 你可以像这样包装连接对象

con = new net.sf.log4jdbc.ConnectionSpy(con);