jdbc connect failed ,but MysqlWorkBench can connect successfully [duplicate] - java

This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(51 answers)
Closed 6 years ago.
I downloaded and installed mysql-5.7.17-macos10.12-x86_64 ,the workbench and the mysql-connector-java-5.1.40-bin.jar .
I use the the following code to coonect mysql database,but it throws the com.mysql.jdbc.exceptions.jdbc4.CommunicationsException.
Connecting to database: jdbc:mysql://127.0.0.1:3306/demo?user=root&password=root
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.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
I can use the username and password to connect the database in mysqlworkbench and the MYSQL Server Status is running.Does it matter that I have mysql of other version?The version of Mysql in use is 5.7.17.
try {
Class.forName("com.mysql.jdbc.Driver");
String conn = "jdbc:mysql://" + d_server_name + "/" +
d_db_name+"?user="+d_user_name+"&password="+d_password;
System.out.println("Connecting to database: " + conn);
d_connect = DriverManager.getConnection(conn);
System.out.println("Connected to database");
return d_connect;
} catch(Exception e) {
throw e;
}

Try with below format.
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/database","root", "root");

Related

SQL Exception : Too many database connection [duplicate]

This question already has answers here:
Java using JDBC - Too many connections?
(3 answers)
Closed 5 years ago.
When I try to connect to the database for a query, there is an exception occurred.
"SQL Exception : com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
public class DBConnection {
public static java.sql.Connection connect() throws ClassNotFoundException {
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/dmr";
String username = "root";
String password = "root";
try {
// Establishing connection
con = DriverManager.getConnection(url,username,password);
}
catch (SQLException ex) {
System.out.println("SQL Exception : "+ ex);
}
if(con != null){
System.out.println("****CONNECT TO THE DATABASE*****");
}
else{
System.out.println("####NOT CONNECT TO THE DATABASE");
}
return con;
}
}
If you using the local installation of MySQL db then use the below commands to check the how many connection are currently open.
mysql> show status like 'Conn%';
mysql> show status like '%onn%';
To get process list use: mysql> show processlist;
You can kill the unnecessary connections.
If you are using a database server maintained by other party then get their help to check and kill the unnecessary connections. There is property in mySQL server to specify the max number of connection allowed. You have increase it if necessary.
On Java side, i think after you done with database operation you should call con.close in finally block. Am not sure if you are doing that.
"Too many connections" error comes because server connected maximum limit of connection which server can be donem so server can not connect to new client. for solve this problem you need to restart the database server.

MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up [duplicate]

This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(51 answers)
Closed 5 years ago.
I'm having trouble connecting to my database. I think there is a setting somewhere that I don't know about and needs to be changed. I have some really basic code here:
public static void main(String[] args)
{
try
{
Connection con = DriverManager.getConnection("jdbc:mysql://IP:3306/TABLENAME?autoReconnect=true","USERNAME", "PASSWORD");
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Obviously with the login info. And it causes this error:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
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
I've looked through about 10 different posts about the same problem and tried their solutions and still couldn't get it to work.
This problem occurs when the MySQL service is not running and you're trying to connect to the mysql database.
To resolve this in Windows,
Start >> type 'services' and open it.
Find the MySQL service. Probably with name MySQLxx. xx being the version.
eg. MySQL56, MySQL57 etc
Start the service and now try to connection to database.
Increase Mysql max connection,
SET GLOBAL max_connections = 1500;
If still getting an error then queries might be taking too long to respond.
In that case increase attempt while creating jdbc connection,
&maxReconnects=XX
You have to use database name instead of table name

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?

Unable to Connect with my Database 000webhost.com

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.

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