Java Spring jdbc sql server连接错误

Posted

技术标签:

【中文标题】Java Spring jdbc sql server连接错误【英文标题】:Java Spring jdbc sql server connection error 【发布时间】:2015-09-23 20:42:48 【问题描述】:

尝试使用 jdbc 将我在 Tomcat 上的 Java Spring 应用程序连接到我的 sqlserver 数据库。 得到错误:

"请求处理失败;嵌套异常是 org.springframework.jdbc.CannotGetJdbcConnectionException: 不能 获取 JDBC 连接;嵌套异常是 org.apache.commons.dbcp.SQLNestedException:无法创建 PoolableConnectionFactory(与主机 BEN-790 的连接,名为 实例 sqlexpress 失败。错误:“java.net.SocketTimeoutException: 接收超时”。验证服务器和实例名称并检查 没有防火墙阻止到端口 1434 的 UDP 流量。对于 SQL Server 2005 或更高版本,验证 SQL Server Browser 服务是否正在运行 在主机上。)”

pom.xml

<!-- Server Connection -->

-<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>

<!-- Test -->
-<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>


-<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.FINAL</version>
</dependency>

-<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>

<version>1.3.1</version>
</dependency>

-<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
<scope>runtime</scope>
</dependency>

</dependencies>
-<build>
-<plugins>
-<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
-<configuration>
-<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>

-<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>

<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>

</plugin>
-<plugin>
<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
-<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>

</plugin>

-<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
+<configuration>
</plugin>

-<plugin>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</plugin>
</plugins>

我的 servlet-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
-<beans:beans xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" xmlns:context="http://www.springframework.org/schema/context" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/mvc">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven/>

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the $webappRoot/resources directory -->

<resources location="/resources/" mapping="/resources/**"/>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->

-<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property value="/WEB-INF/views/" name="prefix"/>
<beans:property value=".jsp" name="suffix"/>
</beans:bean>

<!-- Enables us to use message.properties files -->
-<beans:bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
<beans:property value="home" name="basename"/>
</beans:bean>

<!-- Enable connection to MS SQL -->
-<beans:bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
<beans:property value="com.microsoft.sqlserver.jdbc.SQLServerDriver" name="driverClassName"/>
<!-- <beans:property name="url" value="jdbc:jtds:sqlserver://localhost/bens;instance=sqlexpress;useNTLMv2=true;domain=BEN-790"/> -->

<beans:property value="jdbc:sqlserver://BENS-790\SQLEXPRESS;databaseName=TIGGER" name="url"/>
<beans:property value="COMPANY\bens" name="username"/>
<beans:property value="" name="password"/>
</beans:bean>

<!-- <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <beans:property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/> <beans:property name="url" value="jdbc:sqlserver://COL-INFA:1433;databaseName=bens"/> <beans:property name="username" value="bens"/> <beans:property name="password" value="MyPwd"/> </beans:bean> -->

<context:component-scan base-package="com.company.tigger"/>

</beans:beans>

【问题讨论】:

您的机器似乎没有连接到数据库服务器。您可以将客户端工具连接到它吗? 是的,我可以使用 MS SQL Server 从数据库的表中选择数据。所以我有我的连接并且浏览器服务正在运行。 【参考方案1】:

为此更改连接 bean 怎么样?

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <!-- S2-Install-Start: INSERT DB SERVER HERE -->
        <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=MyDatabase;" />
        S2-Install-End:
        <property name="username" value="Stefana\Steffi" />
        S2-Install-Start: INSERT DB PASSWORD HERE
        <property name="password" value="" />
        S2-Install-End:
    </bean>

【讨论】:

【参考方案2】:

+1 jpganz 18,修复了它。我将类更改为“org.springframework.jdbc.datasource.DriverManagerDataSource”,并将 url 更改为 localhost:1433,然后它就起作用了。

【讨论】:

以上是关于Java Spring jdbc sql server连接错误的主要内容,如果未能解决你的问题,请参考以下文章

Spring mvc的jdbc查询:queryForInt(java.lang.String sql, java.lang.Object... args)

spring | 踩坑 | Could not get JDBC Connectionl nested exception is java.sql.SQLException...

如何从 SQL 脚本、JDBC 或 Spring JdbcTemplate 在 MySQL 中创建函数?

spring jdbc模糊查询代码怎么写

Spring入门案例之JDBC

spring 14-Spring框架JDBC操作