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

编程入门 行业动态 更新时间:2024-10-25 03:31:07
本文介绍了无法在Java应用程序中连接到SQLServer数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have connecting to my database in a JSF web application that I am trying to develop. Using Windows 7, NetBeans 6.9.1, SQLServer 2008, Glassfish 3.3.

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

I am using the sqljdbc4.jar driver that I copied from my XP system where I have successfully connected to SQLServer 2005.

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

I have verified that SQLServer 2008 is running on my system and I can connect to the database through SQL Server Management Studio using the username and password that I am trying to use in the application. The SQL Server Configuration Manager shows the IP connection port is 1433. So I think my database is set up properly.

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

I think I am doing things correctly, but I just can't connect to the database. Does anyone have any suggestions as to why this is failing and what I can check?

谢谢.

这是我的连接代码:

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$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.facesponent.UIViewRoot.notifyPhaseListeners(UIViewRoot.java:1050) at javax.facesponent.UIViewRoot.notifyBefore(UIViewRoot.java:845) at javax.facesponent.UIViewRoot.encodeBegin(UIViewRoot.java:959) at javax.facesponent.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.然后再次测试您的代码.

Check your SQL Server setting allow accept TCP/IP connections. Enable TCP/IP and restart SQL Server. Then test your code again.

更多推荐

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

本文发布于:2023-10-23 13:43:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520987.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   应用程序   数据库   Java   SQLServer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!