-1

I am creating a sample login page with database using Spring MVC. Using mysql I created a sample file. Its working Perfectly.Now I want to create same using SQL Server I tried a lot but its not working.

//mysql
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/sample_db" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

//SQL
<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="url" value="jdbc:sqlserver://DESKTOP-SAMP333\MSSQLSERVER2012;databaseName=samp" />
    <property name="username" value="root" />
    <property name="password" value="root" />

</bean>

In Maven Dependency:

<!-- DB Connection Pooling -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.0</version>
    </dependency>

    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.2.2</version>
    </dependency>

    <!-- MySQL Connector -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.30</version>
    </dependency>

    <!-- JSTL Dependency -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

I used this dependency for SQL Server:

<dependency>
<groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.2.1.jre8</version>

Where I am wrong? What I want to change to get the result?

Jerold Joel
  • 227
  • 1
  • 9
  • 22

1 Answers1

1

First add a SQL Server dependency

<dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>6.2.2.jre8</version>
    </dependency>

Then you need to download driver for sql server. Follow this link https://www.microsoft.com/en-us/download/details.aspx?id=11774

After completion of downloading unzip the app by opening it.

Then open your IDE(in my case Neatbeans)

  1. Go to Service
  2. Right click on Databases
  3. Click on New Connection
  4. Go to Driver and Choose a new driver enter image description here

  5. A dialog box will be open

  6. Now add a driver from that unzipped folder. The driver will be on Microsoft JDBC Driver 6.2 for SQL Server\sqljdbc_6.2\enu folder. You have to choose mssql-jdbc-6.2.2.jre8 jar file and click next
  7. Now you should configure a configuration as follows Host as localhost Port as 1433 Username as username that was created on sql server Password as you password

enter image description here

Click Test Connection before clicking on finish button to check whether the connection is right or not. If connection is successfull you can click on finish button

And you are done

Now you should change your beans configuration

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=samp" />
    <property name="username" value="root" />
    <property name="password" value="root" />

</bean>
Nishan Dhungana
  • 831
  • 3
  • 11
  • 30