title: How to establish database connection between spring MVC + PostgreSQL?
body: <p>Currently am working on Spring MVC and I have included dependency for PostgreSQL but whenever i tried to start connection it fails.
  Here is my pom.xml codes </p>
  
  <pre><code>  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
       http://maven.apache.org/maven-v4_0_0.xsd"&gt;
        &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
        &lt;groupId&gt;sms&lt;/groupId&gt;
        &lt;artifactId&gt;system&lt;/artifactId&gt;
        &lt;packaging&gt;war&lt;/packaging&gt;
        &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
        &lt;name&gt;system Maven Webapp&lt;/name&gt;
        &lt;url&gt;http://maven.apache.org&lt;/url&gt;
        &lt;dependencies&gt;
  
          &lt;dependency&gt;
            &lt;groupId&gt;junit&lt;/groupId&gt;
            &lt;artifactId&gt;junit&lt;/artifactId&gt;
            &lt;version&gt;3.8.1&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
        &lt;dependency&gt;
          &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
          &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
          &lt;version&gt;42.2.5&lt;/version&gt;
      &lt;/dependency&gt;
  
          &lt;dependency&gt;  
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;  
          &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;  
          &lt;version&gt;5.1.1.RELEASE&lt;/version&gt;  
         &lt;/dependency&gt;  
  
         &lt;dependency&gt;    
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;    
          &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;    
          &lt;version&gt;3.0-alpha-1&lt;/version&gt;    
      &lt;/dependency&gt;  
        &lt;/dependencies&gt;
        &lt;build&gt;
          &lt;finalName&gt;system&lt;/finalName&gt;
        &lt;/build&gt;
      &lt;/project&gt;
  </code></pre>
  
  <p>This is the codes from -servlet file</p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      xmlns:context="http://www.springframework.org/schema/context"  
      xmlns:mvc="http://www.springframework.org/schema/mvc"  
      xsi:schemaLocation=" 
          http://www.springframework.org/schema/beans  
          http://www.springframework.org/schema/beans/spring-beans.xsd  
          http://www.springframework.org/schema/context  
          http://www.springframework.org/schema/context/spring-context.xsd  
          http://www.springframework.org/schema/mvc  
          http://www.springframework.org/schema/mvc/spring-mvc.xsd"&gt;  
  
      &lt;!-- Provide support for component scanning --&gt;  
      &lt;context:component-scan base-package="com.javatpoint" /&gt;  
    &lt;mvc:resources mapping="/resources/**" location="/resources/"/&gt;
      &lt;!--Provide support for conversion, formatting and validation --&gt;  
      &lt;mvc:annotation-driven/&gt;  
  &lt;/beans&gt; 
  </code></pre>
  
  <p>This is the codes from web.xml file </p>
  
  <pre><code> &lt;!DOCTYPE web-app PUBLIC
       "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
       "http://java.sun.com/dtd/web-app_2_3.dtd" &gt;
      &lt;web-app&gt;
        &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
        &lt;servlet&gt;    
          &lt;servlet-name&gt;sms&lt;/servlet-name&gt;    
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;    
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;      
      &lt;/servlet&gt;    
      &lt;servlet-mapping&gt;    
          &lt;servlet-name&gt;sms&lt;/servlet-name&gt;    
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;    
      &lt;/servlet-mapping&gt;    
      &lt;/web-app&gt;
  
  
  
  The configuration of jdbc driver 
  [jdbc driver][2]
  
  In the end i get this error
  [error][3]
  </code></pre>
  
  <p>i tried to  download the jdbc driver version that matches with my jdk but still i get the same error..
  is there something wrong with my approach? How can i solve this?    Any help from you guys will be much appreciated</p>
  
  <pre><code>  [1]: https://i.stack.imgur.com/vEePs.png
    [2]: https://i.stack.imgur.com/ep3lz.png
    [3]: https://i.stack.imgur.com/0j8v0.png
  </code></pre>
  
  <p>T<strong>HE ERROR MESSAGE FROM "DETAILS" BUTTON</strong> </p>
  
  <blockquote>
  <pre><code>Could not connect to smsDatabase.
  Error creating SQL Model Connection connection to smsDatabase. (Error: Connection refused. Check that the hostname and port are
  </code></pre>
    
    <p>correct and that the postmaster is accepting TCP/IP connections.
        Exception: java.net.ConnectException: Connection refused: connect
        Stack Trace:
        java.net.ConnectException: Connection refused: connect
          at java.net.DualStackPlainSocketImpl.connect0(Native Method)
          at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
          at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
          at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
          at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
          at java.net.PlainSocketImpl.connect(Unknown Source)
          at java.net.SocksSocketImpl.connect(Unknown Source)
          at java.net.Socket.connect(Unknown Source)
          at java.net.Socket.connect(Unknown Source)
          at java.net.Socket.(Unknown Source)
          at java.net.Socket.(Unknown Source)
          at org.postgresql.core.PGStream.(PGStream.java:60)
          at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:77)
          at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
          at org.postgresql.jdbc2.AbstractJdbc2Connection.(AbstractJdbc2Connection.java:116)
          at org.postgresql.jdbc2.Jdbc2Connection.(Jdbc2Connection.java:24)
          at org.postgresql.Driver.makeConnection(Driver.java:369)
          at org.postgresql.Driver.connect(Driver.java:245)
          at org.eclipse.datatools.enablement.internal.postgresql.PostgreSQLJDBCConnection.createConnection(PostgreSQLJDBCConnection.java:87)
          at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
          at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
          at org.eclipse.datatools.enablement.internal.postgresql.PostgreSQLJDBCConnection.(PostgreSQLJDBCConnection.java:47)
          at org.eclipse.datatools.enablement.internal.postgresql.PostgreSQLConnectionFactory.createConnection(PostgreSQLConnectionFactory.java:51)
          at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
          at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
          at org.eclipse.datatools.connectivity.sqm.internal.core.connection.ConnectionInfoImpl.initializeJDBCConnection(ConnectionInfoImpl.java:705)
          at org.eclipse.datatools.connectivity.sqm.internal.core.connection.ConnectionInfoImpl.(ConnectionInfoImpl.java:690)
          at org.eclipse.datatools.connectivity.sqm.internal.core.connection.ConnectionFactory.createConnection(ConnectionFactory.java:38)
          at org.eclipse.datatools.connectivity.sqm.core.SQMConnectionFactory.createConnection(SQMConnectionFactory.java:45)
          at org.eclipse.datatools.connectivity.sqm.internal.core.connection.ConnectionFactory.createConnection(ConnectionFactory.java:59)
          at org.eclipse.datatools.connectivity.internal.ConnectionFactoryAdapterProvider.createConnection(ConnectionFactoryAdapterProvider.java:114)
          at org.eclipse.datatools.connectivity.internal.ManagedConnection.createConnection(ManagedConnection.java:163)
          at org.eclipse.datatools.connectivity.internal.CreateConnectionJob.run(CreateConnectionJob.java:56)
          at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
        End of Stack Trace
        )
        Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
        Exception: java.net.ConnectException: Connection refused: connect
        Stack Trace:
        java.net.ConnectException: Connection refused: connect
          at java.net.DualStackPlainSocketImpl.connect0(Native Method)
          at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
          at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
          at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
          at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
          at java.net.PlainSocketImpl.connect(Unknown Source)
          at java.net.SocksSocketImpl.connect(Unknown Source)
          at java.net.Socket.connect(Unknown Source)
          at java.net.Socket.connect(Unknown Source)
          at java.net.Socket.(Unknown Source)
          at java.net.Socket.(Unknown Source)
          at org.postgresql.core.PGStream.(PGStream.java:60)
          at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:77)
          at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
          at org.postgresql.jdbc2.AbstractJdbc2Connection.(AbstractJdbc2Connection.java:116)
          at org.postgresql.jdbc2.Jdbc2Connection.(Jdbc2Connection.java:24)
          at org.postgresql.Driver.makeConnection(Driver.java:369)
          at org.postgresql.Driver.connect(Driver.java:245)
          at org.eclipse.datatools.enablement.internal.postgresql.PostgreSQLJDBCConnection.createConnection(PostgreSQLJDBCConnection.java:87)
          at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
          at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
          at org.eclipse.datatools.enablement.internal.postgresql.PostgreSQLJDBCConnection.(PostgreSQLJDBCConnection.java:47)
          at org.eclipse.datatools.enablement.internal.postgresql.PostgreSQLConnectionFactory.createConnection(PostgreSQLConnectionFactory.java:51)
          at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
          at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
          at org.eclipse.datatools.connectivity.sqm.internal.core.connection.ConnectionInfoImpl.initializeJDBCConnection(ConnectionInfoImpl.java:705)
          at org.eclipse.datatools.connectivity.sqm.internal.core.connection.ConnectionInfoImpl.(ConnectionInfoImpl.java:690)
          at org.eclipse.datatools.connectivity.sqm.internal.core.connection.ConnectionFactory.createConnection(ConnectionFactory.java:38)
          at org.eclipse.datatools.connectivity.sqm.core.SQMConnectionFactory.createConnection(SQMConnectionFactory.java:45)
          at org.eclipse.datatools.connectivity.sqm.internal.core.connection.ConnectionFactory.createConnection(ConnectionFactory.java:59)
          at org.eclipse.datatools.connectivity.internal.ConnectionFactoryAdapterProvider.createConnection(ConnectionFactoryAdapterProvider.java:114)
          at org.eclipse.datatools.connectivity.internal.ManagedConnection.createConnection(ManagedConnection.java:163)
          at org.eclipse.datatools.connectivity.internal.CreateConnectionJob.run(CreateConnectionJob.java:56)
          at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
        End of Stack Trace</p>
  
  <pre><code>Error creating PostgreSQL JDBC Connection Factory connection to smsDatabase. (Error: Connection refused. Check that the hostname and
  </code></pre>
    
    <p>port are correct and that the postmaster is accepting TCP/IP
    connections.
        Exception: java.net.ConnectException: Connection refused: connect
        Stack Trace:
        java.net.ConnectException: Connection refused: connect
          at java.net.DualStackPlainSocketImpl.connect0(Native Method)
          at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
          at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
          at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
          at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
          at java.net.PlainSocketImpl.connect(Unknown Source)
          at java.net.SocksSocketImpl.connect(Unknown Source)
          at java.net.Socket.connect(Unknown Source)
          at java.net.Socket.connect(Unknown Source)
          at java.net.Socket.(Unknown Source)
          at java.net.Socket.(Unknown Source)
          at org.postgresql.core.PGStream.(PGStream.java:60)
          at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:77)
          at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
          at org.postgresql.jdbc2.AbstractJdbc2Connection.(AbstractJdbc2Connection.java:116)
          at org.postgresql.jdbc2.Jdbc2Connection.(Jdbc2Connection.java:24)
          at org.postgresql.Driver.makeConnection(Driver.java:369)
          at org.postgresql.Driver.connect(Driver.java:245)
          at org.eclipse.datatools.enablement.internal.postgresql.PostgreSQLJDBCConnection.createConnection(PostgreSQLJDBCConnection.java:87)
          at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
          at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
          at org.eclipse.datatools.enablement.internal.postgresql.PostgreSQLJDBCConnection.(PostgreSQLJDBCConnection.java:47)
          at org.eclipse.datatools.enablement.internal.postgresql.PostgreSQLConnectionFactory.createConnection(PostgreSQLConnectionFactory.java:51)
          at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
          at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
          at org.eclipse.datatools.connectivity.internal.ManagedConnection.createConnection(ManagedConnection.java:166)
          at org.eclipse.datatools.connectivity.internal.CreateConnectionJob.run(CreateConnectionJob.java:56)
          at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
        End of Stack Trace
        )
        Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
        Exception: java.net.ConnectException: Connection refused: connect
        Stack Trace:
        java.net.ConnectException: Connection refused: connect
          at java.net.DualStackPlainSocketImpl.connect0(Native Method)
          at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
          at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
          at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
          at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
          at java.net.PlainSocketImpl.connect(Unknown Source)
          at java.net.SocksSocketImpl.connect(Unknown Source)
          at java.net.Socket.connect(Unknown Source)
          at java.net.Socket.connect(Unknown Source)
          at java.net.Socket.(Unknown Source)
          at java.net.Socket.(Unknown Source)
          at org.postgresql.core.PGStream.(PGStream.java:60)
          at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:77)
          at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
          at org.postgresql.jdbc2.AbstractJdbc2Connection.(AbstractJdbc2Connection.java:116)
          at org.postgresql.jdbc2.Jdbc2Connection.(Jdbc2Connection.java:24)
          at org.postgresql.Driver.makeConnection(Driver.java:369)
          at org.postgresql.Driver.connect(Driver.java:245)
          at org.eclipse.datatools.enablement.internal.postgresql.PostgreSQLJDBCConnection.createConnection(PostgreSQLJDBCConnection.java:87)
          at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
          at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
          at org.eclipse.datatools.enablement.internal.postgresql.PostgreSQLJDBCConnection.(PostgreSQLJDBCConnection.java:47)
          at org.eclipse.datatools.enablement.internal.postgresql.PostgreSQLConnectionFactory.createConnection(PostgreSQLConnectionFactory.java:51)
          at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
          at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
          at org.eclipse.datatools.connectivity.internal.ManagedConnection.createConnection(ManagedConnection.java:166)
          at org.eclipse.datatools.connectivity.internal.CreateConnectionJob.run(CreateConnectionJob.java:56)
          at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55) End of Stack Trace</p>
  </blockquote>