How to connect to CPanel Remote MySql in Java ?

How to connect to CPanel Remote MySql in Java ?

When you are having your code in local and you want to connect your remote mysql which is in your cpanel, then you need to follow below steps to connect to it.


Step 1: Login to CPanel and Navigate to Remote MySql


Login to cpanel and navigate to remote mysql which is under databases

CPanel Login -> Databases -> Remote MySql -> Click



Step 2: Find your local system IP


Open to know your local system IP



Step 3: Add Host to Remote MySql


Add the IP address host to remote mysql which you got from

Host (% wildcard is allowed): -> Add IP address -> Add Host



Step 4: Find your website’s IP Address

Hope you already know your own website’s IP address. But if you don’t know then find it in the below website

Get IP Address Of your website



Step 5: Java Program to connect to CPanel Remote MySql


-> DB URL : jdbc:mysql://YOUR_DOMAIN_OR_IP_ADDRESS/test_database
test_database – Name of the database.


Note: Ensure you have MySql Java connect Jar as well.


How to connect to CPanel Remote MySql in Java ? – Full Program:


package in.javadomain;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class CPanelRemoteMySqlConnect {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://YOUR_DOMAIN_OR_IP_ADDRESS/test_database";
// test_database -> Name of the database

// Database credentials
static final String USER = "DB_USER_ID";
static final String PASS = "DB_PASSWORD";

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// STEP 2: Register JDBC driver

// STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");

// STEP 4: Execute a query
// stmt = conn.createStatement();
String query = "SELECT tst_title FROM test_table";

// create the java statement
Statement st = conn.createStatement();

// execute the query, and get a java resultset
ResultSet rs = st.executeQuery(query);

// iterate through the java resultset
while ( {
String tst_title = rs.getString("tst_title");
} catch (SQLException se) {
// Handle errors for JDBC
} catch (Exception e) {
// Handle errors for Class.forName
} finally {
// finally block used to close resources
try {
if (stmt != null) {
} catch (SQLException se) {
} // do nothing
try {
if (conn != null) {
} catch (SQLException se) {
} // end finally try
} // end try



If suppose you are getting the below error then either you added wrong local system ip host to remote MySql host section (refer step 3) or you are in dynamic IP area section, which is generating the dynamic IP addresses. If dynamic IP address then you can use “*”, but still it is not secured and not recommended as well.

Eg: 12.21.23.* [not secured and not recommended]



Connecting to a selected database…
java.sql.SQLException: Access denied for user ‘tstusr’@’’ (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(
at com.mysql.jdbc.SQLError.createSQLException(
at com.mysql.jdbc.MysqlIO.checkErrorPacket(
at com.mysql.jdbc.MysqlIO.checkErrorPacket(
at com.mysql.jdbc.MysqlIO.checkErrorPacket(
at com.mysql.jdbc.MysqlIO.secureAuth411(
at com.mysql.jdbc.MysqlIO.doHandshake(
at com.mysql.jdbc.ConnectionImpl.createNewIO(
at com.mysql.jdbc.ConnectionImpl.<init>(
at com.mysql.jdbc.JDBC4Connection.<init>(
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(
at com.mysql.jdbc.ConnectionImpl.getInstance(
at com.mysql.jdbc.NonRegisteringDriver.connect(
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at in.javadomain.CPanelRemoteMySqlConnect.main(



If you are facing any issue still connecting to remote mysql from your local java program, feel free to write in below comments section to help you.

1,102 total views, 1 views today

About Mirthbees 679 Articles
Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in


  1. Hey Can we use in Android to connect, update & insert in mysql db from android?
    If yes can you show me or send any reference or sample.

    If we connect no need to use Php api to connect to mysql db which we are using in web application

Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.