Created
July 25, 2018 09:37
-
-
Save wolkenschieber/3e9939f8acb3da56890400c2388dd5f8 to your computer and use it in GitHub Desktop.
MSSQL Test JDBC connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mssql.test; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.Statement; | |
public class ConnectTest | |
{ | |
public static void main( String[] args ) throws ClassNotFoundException | |
{ | |
String host = System.getProperty( "ms.host" ); | |
String port = System.getProperty( "ms.port" ); | |
String database = System.getProperty( "ms.database" ); | |
String user = System.getProperty( "ms.user" ); | |
String password = System.getProperty( "ms.password" ); | |
String connectionUrl = String.format( | |
"jdbc:sqlserver://%s:%s;databaseName=%s;user=%s;password=%s", host, | |
port, database, user, password ); | |
Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" ); | |
try (Connection con = DriverManager.getConnection( connectionUrl )) | |
{ | |
String SQL = "SELECT 1"; | |
Statement stmt = con.createStatement(); | |
ResultSet rs = stmt.executeQuery( SQL ); | |
System.out.println( rs ); | |
} | |
catch( Exception e ) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment