0

I am learning spring just now. I want to connect my webapp to a ms sql 2008 R2 server. but i am getting many errors doing so. my bean file looks like this,

<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url"
            value="jdbc:sqlserver://Mrunal-PC\\MSSQLSERVER1;DataBaseName=Test;" />
        <property name="username" value="sa" />
        <property name="password" value="abc123" />
    </bean>

and i am getting following exception.

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 1
\mssqlserver1
 ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.escape(Unknown Source)
    at java.util.regex.Pattern.atom(Unknown Source)
    at java.util.regex.Pattern.sequence(Unknown Source)
    at java.util.regex.Pattern.expr(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:155)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:120)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:630)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:909)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:970)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:980)
    at com.tutorialspoint.StudentJDBCTemplate.create(StudentJDBCTemplate.java:19)
    at com.tutorialspoint.MainApp.main(MainApp.java:18)

erliear i was trying to connect using window authentication but even that time it was giving me time out error. I am really new here so i am unable to find any error. can anybody help me?

7
  • 1
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="url" value="jdbc:sqlserver://Mrunal-PC\\MSSQLSERVER1;DataBaseName=Test;" /> <property name="username" value="sa" /> <property name="password" value="abc123" /> </bean> Commented Nov 2, 2015 at 14:30
  • try \\\\ instead Commented Nov 2, 2015 at 14:31
  • Thank you. i will try right away. Commented Nov 2, 2015 at 14:31
  • if i use \\\ then its giving me following error. Commented Nov 2, 2015 at 14:34
  • 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 named instance has failed. Error: java.net.SocketTimeoutException: Receive timed out. at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) Commented Nov 2, 2015 at 14:34

1 Answer 1

1

Try jdbc:sqlserver://localhost;DataBaseName=Test. See more on proper jdbc url at the MSDN

Sign up to request clarification or add additional context in comments.

4 Comments

now its giving The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
Is your server running on the default port? Is it running on the same machine you are running your Spring app?
it is running on the same machine. i enabled tcp connection in sql configuration manager and also mentioned the port as 1433 in ip section. its still not working
i tried to connect to my db using eclips ide but its not working. connection url is jdbc:sqlserver://localhost:1433;databaseName=Test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.