I'm working on an openshift tomcat project by using mysql as backend I am trying to connect database and it is not connecting and shows error message "Communications link failure Last packet sent to the server was 0 ms ago" I have connected database using this code
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException ex){
//return(ex.getMessage());
}
con = DriverManager.getConnection(MYSQL_DATABASE_HOST,MYSQL_USERNAME,MYSQL_PASSWORD);
} catch (Exception ex) {
return(ex.getMessage());
}
return "success";
I've provided the database url by hardcoding the database IP and port,when I print System.getenv("OPENSHIFT_MYSQL_DB_HOST"); I'm getting null value. please tell me anyone the error
the problem you are facing occurs when your java code is unable to connect to MySQL server. There is one more way, check if your MySQL is up and running. You can get your MySQL server's ip by
rhc port-forward -a <mysql_gear_name>
You can then explicitly give that IP in DriverManager.getConnection
From the command line, run rhc port-forward to access databases or internal ports directly.
Eg :
rhc port-forward -a <name of the gear including mysql db>
Related
I have a problem when I try to connect to SQL Server with Java. I have the next error:
The connection to the host DESKTOP-C0SCI39, named instance failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
My firewall is off and I don't know what is wrong(mssql-jdbc-8.4.1.jre14 is included). This is my Java code:
String url = "jdbc:sqlserver://DESKTOP-C0SCI39\\;databaseName=students";
try {
Connection connection = DriverManager.getConnection(url);
System.out.println("Connected to MS SQL");
} catch (SQLException throwables) {
System.out.println("Error!");
throwables.printStackTrace();
}
Try this solution, I have ran into similar problems in the past and have found that these help.
Check that the JDBC Driver is imported in the project. (Preferred - 5.1.48)
Check that your SQL Server is up and running (Check if SQL Service is running)
Check if you entered your connection string properly
eg: private String URL = "jdbc:mysql://localhost:3306/dbName"; //database url
Try adding a useSSL=false to the end of your connection string dbName?useSSL=false
Open your server with a server management software such as SQL Server Management Studio to see if your SQL Server is working as it should
Your database is not running where you think it is running. Verify the port and location of the server and try again.
Please go through below code that I wrote in Java Swing to connect H2 Database in LAN, I did Google but not get proper solution.
try {
//192.168.0.200 is Partner IP Address
Class.forName("org.h2.Driver");
Connection connection = DriverManager.getConnection("jdbc:h2:tcp://192.168.0.200/~/testingDB", "sa", "");
System.out.println("Connected" + connection);
} catch (Exception e) {
System.out.println("Here" + e.toString());
}
I am trying to connect H2 Database that are installed on another computer using IPV4 address but I am getting below error.
Hereorg.h2.jdbc.JdbcSQLException: Remote connections to this server are not allowed, see -tcpAllowOthers [90117-184]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.message.DbException.get(DbException.java:144)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:83)
at java.lang.Thread.run(Unknown Source)
I given below command in partner as well as my computer but still I am getting the same error.
http://www.windows-commandline.com/enable-remote-desktop-command-line/
Please help me to find the solution.
You need to start the H2 server with the -tcpAllowOthers option. This option is not enabled by default for security reasons.
I trying to conncet to my MySQL Database hosted in a virtual machine, but it doesn't work
for me.
Here is my setup:
Newest Ubuntu Server witch Apache2 mysql installed and is working
Database "feedback" with the table "test" set up and filled with test data
The network adapter is bridged. The IP of the server is (if I type in ifconfig) 10.0.0.1 and the IP of my pc is 10.0.0.4.
Port 3306 was manually opened. The jar mysql connector java 5.1.21-bin.jar is a Referenced Libary
Here is the Java Code:
import java.sql.*;
public class Main {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Sucess");
Connection con = DriverManager.getConnection("jdbc:mysql://10.0.0.1/feedback","root","myrootpassword");
con.setReadOnly(true);
} catch (Exception e) {
System.err.print("NO CONNECTION");
}
}
}
I hope that someone could help with this, because I'm working on this problem for a while.
Thanks
Just two ideas:
Is MySQL configured to listen on all addresses and not only local ones ? What's the value of the bind-address configuration directive ? 0.0.0.0 means "listen on all interfaces", while 127.0.0.1 means "listen on the localhost interface, for access from this host only".
Maybe you can extract some details about the issue in your catch statement ?
Hope this helps !
If i am correctly understand a question ,To connect VM database,then check for the following steps
1.jdbc:mysql://10.0.0.1/feedback.
2.Make sure there is no firewall blocking the access to port 3306.
3.make sure the user you are connecting with is allowed to connect from this particular hostname.
Can someone explain to me why this line works:
conn = DriverManager.getConnection("jdbc:mysql://myWebsite.com:3306/schemaName?user=userX&password=passwordX");
But this line does not:
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/schemaName?user=userX&password=passwordX");
I get a Communications Link Failure when attempting to access through the localhost (or 127.0.0.1). However, I'm able to access the database via localhost through PHP and the MySQLQuery Browser and MySQL Aministrator.
If needed here's the entire method I'm using:
public Database() throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
try {
conn = DriverManager.getConnection("jdbc:mysql://myWebsite.com:3306/schemaName?user=userX&password=passwordX");
// Next line does not work.
// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/schemaName?user=userX&password=passwordX");
} catch (SQLException ex) {
displaySQLException(ex); // Seperate routine to display errors.
}
}
Thanks for any help,
Richard
It's possible your mysqld is binding specifically to the ethernet interface instead of all interfaces (0.0.0.0) or the localhost interface (127.0.0.1).
On a *nix platform you can check which interface the daemon is listening on with the following command:
$ netstat -ln|grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
In second code:
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/schemaName?
user=userX&password=passwordX");
It will try to connect to mysql on machine on localhost (on which the code is running). In your case it might be possible that mysql on your machine or from where you are running the code is not available or stopped or usename/password you are passing are not valid or schemaname does not exist. But on myWebsite.com it is up.
There is nothing wrong in your code. Make sure mySql is installed and running and username/password are valid, a schema with provided schemaname exists on machine on which you run this code with localhost.
This might happen for many reasons, like
MySQL server might be stopped on the target machine
MySQL might be configured not to accept remote connections
The firewall might be blocking remote connections on port 3306
In my case, I faced this error when trying to connect to mysql-server running inside a container on a Ubuntu host VM from the same host.
Example:
If my VM name is abc.company.com, the following jdbc URL would not work:
jdbc:mysql://abc.company.com:3306/dbname
Above jdbc url would work fine from other machines like xyz.company.com but just not abc.company.com.
where as the following jdbc URL would work just fine on abc.company.com machine:
jdbc:mysql://localhost:3306/dbname
which led me to check the /etc/hosts file.
Adding the following line to /etc/hosts fixed this issue:
127.0.1.1 abc.company.com abc
This seems to be an OS bug that requires us to add these on some Ubuntu versions.
Reference: https://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution
Before trying this, I had tried all other solutions like GRANT ALL.., changing the bind-address line in mysql.cnf.. None of them helped me in this case.
i am using below code
try {
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root","root"); // cust is the DSN Name
} catch (Exception e) {
System.out.println("Exception "+e);
}
got this exception during run time...
tried netstat -a in command prompt to check if mysql is running on port 3306 below is the trace...
Proto Local Address Foreign Address State
TCP user:epmap user:0 LISTENING
TCP user:microsoft-ds user:0 LISTENING
TCP user:3306 user:0 LISTENING
Well, googling your problem i have come to this solution:
Try reseting your user password executing the next statement at your mysql command line client:
SET PASSWORD FOR 'root'#'localhost' = OLD_PASSWORD('root');
As it seems it kind of changes the authentication method at the mysql server.
Let me know if this have worked.
Other option is to check the compatibility between the version of your MySQL database and the MySQL JDBC driver you are using.