JDBC sqlserver connection string

By FoxLearn 10/8/2024 9:53:16 AM   5
To connect to a SQL Server database using JDBC, you can use the following connection string format.

A JDBC connection string is a URL that includes the database host, port, and other parameters needed to connect to a SQL Server database.

The general format of a JDBC SQL Server connection string is structured as follows:

jdbc:sqlserver://<server_name>:<port>;databaseName=<database_name>;user=<username>;password=<password>;
  • <server_name>: The name or IP address of the SQL Server instance.
  • <port>: The port number (default is 1433).
  • <database_name>: The name of the database you want to connect to.
  • <username>: Your SQL Server username.
  • <password>: Your SQL Server password.

Connect to the default database on the local computer by using integrated authentication

jdbc:sqlserver://localhost;integratedSecurity=true;

Connect to a named database on a remote server

jdbc:sqlserver://localhost;databaseName=AdventureWorks;integratedSecurity=true;

Connect on the default port to the remote server

jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks;integratedSecurity=true;

Connect by specifying a customized application name

jdbc:sqlserver://localhost;databaseName=AdventureWorks;integratedSecurity=true;applicationName=MyApp;

When specifying the SQL Server instance in a JDBC connection string, you typically use one of the following formats, not both

Using the instance name (default port 1433)

jdbc:sqlserver://<server_name>\\<instance_name>;databaseName=<database_name>;user=<username>;password=<password>;

For example:

jdbc:sqlserver://FOXLEARN\SQLEXPRESS;databaseName=AdventureWorks;

Using the server name and port number

jdbc:sqlserver://<server_name>:<port_number>;databaseName=<database_name>;user=<username>;password=<password>;

For example:

jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks;

Using JDBC URL with host, port, and database name, using Windows Authentication.

jdbc:sqlserver://192.168.1.17:1443;databaseName=AdventureWorks;integratedSecurity=true

Make sure you have the SQL Server JDBC driver in your project’s classpath to use this connection string.