Wednesday, March 18, 2009

Connecting to a MS SQL Server which is older than version 7.0 using Java

Hi All,

This would be my first Java tweak in this blog.

Problem: Same as title.

Common Thought: There is a common thought by most of the Java Developers reading this blog. They may think what is the big deal in this.

Complexity is involved in: It is very common in Java to connect to the Oracle Databases. But as you see in the title, this post is about trying to connect to a MS SQL Server. People might think that this is also a very common problem solved by all Dev folks.

But as you see, things get complex if you want to connect to a MS SQL Server which is older than version 7.0.


To solve this complexity, here comes jTDS.

"jTDS is an open source 100% pure Java (type 4) JDBC 3.0 driver for Microsoft SQL Server (6.5, 7, 2000 and 2005)", unquote from [1].

Once you get this driver (jar file) into your Eclipse/NetBeans IDE's project's build path, then the rest of it is known to you all.

Now I would just write a code that would connect to the MS SQL Server and just executes a "select" on a table.

Please note the Class.forName driver value. This is most important.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class MailAttachData {

static Connection con=null;
con = DriverManager.getConnection("jdbc:jtds:sqlserver://your_host_name:port number/table_name","userName","passwd");

catch(Exception e)

public void getData()
String query = "select * from Contactid";
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(query);

String emails = null;

emails = rs.getString(TABLE_COLUMN_INDEX #);


public static void main(String args[])

Let me know your problems if you face any.

Let me know your feedbacks too.

Take care all. Keep visiting and waiting for some more posts.

No comments:

Post a Comment