CONNECT SQL SERVER DATABASE FROM JAVA

If you have any queries please leave a message here
Your Message
×


  1. Install Windows Operating System

  2. Install SQL Server

  3. Install Java and Set Environment Variables

  4. Copy sqljdbc.jar file into the bin folder of JDK (C:\Program Files\Java\jdk1.8.0_112\bin) and then extract here.

    sqljdbc.jar for JRE 7

    sqljdbc.jar for JRE 8

    sqljdbc.jar for JRE 9

    If you do not keep your *.java files into the bin folder of JDK (C:\Program Files\Java\jdk1.8.0_112\bin) then you must need to extract the mysqlconnector.jar file into the location where your *.java files are present.

Driver Class The driver class for the SQL-Server database is com.microsoft.sqlserver.jdbc.SQLServerDriver
Connection URL

The connection URL for the SQL-Server database is jdbc:sqlserver://192.168.10.195\\SQLSERVER2008R2;database=OfficeDatabase;user=sa;password=QuestionSolves

  • jdbc is the API

  • sqlserver is the database

  • 192.168.10.195 is the server IP address on which SQL-Server is running, we may also use server name

  • SQLSERVER2008R2 is the SQL-Server instance name

  • database=OfficeDatabase is the database name. We may use any database, in such case, you need to replace the OfficeDatabase with your database name

  • user=sa is the username and password=12345 is the password for SQL Server Authentication




Example 1
Write a program to create employee table and insert 3 records into it

//CreateEmployeeTable.java
import java.sql.*;
public class CreateEmployeeTable
{
public static void main(String args[])
{
try
{
// SQL-Server Database Connection
String connectionURL="jdbc:sqlserver://192.168.10.195\\SQLSERVER2008R2;database=OfficeDatabase;user=sa;password=12345";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con=DriverManager.getConnection(connectionURL);

// Query Execution
Statement stmt=con.createStatement();
stmt.executeUpdate("create table employee(empid varchar(10),empname varchar(50),salary int,primary key(empid))");
stmt.executeUpdate("insert into employee values('E-1010','Mr. X',25000)");
stmt.executeUpdate("insert into employee values('E-1020','Mr. Y',50000)");
stmt.executeUpdate("insert into employee values('E-1030','Mr. Z',35000)");

// Close The Connection
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


Example 2
Write a program to check whether an employee record present in the database or not

//ViewEmployeeDetails.java
import java.io.*;
import java.sql.*;
public class ViewEmployeeDetails
{
public void selectOperation()throws IOException
{
// Input employee id from the user
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the employee id ...");
String id=br.readLine();
try
{
String connectionURL="jdbc:sqlserver://192.168.10.195\\SQLSERVER2008R2;database=OfficeDatabase;user=sa;password=12345";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con=DriverManager.getConnection(connectionURL);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from employee");
int count=0;

// Check whether the given employee id present in the database or not
while(rs.next())
{
if(rs.getString("empid").equals(id))
{
System.out.println(rs.getString("empid"));
System.out.println(rs.getString("empname"));
System.out.println(rs.getString("salary"));
count=1;
}
}
con.close();

if(count==0)
System.out.println("Employee details not present in the database");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


//CallProgram.java
import java.io.*;
public class CallProgram
{
public static void main()throws IOException
{
ViewEmployeeDetails ob=new ViewEmployeeDetails();
ob.selectOperation();
}
}



ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution

Solutions of Different Questions


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us