Updated March 8, 2023
Introduction to JDBC connection string
JDBC connection string is also known as the JDBC URL, this will instruct the database that how to connect the remote database server. After connecting to the database server using the connection string the connection is open for two hours, after completing two hours it will be disconnected from the database server. Hostname, database name, username, pass, word, and driver name are the important parameter while making the connection string of any database. The connection string is an important parameter to establish the connection between the java application and database server, JDBC URL connection string format is different for different databases.
Syntax:
Below is the syntax of the connection strings is as follows.
- The JDBC connection string for MySQL database –
Database_url = Jdbc:mysql://host_name/database_name
Database_driver = com.mysql.jdbc.driver
Database_username = name_of_user
Database_user_password = password_of_user
- JDBC connection string for PostgreSQL database –
Database_url = Jdbc:postgresql://host_name/database_name
Database_driver = org.postgresql.driver
Database_username = name_of_user
Database_user_password = password_of_user
- The JDBC connection string for SQL server database –
Database_url = Jdbc:microsoft:sqlserver//host_name:port_no/database_name = name_of_database
Database_driver = com.microsoft.jdbc.sqlserver.SQLServerDriver
Database_username = name_of_user
Database_user_password = password_of_user
- The JDBC connection string for DB2 database –
Database_url = Jdbc:as400://host_name/database_name;
Database_driver = com.ibm.as400.access.AS400JDBCDRIVER
Database_username = name_of_user
Database_user_password = password_of_user
Parameter description syntax of JDBC connection string.
- Database URL – This is nothing but the database URL which was used to connect the java application to the database server. We need to use different URL for the different types of databases. This parameter contains the information of database hostname; database hostname is nothing but the IP of the database server. It will also contain the information of the database port and database name.
- Database driver – This is an important parameter while using a connection string to connect the database server. We need to define different database drivers for different databases. This will contain the driver information of the database server.
- Database username – This parameter is also important while connecting to the database server using the connection string. This is the database user which we were using to connect the database server. The user which we were using has permission to connect to the database server. Without connecting permission, it will not connect to the database server. It will issue an error that permission is denied.
- Database user password – This is the password of the database user which was we have used in the connection string. We need to define the correct password of the user into the connection string, the wrong password will issue the error i.e. authentication failed.
How does connection string work in JDBC?
- As we know that database URL, database driver, database user, and database user password are the important parameter while making the connection string.
- To connect the database server, we need a correct password for the connection string.
- An incorrect password is not accepted in the connection string.
- The below example shows that we need the correct user password to connect the database server using the connection string.
- In the below example we have used username as Postgres and password as postgres123. But postgres123 is the incorrect password of user Postgres. So it will not be connecting to the database server it will show an error as authentication failed for using the user as Postgres.
Code –
public class Hello {
public static void main /* main method */ (String[] args) throws Exception {
final String DB_CON = "jdbc:postgresql://localhost:5432/pre_stmt";
final String USER_NAME = "postgres";
final String PASSWORD = "postgres123";
Connection conn = DriverManager.getConnection (DB_CON, USER_NAME, PASSWORD);
System.out.println ("Connected to the PostgreSQL Database");
} }
- In the above example, we have used the PostgreSQL database to test the connection to the database server using the connection string.
- We have used the database name as pre_stmt to check the database connection.
- At the time of creating the connection string for the database server, we need to use the correct driver class.
- To connect the java application to any database server we are required to load the jar file. For example to connect MySQL database we need to load the mysqlconnector.jar file.
- Without a database connector, we cannot connect to the specified database. It will be showing the error while connecting to the database server through the connection string.
- To connect the PostgreSQL database server, we need the postgresql-connector_version.jar file.
- We need to load the specified database driver version as per the java version which was we have using for our application.
Examples
The below example shows connection strings are as follows.
JDBC connection string connects to the MySQL database server –
- The below example shows the connection string connects to the MySQL database server are as follows.
- We have used username as root, password as MySQL@123, and database name as jdbc_con_mysql.
Code:
public class Hello {
public static void main /* main method */ (String[] args) throws Exception {
final String DB_CON = "jdbc:mysql://localhost:3306/jdbc_con_mysql";
final String USER_NAME = "root";
final String PASSWORD = "MySQL@123";
Connection conn = DriverManager.getConnection(DB_CON, USER_NAME, PASSWORD);
System.out.println("Connected to the MySQL Database server......");
}
}
JDBC connection string connects to the PostgreSQL database server –
- The below example shows connection strings connect to the PostgreSQL database server are as follows.
- We have used username as Postgres, password as Postgres, and database name as jdbc_con_postgresql.
Code:
class Hello {
public static void main /* main method */ (String[] args) throws Exception {
final String DB_CON = "jdbc:postgresql://localhost:5432/jdbc_con_postgresql";
final String USER_NAME = "postgres";
final String PASSWORD = "postgres";
Connection conn = DriverManager.getConnection (DB_CON, USER_NAME, PASSWORD);
System.out.println ("Connected to the PostgreSQL Database server......");
} }
JDBC connection string connects to the DB2 database server –
- The below example shows connection strings connect to the DB2 database server are as follows.
- We have used username as jdbc_test, password as Jdbc@1234, and database name as jdbc_con_db.
Code:
public class Hello {
public static void main /* main method */ (String[] args) throws Exception {
final String DB_CON = "jdbc:as400://localhost:50000/jdbc_con_db";
final String USER_NAME = "jdbc_test";
final String PASSWORD = "Jdbc@1234";
Connection conn = DriverManager.getConnection (DB_CON, USER_NAME, PASSWORD);
System.out.println ("Connected to the DB2 Database server......");
}
}
Conclusion
The connection string is used to connect from the java application to the specified database server. We need database hostname, database name, database username, database user password, and database port number to connect the database server using the connection string. We need a database driver to connect the specified database.
Recommended Articles
This is a guide to JDBC connection string. Here we discuss How does connection string works in JDBC along with the examples and outputs. You may also have a look at the following articles to learn more –