title: How to connect to MySQL database using jdbc and tomcat
body: <p>I am working on a dynamic web project for class. I am trying to pull data from a MYSQL database so that I can read it and use it on my web application. When I try and connect, I get this error: </p>
  
  <pre><code>        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)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
      at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1127)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:356)
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2502)
      at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:832)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:46)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
      at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
      at java.sql.DriverManager.getConnection(DriverManager.java:571)
      at java.sql.DriverManager.getConnection(DriverManager.java:215)
      at edu.neumont.csc280.controller.DBConnection.openConnection(DBConnection.java:31)
      at edu.neumont.csc280.listeners.ServiceLoader.contextInitialized(ServiceLoader.java:31)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
      at java.util.concurrent.FutureTask.run(FutureTask.java:262)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:744)
  Caused by: java.net.ConnectException: Connection timed out: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:579)
      at java.net.Socket.connect(Socket.java:528)
      at java.net.Socket.&lt;init&gt;(Socket.java:425)
      at java.net.Socket.&lt;init&gt;(Socket.java:241)
      at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:258)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:306)
      ... 25 more
  May 22, 2014 3:47:10 AM org.apache.myfaces.config.DefaultFacesConfigurationProvider getStandardFacesConfig
  INFO: Reading standard config META-INF/standard-faces-config.xml
  May 22, 2014 3:47:10 AM org.apache.myfaces.config.DefaultFacesConfigurationProvider getClassloaderFacesConfig
  INFO: Reading config : jar:file:/C:/xampp/tomcat/lib/openwebbeans-jsf-1.2.1.jar!/META-INF/faces-config.xml
  May 22, 2014 3:47:10 AM org.apache.myfaces.config.DefaultFacesConfigurationProvider getClassloaderFacesConfig
  INFO: Reading config : jar:file:/C:/xampp/tomcat/lib/openwebbeans-el22-1.2.1.jar!/META-INF/faces-config.xml
  May 22, 2014 3:47:10 AM org.apache.myfaces.config.LogMetaInfUtils logArtifact
  INFO: Artifact 'myfaces-api' was found in version '2.1.13' from path 'file:/C:/xampp/tomcat/lib/myfaces-api-2.1.13.jar'
  May 22, 2014 3:47:10 AM org.apache.myfaces.config.LogMetaInfUtils logArtifact
  INFO: Artifact 'myfaces-impl' was found in version '2.1.13' from path 'file:/C:/xampp/tomcat/lib/myfaces-impl-2.1.13.jar'
  May 22, 2014 3:47:10 AM org.apache.myfaces.application.ApplicationImpl getProjectStage
  INFO: Couldn't discover the current project stage, using Production
  May 22, 2014 3:47:10 AM org.apache.myfaces.config.FacesConfigurator handleSerialFactory
  INFO: Serialization provider : class org.apache.myfaces.shared_impl.util.serial.DefaultSerialFactory
  May 22, 2014 3:47:10 AM org.apache.myfaces.config.annotation.DefaultLifecycleProviderFactory getLifecycleProvider
  INFO: Using LifecycleProvider org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider
  May 22, 2014 3:47:10 AM org.apache.myfaces.webapp.AbstractFacesInitializer initFaces
  INFO: ServletContext initialized.
  May 22, 2014 3:47:10 AM org.apache.coyote.AbstractProtocol start
  INFO: Starting ProtocolHandler ["http-bio-8080"]
  May 22, 2014 3:47:10 AM org.apache.coyote.AbstractProtocol start
  INFO: Starting ProtocolHandler ["ajp-bio-8009"]
  May 22, 2014 3:47:10 AM org.apache.catalina.startup.Catalina start
  INFO: Server startup in 22686 ms
  </code></pre>
  
  <p>I would like to know what possible reasons are there for this error? I know my server is indeed listening on port 3066 and I have the credentials necessary to connect. I do not understand the way tomcat7.0 is trying to communicate from the application to the MYSQL database. I have the latest MYSQL jdbc driver installed, but when I try and DriverManager.getConnection(), it times out. So I think it has something to do with the configuration. This is my config:</p>
  
  <p><code>String url = "jdbc:mysql://myserver.com:3306/trivitup";
          String user = user";
          String pw = "pass";</code></p>
  
  <p>I typed in netstat -a on my server to see what ports it was listening to</p>
  
  <pre><code>I did netstat -a and this is what I got:
  
  Active Internet connections (servers and established)
  Proto Recv-Q Send-Q Local Address               Foreign Address             State
  tcp        0      0 *:mysql                     *:*                         LISTEN
  tcp        0      0 *:ssh                       *:*                         LISTEN
  tcp        0      0 localhost:smtp              *:*                         LISTEN
  tcp        0    320 ip-172-31-2-171.us-west:ssh c-76-27-90-190.hsd1.u:31050 ESTABLISHED
  tcp        0      0 *:http                      *:*                         LISTEN
  tcp        0      0 *:ssh                       *:*                         LISTEN
  tcp        0      0 *:https                     *:*                         LISTEN
  udp        0      0 *:bootpc                    *:*
  udp        0      0 ip-172-31-2-171.us-west-:ntp *:*
  udp        0      0 localhost:ntp               *:*
  udp        0      0 *:ntp                       *:*
  Active UNIX domain sockets (servers and established)
  Proto RefCnt Flags       Type       State         I-Node Path
  unix  2      [ ACC ]     STREAM     LISTENING     8268   /var/lib/mysql/mysql.sock
  unix  7      [ ]         DGRAM                    6704   /dev/log
  unix  2      [ ACC ]     STREAM     LISTENING     5745   @/com/ubuntu/upstart
  unix  2      [ ACC ]     STREAM     LISTENING     6761   /var/run/dbus/system_bus_socket
  unix  2      [ ACC ]     SEQPACKET  LISTENING     5947   @/org/kernel/udev/udevd
  unix  3      [ ]         STREAM     CONNECTED     6770   /var/run/dbus/system_bus_socket
  unix  3      [ ]         STREAM     CONNECTED     6765
  unix  3      [ ]         DGRAM                    5955
  unix  2      [ ]         DGRAM                    7262
  unix  2      [ ]         DGRAM                    7166
  unix  3      [ ]         STREAM     CONNECTED     8455
  unix  2      [ ]         DGRAM                    7325
  unix  2      [ ]         DGRAM                    7243
  unix  3      [ ]         DGRAM                    5954
  unix  2      [ ]         DGRAM                    8451
  unix  3      [ ]         STREAM     CONNECTED     8456
  unix  3      [ ]         STREAM     CONNECTED     6769
  unix  3      [ ]         STREAM     CONNECTED     6764
  </code></pre>
  
  <p>Thanks for reading</p>