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 {
		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");	
		Connection conn = DriverManager.getConnection("jdbc:sqlserver://HOSP_SQL1.company.com;user=name;password=abcdefg;database=Test");
		System.out.println("test");
		Statement sta = conn.createStatement();
		String Sql = "select * from testing_table";
		ResultSet rs = sta.executeQuery(Sql);
		while (rs.next()) {
			System.out.println(rs.getString("txt_title"));
		}
	}
}
Category >> Database  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • DB Fucker

    maa chuda na fir apni

  • 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
    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
    Paris is the capital and most populous city of France.

    Tokyo
    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 {
    Class.forName(“com.mysql.jdbc.Driver”);
    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 https://confluence.atlassian.com/display/DOC/Configuring+a+SQL+Server+Datasource+in+Apache+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.