• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

JDBC Connection problem

Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am having problem in getConnection(), i written DatabaseConnection class.
but it's compiling fine but while runing it's showing Exceptions.
I am using NetBeans 6 and Server tomcat 6.0.16. please help to solve this problem. I added jar file that is "mysql-connector-java-5.1.6-bin.jar" i added jar file in the folder C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.16\lib

I am new to coding please help me or give sugestions.............

import java.sql.*;
//import java.util.*;

* @author venu
public class DatabaseConnection {
public static Connection getConnection() {
Connection connection=null;
String driver= "com.mysql.jdbc.Driver";
String URL = "jdbc:mysql://mysql.local.com:3306/venu";
String username = "root";
String password = "root";
try {
if(driver == null || URL == null) {
throw new Exception("Driver class or Driver URL should not be null");
catch(Exception e) {
return connection;
public static void main(String args[])

Connection con = getConnection();
System.out.println("got connection");
System.out.println("got connection");
catch(Exception e)

/* Exceptions */

com.mysql.jdbc.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1070)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at com.venu.login.common.DatabaseConnection.getConnection(DatabaseConnection.java:27)
at com.venu.login.common.DatabaseConnection.main(DatabaseConnection.java:40)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
got connection
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:507)
at java.net.Socket.connect(Socket.java:457)
at java.net.Socket.<init>(Socket.java:365)
at java.net.Socket.<init>(Socket.java:207)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:280)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2026)
... 7 more
at com.venu.login.common.DatabaseConnection.main(DatabaseConnection.java:42)
BUILD SUCCESSFUL (total time: 22 seconds)

Venu J
Posts: 10445
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Caused by: java.net.ConnectException: Connection timed out: connect

Are you sure that the connection URL that you are using is correct? Is the DB server listening on that address and port?
venu jayaram
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya i am using correct URL and port number.

In Netbeans 6 in service tab database it's showing the tables name using same URL and Port number with database Name


i don't no what's the problem when i click on errros that will call the
database API it's showing error on getConnection method

Please tell how to solove this problem.
    Bookmark Topic Watch Topic
  • New Topic