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.
Table of Contents
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:
[java]
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
}
}
[/java]
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.
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
I think you are looking this:
http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
Hope this helps.
Here again you need to change the db connection alone like how we explained in this post to connect to remote mysql.
run:
Connecting to a selected database…
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:355)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at tester.CPanelRemoteMySqlConnect.main(CPanelRemoteMySqlConnect.java:36)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.(Socket.java:434)
at java.net.Socket.(Socket.java:244)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:305)
… 15 more
BUILD SUCCESSFUL (total time: 22 seconds)