Technical · Database

Connection to SQL2005 with Java

September 19, 2007

Database

Connection to SQL2005 with Java

Ivanov Cepeda from Microsoft Colombia asks me how to connect a Java application to SQL Server 2005. With the help of some of the guys from Gattaca's COS (Commercial Open Source) group we have reviewed the topic and we have the following alternative:

1. Download from this URL the JDBC Driver for SQL 2005.

2. Follow the instructions in the Install.txt file very carefully. This driver is for both Windows and Linux operating systems; for Linux it comes with the tar.gz, for Windows the .exe.

3. Now you can write a class like this

 
package persistencia;

import java.util.*;
import java.sql.*;


public class Consultas {

    
    
    public static void main(String[] args) {
        
        // Create a variable for the connection string.
        String connectionUrl = "jdbc:sqlserver://192.168.16.19:1433;forwardReadOnlyMethod=direct" +
            "databaseName=MyDataBase;user=UserSQL;password=xxxxxxxx";

        // Declare the JDBC objects.
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        
            try {
                // Establish the connection.
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    con = DriverManager.getConnection(connectionUrl);
            
                    // Create and execute an SQL statement that returns some data.
                    String SQL = "SELECT TOP 10 * FROM ApplicationUser;";
                    stmt = con.createStatement();
                    rs = stmt.executeQuery(SQL);
            
                    // Iterate through the data in the result set and display it.
                    while (rs.next()) {
                        System.out.println(rs.getString(4) + " " + rs.getString(6));
                    }
            }
        
        // Handle any errors that may have occurred.
        catch (Exception e) {
            e.printStackTrace();
        }

        finally {
            if (rs != null) try { rs.close(); } catch(Exception e) {}
                if (stmt != null) try { stmt.close(); } catch(Exception e) {}
                if (con != null) try { con.close(); } catch(Exception e) {}
        }
    }

}

I hope this helps, and thanks to Carlos Andres Valencia, who took time away from his 11 projects to help us solve this.

Juan Carlos Peláez

Enjoying this? Get more like it, once a week.

MCTS

Member of the Microsoft Andean Speaker Group

Member of Influencers Group Colombia.

Keywords: SQL Server 2005, JDBC, JDBC Driver for SQL Server 2005, Connectivity, JAVA, SQL2005, SQL Server 2005, Drivers, Interoperability

Need Expert Help?

Let's discuss how I can help with your technical challenges

Schedule a Consultation

Get the next one in your inbox

More like this, once a week. The Digest covers AI systems and agents in one weekly edition.

Confirm by email to complete your subscription. Unsubscribe anytime.

Related reading

More in the Digest — a weekly roundup of AI and agents news — or start with AI Strategy. Want help with this? Tell me about your needs →