wood burning stoves*
The moose likes JDBC and the fly likes Connecting PostgreSQL through JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connecting PostgreSQL through JDBC" Watch "Connecting PostgreSQL through JDBC" New topic
Author

Connecting PostgreSQL through JDBC

Shyam Kasthala
Ranch Hand

Joined: Mar 30, 2006
Posts: 48
Hi,
I have written a JSP page and using PostgreSQL database.Now i want to connect to PostgreSQL using JDBC.I have used the driver : org.postgresql.Driver as
Class.forName("org.postgresql.Driver");
con=DriverManager.getConnection("jdbc:postgresql://192.168.15.139:8080/MGS","username", "password");
I am not able to display the JSP page.I am getting the error as :
java.lang.ClassNotFoundException: org.postgresql.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1332)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1181)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at project.mgs.MyDriver.getConnection(MyDriver.java:11)
at org.apache.jsp.Project_jsp._jspService(org.apache.jsp.Project_jsp:47)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)


Please can any one tell me what's going wrong.What .jar files should i include in the lib folder of my application.Please tell me the names of the .jar files.Thanks in advance.

Thanks & Regards
Shyam kasthala


Thanks & Regards<br /> Shyam Kasthala<br />ILFS Infotech Limited
Masoud Kalali
Author
Ranch Hand

Joined: Jul 08, 2004
Posts: 531

You need to include postgresql-x.jar in your classpath , do you add it ?
you can download the driver from here :

http://jdbc.postgresql.org/download/postgresql-8.1-407.jdbc3.jar
http://jdbc.postgresql.org/download.html


hth


Masoud Kalali
Software Engineer - My Weblog - GlassFish Security
Shyam Kasthala
Ranch Hand

Joined: Mar 30, 2006
Posts: 48
Hi,
Sorry for delay.I have included the postgresql-8.1-407.jdbc3.jar file in my application's lib folder along with servlet-api.jar,jsp-api.jar,jdbc.jar . Just look at my program to connect to postgresql.

package project.mgs;
import java.sql.*;

public class MyDriver
{
public static Connection getConnection() throws Exception
{
Connection con=null;
try
{
Class.forName("org.postgresql.Driver");
con=DriverManager.getConnection("jdbc:postgresql://192.168.15.139:8080/MGS","username", "password");
return con;
}//try
catch (Exception e)
{
e.printStackTrace();
}//catch
return null;
}//getConnection
}//class


I am getting the error as follows : -

org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:136)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:117)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
at org.postgresql.jdbc3.Jdbc3Connection.<init>(Jdbc3Connection.java:24)
at org.postgresql.Driver.connect(Driver.java:235)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at project.mgs.MyDriver.getConnection(MyDriver.java:12)
at org.apache.jsp.Project_jsp._jspService(org.apache.jsp.Project_jsp:47)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.EOFException
at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:243)
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:253)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:94)
... 29 more

Please tell me how to connect now. Thanks in advance.
Masoud Kalali
Author
Ranch Hand

Joined: Jul 08, 2004
Posts: 531

Are you sure that your database is running on port 8080 ?
I think you have used this port by mistake , you may take a look at :
http://jdbc.postgresql.org/documentation/80/connect.html
which is postgresql jdbc connection manual.
i suggest you change you connection manager line to :


and tell us if you faced any problem.
i change the connection string to connect to postgreSQL on its default post , I assumed that you did not change the postgre sql server listening port.
kiran juthi
Greenhorn

Joined: Mar 17, 2009
Posts: 1
Check the port, postgres by default starts at 5432
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
And welcome to JavaRanch, Kiran Juthi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connecting PostgreSQL through JDBC
 
Similar Threads
Exception while including taglibs(apache.org.xtags) in the JSP?
Error while jsp connecting to database
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
problem with tomcat when i am connection with db2 database
Error while accessing hibernate session through JSF