androidGenX
androidGenX

Reputation: 1148

java.lang.ClassNotFoundException: org.postgresql.Driver postgresql ubuntu

I have configured apache tomcat in ubuntu. I am using postgresql as database. I have downloaded postgresql-9.3-1101.jdbc3.jar connector and set CLASSPATH as follows:

export CLASSPATH=/var/lib/tomcat7/webapps/CMAS/WEB_INF/lib/postgresql-9.3-1101.jdbc3.jar

I am using a JSP program to access Database values, code as follows:

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.* " %>
<%@ page import="java.io.*" %>
<%
try {
String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://localhost:5432/CMAS";
String username = "postgres";
String password = "postgres";

String myDataField = null;
String myQuery = "SELECT * FROM survey_details";
Connection myConnection = null;
PreparedStatement myPreparedStatement = null;
ResultSet myResultSet = null;
Class.forName(driver).newInstance();
myConnection = DriverManager.getConnection(url,username,password);
myPreparedStatement = myConnection.prepareStatement(myQuery);
myResultSet = myPreparedStatement.executeQuery();
if(myResultSet.next()){
out.print("herezz");
myDataField = myResultSet.getString("imei");
out.print(myDataField);
}
}
catch(Exception e){
out.print(e);
}

%>

I am getting an exception "java.lang.ClassNotFoundException: org.postgresql.Driver". I understand its because of classpath. But how can I fix this?

Upvotes: 0

Views: 7822

Answers (1)

hadaytullah
hadaytullah

Reputation: 1184

The tomcat server does not have the postgresql JDBC driver in its resources.

Copy the postgresql-xxxx.Jar file to "apache-tomcat-xxxx\lib" folder.

(xxxx = whatever version you are using)

Upvotes: 1

Related Questions