Java code for connecting MS SQL Server by using SQL Server Authentication

First of all, You will need to add a jar file to your project library as SQL Server 2000 Driver for JDBC Service. My target is SQL Server 2000, it will require the jar file called "sqljdbc4.jar". This is not supported on Microsoft website now, you can download it here. For other versions of SQL Server, here is the link of SQL Server 2000 Driver for JDBC Service.

The following is the code for connection MS SQL Server and select some records from a testing table.

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
	public static void main(String[] args) throws SQLException, ClassNotFoundException {
		Connection conn = DriverManager.getConnection("jdbc:sqlserver://;user=name;password=abcdefg;database=Test");
		Statement sta = conn.createStatement();
		String Sql = "select * from testing_table";
		ResultSet rs = sta.executeQuery(Sql);
		while ( {
Category >> Database  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Rahul Yadav

    bata do bhai punay milega

  • kallo

    lo kallo bt

  • pandeyji

    kamal krte ho pandey ji khud k question ka khud h answer de rahe ho

  • Apalna

    vidhya se pucho usko jada ata h

  • nacho

    toh nacho tm jake hamara toh ni chala

  • aaru

    tumhe to aata hai sikha sakte ho

  • Apalna

    hm to ni sikha re kal lo jo krna h

  • sdasd

    aage jao baba hm kyo example de

  • aaru

    mujhe bhi sikha do

  • lo kallo bt

    lo kallo bt , hm kyo sikhaye tmko

  • Apalna

    Mujhe b sikha do , meko ni ata ye Please Help me

  • mekoBnipta

    bhai kahi aur pucho

  • KyaPtaYr

    yes very interesting question i also dont know

  • jai

    How Can we fetch data For a given Field From mysql to a combobox..

  • tozduman

    It works 🙂 Simple is best, thanks you!

  • Guest

    W3Schools Demo
    Resize this responsive page!

    London is the capital city of England.
    It is the most populous city in the United Kingdom,
    with a metropolitan area of over 13 million inhabitants.

    Paris is the capital and most populous city of France.

    Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
    and the most populous metropolitan area in the world.

  • rajendra lamror

    try {
    connection = DriverManager.getConnection(
    “jdbc:mysql://HOST:port_number/DB_name”,”DB_username”, “DB_pass”);
    st = connection.createStatement();
    rs = st.executeQuery(“SELECT * FROM table name WHERE id = 1”);
    } catch (Exception e) {
    System.out.println(“DB error : ” + e);

  • suresh

    Can you give an example how to connect sql into java

  • Actually, here’s some info on how to configure the SQL connection using Tomcat

  • How about storing the username and password in some config file on the server? I am new to Java, but hopefully it is possible somehow? Could you provide any example? Thanks!

  • sana

    nice and simple basic guide.