2016-05-23 11 views
0

私はSpring JDBCの新機能です。私は、マニュアルコードを使用して、私の地元のMS SQL Serverインスタンスに接続できるんだけど、春のJDBCを使用して、次の例外だ:SpringのSocketTimoutException JDBC

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. 
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) 
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:390) 
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:470) 
    at org.springframework.jdbc.core.JdbcTemplate.queryForRowSet(JdbcTemplate.java:511) 
    at org.springdemo.demo1.CustomJDBCTemplate.getDBs(CustomJDBCTemplate.java:25) 
    at org.springdemo.demo1.MainApp.main(MainApp.java:15) 
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. 

SQL Browserサービスがすでに実行されているが。続き

はbeans.xmlのです:

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

<bean id="dataSource1" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> 
    <property name="url" 
     value="jdbc:sqlserver://localhost\\SQLExpress;integratedSecurity=true;" /> 
</bean> 

<bean id="dataSource2" 
    class="org.apache.commons.dbcp2.BasicDataSource"> 
    <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" /> 
    <property name="url" 
     value="jdbc:sqlserver://localhost\\SQLExpress;integratedSecurity=true;" /> 
</bean> 

<bean id="customJDBCTemplate" class="org.springdemo.demo1.CustomJDBCTemplate"> 
    <property name="dataSources"> 
     <list> 
      <ref bean="dataSource1" /> 
     </list> 
    </property> 
</bean> 

あなたが見ることができるように、私は春のJDBCおよびコモンズDBCPの両方を試してみたが、両方が失敗しました。テンプレートクラスここ

package org.springdemo.demo1; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.jdbc.support.rowset.SqlRowSet; 

public class MainApp { 

public static void main(String[] args) { 
    ApplicationContext context = new ClassPathXmlApplicationContext(
      "beans.xml"); 
    CustomJDBCTemplate template = (CustomJDBCTemplate) context 
      .getBean("customJDBCTemplate"); 

    SqlRowSet srs = template.getDBs(); 
    while (srs.next()) { 
     System.out.println(srs.getString("name")); 
    } 
} 
} 

です:

package org.springdemo.demo1; 

import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.List; 

import javax.sql.DataSource; 

import org.springframework.jdbc.core.JdbcTemplate; 
import org.springframework.jdbc.support.rowset.SqlRowSet; 

public class CustomJDBCTemplate { 
    private List<DataSource> dataSources; 
    private List<JdbcTemplate> jdbcTemplateObjects = new ArrayList<JdbcTemplate>(); 

    public void setDataSources(List<DataSource> dataSources) { 
     this.dataSources = dataSources; 
     for(DataSource s : dataSources) 
      this.jdbcTemplateObjects.add(new JdbcTemplate(s)); 
    } 

    public SqlRowSet getDBs() 
    { 
     String query = "SELECT name FROM master.dbo.sysdatabases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb');"; 
     return jdbcTemplateObjects.get(0).queryForRowSet(query); 
    } 
} 

編集1: @Shailendra - ここでは、マニュアル作業コードだ: はここにメインのコードです

package jdbcexample; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

public class Class2 { 

    final static String DB_URL = "jdbc:sqlserver://localhost\\SQLExpress;integratedSecurity=true;"; 

    public static void main(String[] args) { 
     Connection conn = null; 
     Statement stmt = null; 
     try { 
      // STEP 2: Register JDBC driver 
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

      // STEP 3: Open a connection 
      System.out.println("Connecting to database..."); 
      conn = DriverManager.getConnection(DB_URL); 

      // STEP 4: Execute a query 
      System.out.println("Creating statement..."); 
      stmt = conn.createStatement(); 
      String sql_getDBList = "SELECT name FROM master.dbo.sysdatabases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb');"; 
      ResultSet rs = stmt.executeQuery(sql_getDBList); 

      // STEP 5: Extract data from result set 
      while (rs.next()) { 
       // Retrieve by column name 
       String dis = rs.getString("name"); 
       System.out.println(dis); 
      } 
      stmt.close(); 
      conn.close(); 
     } catch (SQLException se) { 
      // Handle errors for JDBC 
      se.printStackTrace(); 
     } catch (Exception e) { 
      // Handle errors for Class.forName 
      e.printStackTrace(); 
     } finally { 
      // finally block used to close resources 
      try { 
       if (stmt != null) 
        stmt.close(); 
      } catch (SQLException se2) { 
      }// nothing we can do 
      try { 
       if (conn != null) 
        conn.close(); 
      } catch (SQLException se) { 
       se.printStackTrace(); 
      }// end finally try 
     }// end try 
     System.out.println("Goodbye!"); 
    } 
} 
+0

正常に動作する「手動コード」を共有してください。 – Shailendra

+0

@Shailendra - 上記の作業コードを追加しました。 – ak92

答えて

0

私の髪を引き出し、私は本当に愚かだという結論に到達するために私に完全な一日の費用: | 私はデータソースの接続文字列に '\'のエスケープシーケンスを入れようとしていました。 削除されました。現在は問題なく動作しています。

0

変更してみてくださいIntegratedSecurity機能=真; IntegratedSecurity機能= SSPI

にこれは問題になる可能性があります: JDBC: Simple MSSql connection example not working

同様 Unable to establish database connection to SQL Server 2008 using java in Eclipse IDE

+0

動作しませんでした。次の例外があります。 原因:com.microsoft.sqlserver.jdbc.SQLServerException:プロパティintegratedSecurityに有効なブール値が含まれていません。 trueまたはfalseのみが使用できます。 – ak92

+0

リンクを確認してください。 –

+0

最初のリンクに従ってポート番号を変更しようとしました。今では手動コードも機能しなくなりました。 2番目のリンクも役に立たなかった。 – ak92

関連する問題