get MS SQL Server Connection via ODBC - Java java.sql

Java examples for java.sql:SqlServer

Description

get MS SQL Server Connection via ODBC

Demo Code


//package com.java2s;
import java.sql.*;

public class Main {
    public static Connection getMSSQLCon(String server, String db,
            String uid, String pwd) {
        try {/*from   w  w w. j a  v  a2  s .c o m*/
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            // set this to a MS SQL Server
            String database = "jdbc:odbc:Driver={SQL Server};Server="
                    + server;
            database += ";Database=" + db + ";UID=" + uid + ";PWD=" + pwd
                    + ";";
            // now we can get the connection from the DriverManager
            return DriverManager.getConnection(database, "", "");
        } catch (Exception e) {
            return null;
        }
    }
}

Related Tutorials