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 whatismyip.com 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 whatismyip.com

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
Class.forName("com.mysql.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 (rs.next()) {
String tst_title = rs.getString("tst_title");
System.out.println(tst_title);
}
st.close();
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
try {
if (stmt != null) {
conn.close();
}
} catch (SQLException se) {
} // do nothing
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
} // 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’@’12.21.23.65’ (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3923)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1273)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
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(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at in.javadomain.CPanelRemoteMySqlConnect.main(CPanelRemoteMySqlConnect.java:28)

 

 

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.

270 total views, 1 views today

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *