【问题标题】:Error: Incorrect syntax near (database name)错误:(数据库名称)附近的语法不正确
【发布时间】:2015-03-19 07:35:59
【问题描述】:

您好,我的 Java 程序出现此错误。这是我的查询。它在 SQL Server 中运行良好。但得到 ​​p>

错误:“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;

    }

【问题讨论】:

  • 准备你的问题,尽可能少写代码

标签: java sql-server jdbc


【解决方案1】:

我看不出有什么不正常的地方。如果有一个原因你在存储过程中没有这个而不是通过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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多