将 JDBC 驱动程序连接到 MSSQL 数据库时出现问题
Posted
技术标签:
【中文标题】将 JDBC 驱动程序连接到 MSSQL 数据库时出现问题【英文标题】:Problems connecting JDBC driver to MSSQL database 【发布时间】:2015-08-25 19:47:18 【问题描述】:我们有一个 java web 应用程序,我遇到了一些 JDBC 问题。去年,在 Java 7 上,我们使用 sqljdbc4.jar 连接到同一网络上另一台服务器上的 SQL 数据库没有问题。今年我们将应用程序升级到 Java 8,这就是问题出现的时候。我们将 JDBC 版本更新为 4.2 (sqljdbc42.jar)。 .jar 文件包含在我们的构建路径中,并且在一年前就可以使用。我们在 Eclipse-Luna IDE 中使用 JBOSS 7.0。
现在当我们尝试使用 JDBC 时,我们得到一个 java.lang.ClassNotFoundException。
Connection conn = null;
String db_hostname = "test.fake.com";
String db_portNumber = "1433";
String db_name = "db";
String db_userid = "user";
String db_password = "password";
String db_final_string = "jdbc:sqlserver://" + db_hostname + ":" + db_portNumber + ";databaseName=" + db_name;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(_db_final_string, _db_userid, _db_password);
这是由于 Class.forName() 导致的错误:
15:30:12,919 ERROR [stderr] (http--0.0.0.0-9091-5) java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver from [Module "deployment.irw-ear.ear:main" from Service Module Loader]
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:191)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:361)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:333)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:310)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:103)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at java.lang.Class.forName0(Native Method)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at java.lang.Class.forName(Unknown Source)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at com.softified.irw.delegate.IAContentDelegate.getIADataFromClient(IAContentDelegate.java:1776)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at com.softified.irw.delegate.IAContentDelegate.getIAData(IAContentDelegate.java:1751)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at com.softified.irw.ejb.IAContentBean.getIAData(IAContentBean.java:188)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
15:30:12,920 ERROR [stderr] (http--0.0.0.0-9091-5) at java.lang.reflect.Method.invoke(Unknown Source)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:370)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at com.softified.irw.ejb.BaseBean.intercept(BaseBean.java:27)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at sun.reflect.GeneratedMethodAccessor12.invoke(Unknown Source)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at java.lang.reflect.Method.invoke(Unknown Source)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptor.java:67)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:370)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at com.softified.irw.ejb.BaseBean.intercept(BaseBean.java:27)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at sun.reflect.GeneratedMethodAccessor12.invoke(Unknown Source)
15:30:12,921 ERROR [stderr] (http--0.0.0.0-9091-5) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at java.lang.reflect.Method.invoke(Unknown Source)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptor.java:67)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:45)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:44)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
15:30:12,922 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ee.component.ViewDescription$ComponentDispatcherInterceptor.processInvocation(ViewDescription.java:202)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:50)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ejb3.component.session.SessionInvocationContextInterceptor$CustomSessionInvocationContext.proceed(SessionInvocationContextInterceptor.java:126)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:211)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:284)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:182)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ejb3.component.session.SessionInvocationContextInterceptor.processInvocation(SessionInvocationContextInterceptor.java:71)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,923 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
15:30:12,924 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:146)
15:30:12,924 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
15:30:12,924 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
15:30:12,924 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:76)
15:30:12,924 ERROR [stderr] (http--0.0.0.0-9091-5) at com.softified.ithos.irw.api.IIAContent$$$view10.getIAData(Unknown Source)
15:30:12,924 ERROR [stderr] (http--0.0.0.0-9091-5) at com.softified.ithos.irw.IAContentView.getIADataDubois(IAContentView.java:123)
15:30:12,924 ERROR [stderr] (http--0.0.0.0-9091-5) at com.softified.ithos.irw.IAContentView.insertExternalData(IAContentView.java:98)
15:30:12,924 ERROR [stderr] (http--0.0.0.0-9091-5) at com.softified.ithos.irw.IAContentView.doPost(IAContentView.java:62)
15:30:12,924 ERROR [stderr] (http--0.0.0.0-9091-5) at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
15:30:12,924 ERROR [stderr] (http--0.0.0.0-9091-5) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
15:30:12,924 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
15:30:12,924 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
15:30:12,925 ERROR [stderr] (http--0.0.0.0-9091-5) at com.softified.ithos.irw.filter.IRWFilter.filterRequest(IRWFilter.java:176)
15:30:12,925 ERROR [stderr] (http--0.0.0.0-9091-5) at com.softified.ithos.irw.filter.IRWFilter.doFilter(IRWFilter.java:97)
15:30:12,925 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
15:30:12,925 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
15:30:12,925 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
15:30:12,925 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
15:30:12,925 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:139)
15:30:12,925 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57)
15:30:12,925 ERROR [stderr] (http--0.0.0.0-9091-5) at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:49)
15:30:12,926 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154)
15:30:12,926 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
15:30:12,926 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567)
15:30:12,926 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
15:30:12,926 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)
15:30:12,926 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
15:30:12,926 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667)
15:30:12,926 ERROR [stderr] (http--0.0.0.0-9091-5) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:952)
15:30:12,926 ERROR [stderr] (http--0.0.0.0-9091-5) at java.lang.Thread.run(Unknown Source)
如果有人能提供有关如何正确配置此功能的任何见解,那将是最有帮助的,因为我们已经尝试了多种调整组合。
【问题讨论】:
【参考方案1】:JBoss AS 7 与 Java 8 不兼容*。您需要更新到 WildFly 8/9。
注意事项
* 在AS 7 on Java 8 中查看更多信息。
【讨论】:
只是这个库不兼容 JBoss AS 7 吗?我们目前正在运行 Java 8 和 JBoss AS 7,除了这个问题。 JBoss 7.0.2 和 Java 8 (1.8.0_45)【参考方案2】:您必须下载兼容的Microsoft JDBC Driver for SQL Server
驱动程序。您可以在 msdn 驱动程序中找到它。
https://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx
这个网址应该可以工作。
【讨论】:
是的,我们目前正在使用与 Java 8(Microsoft JDBC Driver 4.2 for SQL Server)兼容的版本,但仍然出现此错误。 你确定它在你的类路径中吗? 我可以建议您创建一个 main 并直接定位该方法。如果它有效,则意味着您的 Eclipse 部署程序集定义存在问题。您是否检查过您的项目配置/部署程序集定义中是否存在 web-inf/lib? 是的 web-inf/lib 存在于配置/部署程序集定义中。我们在引用的 web-inf/lib 中还有其他依赖项,这些依赖项都没有抛出任何错误。【参考方案3】:我想通了。我需要将 jdbc 驱动程序设置为 JBoss 模块,并将其作为全局模块包含在 Standalone.xml 中。
【讨论】:
以上是关于将 JDBC 驱动程序连接到 MSSQL 数据库时出现问题的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 JDBC 连接到 SQL Server 2008 数据库?
Android - 关于使用 jdbc 连接到 MSSQL 的 classnotfound
msSQL jdbc .. 我连接到服务器但如何连接到使用特定的 databaseName
通过 *** 将 PHP 应用程序连接到 MSSQL 时应该考虑啥?