This post shows how to connect (and read from) a Microsoft SQL Server database using a JDBC driver. It doesn’t use Hibernate or any other ORM.

The driver I used to connect to the SQL Server database is the open-source jTDS driver. This seems to be the recommended driver at the moment, which is kinda surprising as it isn’t an official Microsoft driver. Anyhow, download the driver and add it to your project. Here’s the code to connect to an SQL Server and read something from a table:

    public void readSomething() throws ClassNotFoundException, SQLException
    {
        Class.forName( "net.sourceforge.jtds.jdbc.Driver" );
        Connection conn = DriverManager.getConnection( "jdbc:jtds:sqlserver://ip addr", "username", "password" );

        try
        {
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery( "select * from TABLE" );
            while ( rs.next() )
            {
                System.out.println( rs.getString( "column_name" ) );                
            }   
            rs.close();
        }
        finally
        {
            conn.close();
        }
    }