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";

  1. sana on 2012-10-6

    nice and simple basic guide.

  2. Slava Gorbunov on 2014-11-10

    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!

  3. Slava Gorbunov on 2014-11-10

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

  4. suresh on 2014-11-19

    Can you give an example how to connect sql into java

  5. rajendra lamror on 2014-11-27

    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);

  6. Guest on 2014-11-27

    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.

  7. tozduman on 2014-12-1

    It works 🙂 Simple is best, thanks you!

  8. jai on 2015-1-4

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

  9. KyaPtaYr on 2016-11-29

    yes very interesting question i also dont know

  10. mekoBnipta on 2016-11-29

    bhai kahi aur pucho

  11. Apalna on 2016-11-29

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

  12. lo kallo bt on 2016-11-29

    lo kallo bt , hm kyo sikhaye tmko

  13. aaru on 2016-11-29

    mujhe bhi sikha do

  14. sdasd on 2016-11-29

    aage jao baba hm kyo example de

  15. Apalna on 2016-11-29

    hm to ni sikha re kal lo jo krna h

  16. aaru on 2016-11-29

    tumhe to aata hai sikha sakte ho

  17. nacho on 2016-11-29

    toh nacho tm jake hamara toh ni chala

  18. Apalna on 2016-11-29

    vidhya se pucho usko jada ata h

  19. pandeyji on 2016-11-29

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

  20. kallo on 2016-12-7

    lo kallo bt

  21. Rahul Yadav on 2016-12-22

    bata do bhai punay milega

  22. DB Fucker on 2016-12-27

    maa chuda na fir apni

Leave a comment