Skip to content

Instantly share code, notes, and snippets.

@guyinacube
Created November 27, 2017 11:53
Show Gist options
  • Save guyinacube/5bb53d088f31016e50cfc4f5a5c2d11f to your computer and use it in GitHub Desktop.
Save guyinacube/5bb53d088f31016e50cfc4f5a5c2d11f to your computer and use it in GitHub Desktop.
SQL JDBC Driver connectivity test
import java.sql.*;
import java.util.logging.*;
import java.io.IOException;
public class JDBCConnectionTest{
public static void main(String[] args) {
//Logger logger = Logger.getLogger("com.microsoft.sqlserver.jdbc");
//logger.setLevel(Level.ALL);
//try
//{
// FileHandler fileHandler = new FileHandler("jdbc.log");
// fileHandler.setFormatter(new java.util.logging.SimpleFormatter());
// logger.addHandler(fileHandler);
//}
//catch(IOException e)
//{
// e.printStackTrace();
//}
// Building the Connection URL
// https://docs.microsoft.com/sql/connect/jdbc/building-the-connection-url
// Setting the Connection Properties
// https://docs.microsoft.com/sql/connect/jdbc/setting-the-connection-properties
// **** Update the Server Name and Database Name and user credentials ****
// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://myserver;databaseName=mydatabase;user=sqluser;password=Password123!";
// 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);
DatabaseMetaData dbmd = con.getMetaData();
System.out.println("dbmd:driver version = " + dbmd.getDriverVersion());
System.out.println("dbmd:driver name = " + dbmd.getDriverName());
System.out.println("db name = " + dbmd.getDatabaseProductName());
System.out.println("db ver = " + dbmd.getDatabaseProductVersion());
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment