We use java for a lot of our jobs, and using it to access SQL Server is no exception. We wanted to have controlled access, and avoid “user sprawl” across servers. As such, we use Windows authentication from our java programs. Below is an example:
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
public class connSqlServer {
public static void main(String[] args) {
try {
Connection con = DriverManager.getConnection("jdbc:sqlserver://STSQL01.fake_domain:1433;databaseName=MSDB;integratedSecurity=true;");
Statement stm = con.createStatement();
ResultSet rst = stm.executeQuery("select * from msdb.dbo.backupset");
while (rst.next()) {
System.out.println(rst.getString("database_name"));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}