Unable to Connect with my Database 000webhost.com - java

try {
String connectionURL = "jdbc:mysql://sql4.000webhost.com/a7291194_xxx";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "a7291194_xxx", "xxx");
if(!connection.isClosed())
System.out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}catch(Exception ex){
System.out.println("Unable to connect to database"+ex);
}
With the following connector code I am trying to connect with the database on 000webhost.com but it gives me an error:
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.
I am trying my best for the first time as I am totally new to webhosting so please help me thanks in advance!

its same either on your local or on server
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://DomainName OR IP:3306/databasename", "username", "password");

At 000webhost.com remote MySQL connections are disabled for security and server performance reasons.
If you upgrade your account, MySQL connections will be enabled.

Related

How can I connect to db2 database remotely

I am using following java application to connect db2 database for localhost and code is:
String jdbcClassName="com.ibm.db2.jcc.DB2Driver";
String url="jdbc:db2://localhost:50000/sample";
String user="admin";
String password="admin";
try {
//Load class into memory
Class.forName(jdbcClassName);
//Establish connection
connection = DriverManager.getConnection(url, user, password);
stmt = connection.createStatement();
}
catch (SQLException e) {
e.printStackTrace();
}
The problem is that when application and db2 is running on the same machine, then it is working, but if db2 database is on another machine then it is not working. I am using ip in my url which is:
String url="jdbc:db2://192.168.1.68:50000/sample";
And it's giving the following error:
com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][3.59.81] Exception java.net.ConnectException: Error opening socket to server /192.168.1.64 on port 50,000 with message: Connection timed out: connect. ERRORCODE=-4499, SQLSTATE=08001
How can I connect to the database remotely?
Check if your private or public network firewall is on ?? If it is on then instead of doing it off make an inbound rule and then try to connect it with DB2 database from another system.

Connection refused: connect, can't connect database mysql

I can't connect remote database. When I connect my own database on localhost, it connects. What's wrong?
Exception in thread "main" java.lang.IllegalStateException: Cannot connect the database!
Caused by: 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.
Caused by: java.net.ConnectException: Connection refused: connect
Java Code:
String url = "jdbc:mysql://ipadress:3306/database?autoReconnect=true&useSSL=false";
String username = "username";
String password = "password";
String query = "SELECT * FROM database";
System.out.println("Connecting database...");
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
try (Connection connection = DriverManager.getConnection(url, username, password)) {
System.out.println("Database connected!");
//Uruchamiamy zapytanie do bazy danych
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
}
connection.close();
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
}
I can login to database on PHPMyAdmin, I have no root account, it's my friend's database. I checked if port 3306 is open here: http://www.yougetsignal.com/tools/open-ports/ and it's closed. Where can I open it? In router settings in "port forwarding"? What private IP and type(TCP or UDP) should I set to open this port?
(Apologies if this answer is incomplete, but there is too much that does not fit in comments)
1) Don't ignore exceptions. This is bad : with // handle the error and nothing else in your catch block, in case of error there, your code will not report the error, and will move on with the execution (it should exit/break/return, depending on where that piece of code is).
2) I think checking "SHOW GLOBAL VARIABLES LIKE 'PORT';" is not enough. Ask your friend if the database daemon actually listens to port 3306 on a network interface that you can reach. MySQL can be configured with networking disabled (skip-networking), or enabled but only for local machine (bind-address=127.0.0.1 or localhost -- it should be bind-address=0.0.0.0 or bind-address=hostname or public IP address...).
To check for that yourself, if you are on linux, try with nc or telnet (install nc if you don't have it): nc remotehost 3306. If you get "connection refused", the error is definitely not in your java code, but in the server setup.

Connect an online mysql database to a Java application

How to connect online mysql database to a java application?I have used below code to get the connection to my database.
private static Connection con;
public static Connection connect() throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://host/database";
con = DriverManager.getConnection(url, "username", "password");
return con;
}
but this gives me following error everytime.
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.
What should i do?

Connect to mysql remote database using JDBC?

I'm a java beginner and I'm working on a simple application which connects to a remote mysql database using JDBC. I've tested it locally and it works just fine, however I cannot get it to work on for my remote server.
I don't think its of much use but heres the code:
Connection connection = null;
String dburl = "jdbc:mysql://314159265:3306/Db_Name";
String userName = "user";
String passWord = "password";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(dburl, userName, passWord);
Statement st = connection.createStatement();
String query = "INSERT INTO Example (`TestColumn`) VALUES('hello')";
int rsI = st.executeUpdate(query);
System.out.println("Hi");
}catch (Exception e) {
System.out.println(e);
} finally {
if (connection != null) {
try {
connection.close();
System.out.println("Database connection terminated");
} catch (Exception e) { /* ignore close errors */ }
}
}
When I run this, I get the following message:
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.
I'm pretty sure it must be some kind of server configuration issue.
Notes:
Username, password, IP, database name, etc. are just examples.
This could be a firewall problem, or a configuration problem. But I don't think it is a coding problem at all - you need to start troubleshooting the connection.
Trouble shoot by attempting to use third party client apps to connect to mysql. This will indicate whether it is configured for external access. Although it doesn't ensure that JDBC is visible from the outside, it does rule out some potential firewall problems.
Follow this guide to help you mess with your configurations
Remote MYSQL Database Access
If you are still stuck, it could be a coding problem so check out this page:
How to connent to a remote mysql database with java?
P.S. I am assuming you are using unix as the operating system.
I guess 314159265 could be replaced by some address....
like jdbc:mysql://localhost:3306/
or jdbc:mysql://127.0.0.1:3306/

Translating PHPMyAdmin MySQL connection configuration to JDBC

I have PHPMyAdmin connecting to a remote MySQL server, using those parameters:
$cfg['blowfish_secret'] = 'ba17c1ec07d65003';
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = '*remoteServer*:3306';
$cfg['Servers'][$i]['extension'] = 'mysql';
$cfg['Servers'][$i]['controluser'] = '*user*';
$cfg['Servers'][$i]['controlpass'] = '*password*';
I'm trying to connect to this MySQL server in Java, neither of those work:
final String url ="jdbc:mysql://*remoteServer*:3306/*user*";
Connection conn = DriverManager.getConnection(url, "*user*", "*password*");
and, with ":3306" removed :
final String url ="jdbc:mysql://*remoteServer*/*user*";
Connection conn = DriverManager.getConnection(url, "*user*", "*password*");
I'm getting this error:
SQLException: 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.
SQLState: 08S01
VendorError: 0
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)
...
I think it comes from the "blowfish_secret" (which is just a random cookie) and "auth_type"="cookie" not being accounted for in Java.
How do I fix this?
Also, do I need to tell MySQL that he has to accept to talk to the jdbc driver? (if yes, I'm screwed, I don't have access to the MySQL config files)
blowfish_secret and auth_type are used only as part of the phpmyadmin software, they have nothing to do with a mysql connection.
Do you have firewalls allowing access to MySQL from the machine running java and do you have grants set up to access that user from that specific host?

Categories