Just Replace ‘localhost’ with ‘127.0.0.1’ in code. For example replace string “jdbc:mysql://localhost:3306/snehal?user=root&password=root” with “jdbc:mysql://127.0.0.1:3306/snehal?user=root&password=root”.
See the image below.
Please let me know if this has been helpful to you or not.
didn’t work for me D:
Your comment didn’t work in my case:
——————Program——————————————————-
try {
Class.forName(“com.mysql.jdbc.Driver”);
String connectionUrl = “jdbc:mysql://127.0.0.1:3306/arbeitwagecalc?”
+ “user=root&password=noto”;
con = DriverManager.getConnection(connectionUrl);
} catch (SQLException e) {
System.out.println(“SQL Exception: ” + e.toString());
} catch (ClassNotFoundException cE) {
System.out.println(“Class Not Found Exception: ” + cE.toString());
}
—————————————————————————-
——————-Result———————————————————
run:
Exception in thread “main” java.lang.NullPointerException
SQL Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
at arbeitwagecalculationsystem.Main.main(Main.java:56)
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Java Result: 1
構築成功 (合計時間: 1 秒)
——————————————————————————————-
My Platform:
NetBeans 6.8 on Windows 7, MySQL Server 5.1 with use of ‘mysql-connector-java-5.1.13-bin.jar’.
Best regards,
12/9/2011
NOTO
Whats the error you are getting red? is it same? please check following
– is your database name is correct?
– is your MySQL server is on remote host. if yes, enter remote host ip instead of 127.0.0.1
– check if your MySQL service is started or not.
it didnt work for me
the solution for me was, to check the connection url.
I`v fount two mistakes in that:
String url = “jdbc:mysql :// ” + server + “/” + database;
The first one, the blank space after “mysql”. It caused an : “No suitable driver found for jdbc:mysql :// … ” error,
and the second mistake was, another blank space, after “://”. That caused the “com.mysql.jdbc.exceptions.jdbc4.CommunicationsException” error for me.
So, check the connection url, for unnecessary characters(blank spaces).
Helped to anybody ? 🙂
This solution didnt work for me.
It worked! After spending a couple hours on this, I found your page, and this is the one thing that resolved it for me. Thanks!
This is so stupid that the connector can’t take ‘localhost’!
Hi it worked for me thanks alot..
Nop does not work for me, i’m using mysql-connector-java-5.1.7-bin.jar and MySQL 5.1.31-1ubuntu2 and java version “1.6.0_16”
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Server VM (build 14.2-b01, mixed mode)
please help….
I didn’t get mine to work either.
The applications works in netbeans, but I get the error mentioned above as soon as I run the application for the jar file.
thanks mate it worked after 3 days of trawling the web!. thanks alot dude
It works for me…
Thanks a lot!!!!!!!
🙂
To those still having this issue. I noticed that if I included the name of the database in the connection string, I could connect but the connection would drop after about 20 seconds into the connection. I resolved this by:
1. Removing any unnecessary spaces in the connection string
2. Removing the name of the default database (you have to be happy with viewing all databases that your user has access to or connect with a user limited to a specific database)
3. Use IP address rather than hostname
-Wil Moore III
I have a Mysql server installed in a database server.I developed jsp application for connecting this database.The JSP application is in another webserver.Both are linux systems.But when i access it i get error as follow:
java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused
please kindly help me out of this.
Thanking You ,
didn’t work..
my url and database name are correct with no unwanted spaces. i am running the .java file as java application. have set mysql running in background using mysqld –console and also have apache tomcat running
this is total rubbish
My Solution for this error:
———-
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
…
Last packet sent to the server was 0 ms ago.
…
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
…
———-
Changing localhost to 127.0.0.1 did NOT work.
Although having different MySQL server versions on dev pc and deployment host, the jdbc version was not the problem
// jdbc (packaged in .war):
// mysql-connector-java-5.1.6-bin.jar
// dev mysql: 5.1.37, for debian-linux-gnu
// depl mysql: 5.0.51a-3ubuntu5.4-log (Ubuntu)
// container: tomcat 6.0
Changing the IP ADDRESS to that of the deployment host WORKED (same host as db?!); But gave another error about the user not having permissions on that host – even though i did previously as mysql root
> grant all privileges on mydb.* to ‘myuser’@’%’ identified by ‘####’;
(connecting from both localhost & other hosts using mysql cmd-line client did indeed work, but the web-app on tomcat runing on the same host is denied..?)
This fixed the permission problem (specify IP):
> grant all privileges on mydb.* to ‘myuser’@” identified by ‘####’;
– péter
it seems not to work for my problem
if i set
url=”jdbc:mysql://127.0.0.1:3306/MyNewDatabase?autoReconnect=true”
error
DataSource invalid: “org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)
if i set url=”jdbc:mysql://127.0.0.1:3306/MyNewDatabase
error
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure
Last packet sent to the server was 0 ms ago
Your MySql port may be 80 first check it.After that check if you have given any space in your url.Delete each & every space contained in url.Check whether have given colon(:)correctly or not.Try this it will surely work.
@admin:
For communication link error , enter remote host ip solve the problem.. thanks for the post.
I have Installed NetBeans 6.8 in Mandriva 2008.
I have Java Program as following with JAR file attached in it.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DataBaseConnectionTest {
public static void main(String[] args) {
try{
Class.forName(“com.mysql.jdbc.Driver”);
Connection conn = DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/crush_metal_web”, “root”, “gpstracker”);
}catch(ClassNotFoundException e){
System.out.println(e);
}catch(SQLException e){
System.out.println(e);
}
}
}
When I run the program I get the following the error as.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
MySQL server is running complete. I came to know this when I enter the following command , It will give me MySQL prompt.
mysql -u root -prooot
where user name is root and password is root.
please help me for this.
My mail address is – a_avtar@yahoo.co.in
hm. hope to see same more info
Если кому интересна информация по лучевой терапии, обращайтесь.
so why do you think so?
Hey,
Thank you so much..
The problem for me was i didn’t pass the username and password in the url..
Your code helped me out..
I solved this problem by adding following line to $TOMCAT-CONFIG/policy.d/04webapps.policy
permission java.net.SocketPermission “127.0.0.1:3306”, “connect”;
I have above same error when i want inserting data from one machine to another machine in linux. so plz give me solution.
I am developing a web-based app using jsp and Mysql. I am not receiving any error msgs. the html content of the page is displayed but not the database contents. plz help me.
the code is given below.
SELECT id, title, price FROM book
A First JSP Database
idtitleprice
thanks
/*
SELECT id, title, price FROM book
A First JSP Database
idtitleprice
*/
%@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core_rt”%
%@ taglib prefix=”sql” uri=”http://java.sun.com/jstl/sql_rt”%
sql:setDataSource var=”datasource”driver=”com.mysql.jdbc.Driver”url=”jdbc:mysql://127.0.0.1:3309/publish”user=”root”password=”bkr”/
sql:query var=”books” dataSource=”${datasource}”
SELECT id, title, price FROM book
/sql:query
html
head
titleA First JSP Database/title
/head
body
table border=”1″
tr
td id /td td title /td td price /td
/tr
c:forEach items=”${book.rows}” var=”row”
tr
td c:out value=”${row.id}”/ /td
td c:out value=”${row.title}”/ /td
td c:out value=”${row.price}”/ /td
/tr
/c:forEach
/table
/body
html
I figured it out myself. it was a silly mistake in using the DataSource variable name.
Thanks for the other helps.
Solved:
in my.cnf
comment the line
skip-networking
to
#skip-networking
That’s all
import java.io.*;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.sql.Driver;
class dbtest{
public static void main(String args[]) throws IOException,ClassNotFoundException,SQLException {
String DB_DRIVER = “com.mysql.jdbc.Driver”;
//String host = “localhost”;
// String port = “3306”;
String DB_USER = “root”;
String DB_PASSWORD = “”;
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
try
{
String DB_URL = “jdbc:mysql://localhost:3306/usemaster”;
//DB_URL=DB_URL +””+host+”:”+port;
//DB_URL=DB_URL +”/usemaster”;
Class.forName(DB_DRIVER).newInstance();
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
System.out.println(conn);
}
catch(Exception ex0)
{
System.out.println(ex0);
}
/*stmt=conn.createStatement();
if(conn)
{
System.out.println(“connected”);
}
else
{
System.out.println(“not connected”);
}*/
}
}
i changed localhost to 127.0.0.1 and worked for me.
Check to see your /etc/my.cnf and see what the bind address is. If it says bind-address=192.168.1.7 then connect to 192.168.1.7 or let dns figure out the address and just give the host name.
OMG. I tried everything and this FINALLY solves my problem. THANKS!
Great helpful for me! Thank you!
I solved this issue by increasing the wait_timeout property in /etc/my.cnf file.
Your comment didn’t work in my case:
——————Program——————————————————-
try {
Class.forName(“com.mysql.jdbc.Driver”);
String connectionUrl = “jdbc:mysql://127.0.0.1:3306/arbeitwagecalc?”
+ “user=root&password=noto”;
con = DriverManager.getConnection(connectionUrl);
} catch (SQLException e) {
System.out.println(“SQL Exception: ” + e.toString());
} catch (ClassNotFoundException cE) {
System.out.println(“Class Not Found Exception: ” + cE.toString());
}
—————————————————————————-
——————-Result———————————————————
run:
Exception in thread “main” java.lang.NullPointerException
SQL Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
at arbeitwagecalculationsystem.Main.main(Main.java:56)
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Java Result: 1
構築成功 (合計時間: 1 秒)
——————————————————————————————-
My Platform:
NetBeans 6.8 on Windows 7, MySQL Server 5.1 with use of ‘mysql-connector-java-5.1.13-bin.jar’.
Best regards,
12/9/2011
NOTO
download and install mysql-5.5.23.exe file… u didnt get error… i solved my error..
hi i am develope a programe in which i manage database name through session becaause i am maintain multiple data base in one war file there is problem of
No operations allowed after connection closed.
becasue i am open the conection on start of the page and close connection after end of the page
and data is save in another database some time time but mostly run proper
any one is help me to rectify error and data switching
code of connection
dbname=centre;
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
String database = “jdbc:mysql://localhost:3306/”+getDbname();
db_connection = DriverManager.getConnection (database, username, password);
here centre is the variable for database name
thanks
my mail id is
sandeep050575@gmail.com
After changing the IP Address ,one new problem occured.
error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Accès refusé pour l’utilisateur: ”@’@localhost’. Base ‘siliconsoft’
tried everything bt still it didnt work for me 😦