0

coming from a php background i'm completely lost in the JAVA architecture. this is my first desktop app in over 10 yrs, and my first Java app. So pardon the noob questions.

I use netbeans 7.0.1 + JDK 1.7.0. Create new desktop appliction, with just a single button. do the "import java.sql.*;" at the top of the "DesktopAppView.java" page. I went to library then added the MYSQL JDBC driver. Even downloaded and added the latest JDBC connection from MYSQL site.

went to the services panel, right click on Drivers, MySQL (Connector/J driver) and added new connection and was able to connect to the DB.

then in the button click function in "DesktopAppView.java" page, i added:

//String driver =  "com.mysql.jdbc.Driver";
String driver =  "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/test";
String username = "root";
String password = "";

Class.forName(driver); // load MySQL driver
//Class.forName(driver).newInstance(); // load MySQL driver
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("done"); 

When it comes to the "getConnection" part, application just hangs. When i added a "?connectTimeout=3000" to the url string, it timedout the app raising exception :

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure...
......
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.net.SocketTimeoutException: Connect timed out
  at java.net.SocksSocketImpl.readSocksReply(SocksSocketImpl.java:125)

however, when creating a java application in netbeans (not desktop app).. the same connection code works fine???

I'm pulling my hair out over the last two days. I'm not sure what stuff is added to teh desktop app that maybe conflicting with the DB connection.

I have already tried all posted here by SOheil... Solving a "communications link failure" with JDBC and MySQL Nothing works

PLs HELP !!!

Community
  • 1
  • 1
Shaakir
  • 464
  • 5
  • 13
  • Have you specified MySQL port in the connection URL? – Sridhar Sep 18 '14 at 08:45
  • @Sridhar As long as MySQL is running on the default port, there is no need to specify the port in the url. – Mark Rotteveel Sep 18 '14 at 08:48
  • Start NetBeans again, and don't go into Services. Unlikely, but maybe the two accesses (of Services and your application) clash. – Joop Eggen Sep 18 '14 at 09:16
  • Try specifying a password for the database and use com.mysql.jdbc driver – SSC Sep 18 '14 at 09:21
  • @Sridhar... mysql is running default 3306 port, i have tried to add that to the url string (localhost:3306 or 127.0.0.1:3306 or *my-ip*:3306) but no success – Shaakir Sep 18 '14 at 09:49

2 Answers2

0

What i ended up doing is creating a java application in netbeans (not desktop app) and did all the frame / panel / button code myself.

The JDBC code above works perfectly. Still cannot figure out why creating a desktop app just does not want to connect to DB ???

Shaakir
  • 464
  • 5
  • 13
0

Your problem is one of the following:

  1. Your my-sql server is not listening on the 3306 port, or maybe the service is not started correctly.

You can find out that using the mysql administrator GUI. If you can connect to your db, the service started in the right way.

  1. You have an antivirus or a firewall which blocks the communication requests to the 3306 port of your system, because it detects them as attacks.

Good Luck.

STaefi
  • 4,297
  • 1
  • 25
  • 43