当前位置: 首页 > 面试题库 >

无法在Java应用程序中连接到SQLServer数据库

李成礼
2023-03-14
问题内容

我已经在尝试
开发的JSF Web应用程序中连接到数据库。使用Windows 7,NetBeans 6.9.1,SQLServer 2008,Glassfish 3.3。

我正在使用从 成功连接到SQLServer 2005的XP系统复制的sqljdbc4.jar驱动程序

我已验证SQLServer 2008在我的系统上运行,并且可以 使用要在应用程序中
使用的用户名和
密码通过SQL Server Management Studio连接到数据库。SQL Server
配置管理器显示IP连接端口为1433。因此,我认为
数据库已正确设置。

我认为我做的事正确,但是我无法连接到数据库。
是否有人对为什么失败以及我可以
检查的内容有任何建议?

谢谢。

这是我的连接代码:

public void connectDB()
{
    try
    {
        String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=ROVRDEV;user=rovrdevuser;password=rovrdevuser";
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection con = DriverManager.getConnection(connectionUrl);
        System.out.println("Database connection = "+con );
    }
    catch( Exception e )
    {
        e.printStackTrace();
        System.out.println( "Error connecting to database.  Error: "+e.getMessage() );
    }

}

When I attempt to run this, I get the following exception in the server log:

SEVERE: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:207)
    at rmsbackoffice.SessionBean1.connectDB(SessionBean1.java:130)
    at rmsbackoffice.SessionBean1.doInit(SessionBean1.java:107)
    at rmsbackoffice.SessionBean1.beforePhase(SessionBean1.java:88)
    at rmsbackoffice.org$jboss$weld$bean-web-ManagedBean-class_rmsbackoffice$SessionBean1_$$_WeldClientProxy.beforePhase(org$jboss$weld$bean-web-ManagedBean-class_rmsbackoffice$SessionBean1_$$_WeldClientProxy.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.UIViewRoot.notifyPhaseListeners(UIViewRoot.java:1050)
    at javax.faces.component.UIViewRoot.notifyBefore(UIViewRoot.java:845)
    at javax.faces.component.UIViewRoot.encodeBegin(UIViewRoot.java:959)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1754)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:662)

问题答案:

The TCP/IP connection to the host localhost, port 1433 has failed. Error: “Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.

检查您的SQL Server设置是否允许接受TCP / IP连接。启用TCP / IP
并重新启动SQL Server。然后再次测试您的代码。



 类似资料:
  • 我已经在一个实例中安装了opsmanager数据库,并在其他实例中安装了应用程序 > 在应用程序数据库服务器中,i时显示127.0.0.1:27017运行mongod 当我用sudo服务mongodb-mms启动时,会出现以下错误 uri=mongoDb://db_instance_publicip:27017/?maxpoolsize=150}错误:在等待连接时超时30000 ms后.....

  • application.yml: 我尝试从运行mysql的命名空间上的另一个pod访问该服务,因为它已经预先安装了mysql-client,并从主机访问该服务。两人都有访问数据库的权限。我还在运行应用程序的pod上tring ping。它发现服务有任何问题。 然后我尝试使用NodePort而不是ClusterIP。什么都没变。 我完全卡住了,不知道出了什么问题。如有任何帮助,不胜感激。

  • 问题内容: 我正在研究将由学校使用的应用程序。每所学校将建立自己的数据库。每个学校都会为应用程序提供自己的“设置”文件。设置文件将包含创建设置文件的特定学校的数据库URL。这样一来,使用该应用程序的学生如果想连接到其他数据库,就只能加载其他设置文件。 我的问题是,如何保护用于连接数据库的用户名和密码?因此,只有应用程序具有对数据库的读写访问权限。应用程序仅具有该特定学校的读写权限吗? 如果您需要更

  • 我在将 rails 2.3.5 应用程序连接到远程数据库时遇到问题。 在我的数据库.yml中,我有: 我得到的错误是: 无法通过套接字连接到本地 MySQL 服务器 (2) 我知道问题不在权限或用户设置上,因为当我使用相同的mysqlgem运行一个简单的ruby脚本时,它会起作用。此外,我的python脚本可以连接,我可以通过CLI与连接 我似乎无法让rails使用192.168.1.113而不是

  • 在下面您会发现错误 2019-03-15 10:38:53.865警告1708--[main]O.H.E.J.E.I.JDBCEnvironmentInitiator:HHH000342:无法获取查询元数据的连接:服务器时区值“Paris,Madrid”无法识别或表示多个时区。如果要利用时区支持,则必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)以使用更指定的时区值。2

  • 我刚刚使用带有依赖项的Spring工具套件创建了一个非常基本的Spring Boot项目,并运行了该应用程序,但是,我总是遇到以下异常 2019-11-27 00:31:19.699信息11988---[localhost:27017]org.mongodb.driver.cluster:连接到服务器时监视器线程出现异常localhost:27017 我错过了什么?