在Tomcat中创建mssql数据库jndi资源时出现异常

时间:2011-09-23 21:50:28

标签: java sql-server tomcat jndi

我正在尝试为MSSQL数据库创建一个Tomcat JNDI资源。

这是我的资源字符串在tomcat context.xml文件中的外观

  <Resource 
    name="jdbc/FI/SD" 
    auth="Container" 
    type="javax.sql.DataSource"
    driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
    url="jdbc:sqlserver://LDIA;databaseName=SD"     
    validationQuery="Select 1"
    maxActive="1" maxIdle="1" maxWait="10000"
    user="rels"
    password="hidden" 
  />

这是我在Servlet中用来检查数据源的代码片段:

   Context initCtx = new InitialContext();
   Context envCtx = (Context) initCtx.lookup("java:comp/env");
   DataSource ds = (DataSource)envCtx.lookup("jdbc/FI/SD");
   Connection conn = ds.getConnection();

但是我在第4行创建连接时遇到此错误, 我该如何解决这个问题:

    org.apache.tomcat.dbcp.dbcp.SQLNestedException: 
Cannot create PoolableConnectionFactory (Login failed for user ''. The user is not associated with a trusted SQL Server connection.)
            at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
            at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
            at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
            at com.MyServlet.doGet(MyServlet.java:40)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

1 个答案:

答案 0 :(得分:1)

也许user应该是username