
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java MySQL Connection with IP Address
In this article, we will learn how to connect a Java application to a MySQL database hosted on a specific IP address. By specifying the IP address in the connection URL, we can directly connect to the database even if it's on a different machine. We'll use the DriverManager.getConnection() method to initiate the connection.
Steps to connect a MySQL database via IP Address
Following are the steps to connect a MySQL database via IP Address ?
- Import Connection and DriverManager classes from the java.sql package to enable database connectivity.
- Create a String variable (hostURL) that specifies the JDBC MySQL connection URL, including the IP address (192.168.43.144), port (3306), database name (web), and useSSL=false to disable SSL warnings.
- Define a Connection object and set it to null initially. This will later hold the connection to the database.
- Establish Connection in a Try-Catch Block:
- In the try block, call DriverManager.getConnection() with hostURL, the username (root), and the password (123456) to establish a connection.
- If the connection is successful, print "Connection successful via IP address" to confirm.
- To handle exceptions, we will use a catch block to manage any issues that arise during the connection process. e.printStackTrace() will print the error details if a connection error arises.
Java program to connect a MySQL database via IP Address
Set the JDBC MySQL URL in the DriverManager.getConnection to connect with the IP Address. Following is the code to connect using IP Address ?
import java.sql.Connection; import java.sql.DriverManager; public class JavaIP { public static void main(String[] args) { String hostURL = "jdbc:mysql://192.168.43.144:3306/web?useSSL=false"; Connection con = null; try { con = DriverManager.getConnection(hostURL, "root", "123456"); System.out.println("connection successful via ip address"); } catch (Exception e) { e.printStackTrace(); } } }
Output
This will produce the following output ?
connection successful via ip address
Here is the snapshot of the output ?
Code explanation
The above program, starts by defining a hostURL string that specifies the MySQL database connection details, including the IP address (192.168.43.144), port number (3306), and database name (web). The useSSL=false parameter is added to avoid SSL-related prompts. In the main method, we attempt to establish a database connection by bypassing the hostURL, username (root), and password (123456) to DriverManager.getConnection(). If the connection is successful, a message is printed to confirm; otherwise, the catch block captures any exceptions and prints the stack trace to help diagnose connection issues.