Tuesday, 4 August 2015

Establish a connection with Sqlserver using Jdbc


Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  // load the driver

Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Movie;user=sa;pass word=City05;");   

Note :

Before establishing a connection from server using jdbc first download a specific driver , in this context we downloaded "Sqljdbc4.jar". after download add this file in your project under Lib directory,

Delete duplicate entry from sqlserver table

Remove Repeted entry  from a table .


let us learn how to delete duplicate data from table .
Suppose there is a table with name “Employee” with two columns Id and Name , and there is no primary key involved in this table , hence there is possibility of having repeated values now , we have to delete those repeated values.

With empTemp as
(
Select *,row_number() over(partition by id, name order by id, name) as empTemp from employee
)
Delete from empTemp where empTemp>1


Note :

Row_number() : Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.

Over :  the OVER clause defines a window or user-specified set of rows within a query result set. A window function then computes a value for each row in the window. You can use the OVER clause with functions to compute aggregated values such as moving averages, cumulative aggregates, running totals, or a top N per group results.
With  : Specifies a temporary named result set, known as a common table expression (CTE).