Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host localhost, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
  2. at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
  3. at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:390)
  4. at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:470)
  5. at org.springframework.jdbc.core.JdbcTemplate.queryForRowSet(JdbcTemplate.java:511)
  6. at org.springdemo.demo1.CustomJDBCTemplate.getDBs(CustomJDBCTemplate.java:25)
  7. at org.springdemo.demo1.MainApp.main(MainApp.java:15)
  8. Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host localhost, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
  9.  
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13. xsi:schemaLocation="http://www.springframework.org/schema/beans
  14. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
  15.  
  16. <bean id="dataSource1"
  17. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  18. <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
  19. <property name="url"
  20. value="jdbc:sqlserver://localhost\SQLExpress;integratedSecurity=true;" />
  21. </bean>
  22.  
  23. <bean id="dataSource2"
  24. class="org.apache.commons.dbcp2.BasicDataSource">
  25. <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
  26. <property name="url"
  27. value="jdbc:sqlserver://localhost\SQLExpress;integratedSecurity=true;" />
  28. </bean>
  29.  
  30. <bean id="customJDBCTemplate" class="org.springdemo.demo1.CustomJDBCTemplate">
  31. <property name="dataSources">
  32. <list>
  33. <ref bean="dataSource1" />
  34. </list>
  35. </property>
  36. </bean>
  37.  
  38. package org.springdemo.demo1;
  39.  
  40. import org.springframework.context.ApplicationContext;
  41. import org.springframework.context.support.ClassPathXmlApplicationContext;
  42. import org.springframework.jdbc.support.rowset.SqlRowSet;
  43.  
  44. public class MainApp {
  45.  
  46. public static void main(String[] args) {
  47. ApplicationContext context = new ClassPathXmlApplicationContext(
  48. "beans.xml");
  49. CustomJDBCTemplate template = (CustomJDBCTemplate) context
  50. .getBean("customJDBCTemplate");
  51.  
  52. SqlRowSet srs = template.getDBs();
  53. while (srs.next()) {
  54. System.out.println(srs.getString("name"));
  55. }
  56. }
  57. }
  58.  
  59. package org.springdemo.demo1;
  60.  
  61. import java.sql.SQLException;
  62. import java.util.ArrayList;
  63. import java.util.List;
  64.  
  65. import javax.sql.DataSource;
  66.  
  67. import org.springframework.jdbc.core.JdbcTemplate;
  68. import org.springframework.jdbc.support.rowset.SqlRowSet;
  69.  
  70. public class CustomJDBCTemplate {
  71. private List<DataSource> dataSources;
  72. private List<JdbcTemplate> jdbcTemplateObjects = new ArrayList<JdbcTemplate>();
  73.  
  74. public void setDataSources(List<DataSource> dataSources) {
  75. this.dataSources = dataSources;
  76. for(DataSource s : dataSources)
  77. this.jdbcTemplateObjects.add(new JdbcTemplate(s));
  78. }
  79.  
  80. public SqlRowSet getDBs()
  81. {
  82. String query = "SELECT name FROM master.dbo.sysdatabases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb');";
  83. return jdbcTemplateObjects.get(0).queryForRowSet(query);
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement