2015-01-20 48 views
-3

您好我的Java程序中出现此错误。这是我的查询。它在SQL Server中运行良好。但得到错误:附近的语法不正确(数据库名称)

Error: Incorrect syntax near 'WebApp'.

private static final String SERVICES = 
     "SELECT s.Service_ID " 
      + ",s.[Location_ID] " 
      + ",COALESCE(st.[Service_Type_Name],s.[Service_Name]) AS Service_name " 
      + ",st.Service_Type_Name " 
      + " FROM [WebApp].[dbo].[Services] s join [WebApp].[dbo].[ServiceTypes] st on s.Service_Type=st.Service_Type_ID " 
      + " join WebApp.dbo.Locations l on s.Location_ID=l.Location_ID " 
      + " where s.Deleted=0 " 
      + " ORDER BY Location_ID "; 

,这里是我的方法是MS SQL Server 2008中

public List<MAServiceVO> getAddServices() throws CoopCRSAPIException { 
     ArrayList<MAServiceVO> results = new ArrayList<MAServiceVO>(); 
     MAServiceVO maServiceVO = null; 
     log.debug("==========IN VendorDAOimpl.java (service)==========="); 
     //int serviceID = 0; 
     //int prevServiceID = 0; 
     try { 
      conn = MSSQLDAOFactory.createConnection(); 
      stmt = conn.prepareStatement(SERVICES); 
//   stmt.setTimestamp(1, startDate); 
//   stmt.setTimestamp(2, endDate); 

      stmt.execute(); 
      rs = stmt.getResultSet(); 

      while (rs.next()) { 

        // create new service 
        maServiceVO = new MAServiceVO(); 
        // set service fields 
        maServiceVO.setServiceID(rs.getInt("Service_ID")); 
        maServiceVO.setLocationID(rs.getInt("Location_ID"));      
        maServiceVO.setServiceName(rs.getString("Service_Name")); 
        maServiceVO.setServiceType(rs.getString("Service_Type_Name")); 
        log.debug("==========done with VendorDAOimpl.java (service)==========="); 

       } 

     } catch (SQLException e) { 
      log.debug(e.getMessage()); 
      throw new CoopCRSAPIException(e.getMessage(), " VendorDAOimpl", "getAddServices", 500); 
     } finally { 
      closeConnections("getAddServices"); 
     } 
     log.debug("&&&&&&&&&&&&&&&&&&&&&"); 
     log.debug("==========finsh==========="); 
     return results; 

    } 
+0

准备好您的问题,以尽可能少的代码 – janisz 2015-01-20 22:20:36

回答

0

上工作正常我看不出有什么不正常的存在。如果有一个原因,你没有这样的存储过程,而不是通过SQL?我注意到你没有在最后加入的时候放上方括号,但这不应该有任何区别。

这是您的查询后剥离所有额外的字符串部分的Java。

SELECT s.Service_ID 
    , s.[Location_ID] 
    , COALESCE(st.[Service_Type_Name], s.[Service_Name]) AS Service_name 
    , st.Service_Type_Name 
FROM [WebApp].[dbo].[Services] s 
join [WebApp].[dbo].[ServiceTypes] st on s.Service_Type = st.Service_Type_ID 
join [WebApp].[dbo].[Locations] l on s.Location_ID = l.Location_ID 
where s.Deleted = 0 
ORDER BY Location_ID; 
相关问题