preoblem in jdbc postgresql and tomcat

Started by java unixabout 21 years ago3 messagesgeneral
Jump to latest
#1java unix
javaunix123@yahoo.co.in

hi,
i have installed postgresql-7.4.1,and tomcat4.1.31and
j2sdk1.4.2_07
for tomcat i have included path
/usr/local/jakarta-tomcat-4.1.31
for j2sdk path set is /usr/local/j2sdk1.4.2_07
and CLASSPATH SET IS:
/usr/local/pgsql/pg73jdbc3.jar:/usr/local/pgsql/pg74.215.jdbc3.jar:/usr/local/pg
sql/postgresql-8.0-310.jdbc3.jar:/usr/local/j2sdk1.4.2_07/lib/tools.jar:/usr/loc
al/j2sdk1.4.2_07/jre/lib/rt.jar:/usr/local/jakarta-tomcat-4.1.31/common/lib/serv
let.jar:/usr/local/jakarta-tomcat-4.1.31/common/lib:/usr/local/pgsql:/usr/local/
j2sdk1.4.2_07/jre/lib:/usr/local/j2sdk1.4.2_07/lib
i have included all the paths

all the above path are set in /etc/profile/
and i set path for all three jar
files:/usr/local/pgsql/pg73jdbc3.jar:/usr/local/pgsql/pg74.215.jdbc3.jar:/usr/local/pg
sql/postgresql-8.0-310.jdbc3.jar

when i run this simple java program i get
error as follows:
[root@roopesh bin]# javac PostgreSQLTest.java//compile
PostgreSQLTest.java: In class `PostgreSQLTest':
PostgreSQLTest.java: In method
`PostgreSQLTest.main(java.lang.String[])':
PostgreSQLTest.java:7: Exception
`java.lang.ClassNotFoundException' must be caught, or
it must be declared in the `throws' clause of `main'.

Class.forName("postgresql.Driver").newInstance();
^
PostgreSQLTest.java:7: Exception
`java.lang.InstantiationException' must be caught, or
it must be declared in the `throws' clause of `main'.

Class.forName("postgresql.Driver").newInstance();
^
PostgreSQLTest.java:7: Exception
`java.lang.IllegalAccessException' must be caught, or
it must be declared in the `throws' clause of `main'.

Class.forName("postgresql.Driver").newInstance();

my PostgreSQLTest.java is as below
import java.sql.*;

class PostgreSQLTest {
public static void main (String[] args) {
try {
Driver driver = (Driver)

Class.forName("postgresql.Driver").newInstance();
DriverManager.registerDriver(driver);

String url = "jdbc:postgresql:javatest";
Connection con =
DriverManager.getConnection(url, "postgres", "");
Statement stm = con.createStatement();

stm.setQueryTimeout(10);
ResultSet rs = stm.executeQuery("select col1
from test");

rs.next();

System.out.println(rs.getString(1));

} catch (SQLException e) {

System.out.println("Exception!");
System.out.println(e.toString());
}
}
}
This is one simple example

Now when i tried to perform operations with tomcat
simple sevlet works but with database operations i get
the following errors
file is:ShowBedrock.java
import javax.servlet.*;
import javax.servlet.http.*;

public class ShowBedrock extends HttpServlet
{
public String getServletInfo()
{
return "Servlet connects to PostgreSQL database
and displays result of a SELECT";
}

private Connection dbcon; // Connection for scope
of ShowBedrock

// "init" sets up a database connection
public void init(ServletConfig config) throws
ServletException
{
String loginUser = "postgres";
String loginPasswd = "roopesh";
String loginUrl =
"jdbc:postgresql://localhost/bedrock";

// Load the PostgreSQL driver
try
{
Class.forName("org.postgresql.Driver");
dbcon =
DriverManager.getConnection(loginUrl, loginUser,
loginPasswd);
}
catch (ClassNotFoundException ex)
{

System.err.println("ClassNotFoundException: " +
ex.getMessage());
throw new ServletException("Class not
found Error");
}
catch (SQLException ex)
{
System.err.println("SQLException: " +
ex.getMessage());
}
}

//
Use http GET

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html"); //
Response mime type

// Output stream to STDOUT
PrintWriter out = response.getWriter();

out.println("<HTML><Head><Title>Bedrock</Title></Head>");
out.println("<Body><H1>Bedrock</H1>");

try
{
// Declare our statement
Statement statement =
dbcon.createStatement();

String query = "SELECT name, dept, ";
query += " jobtitle ";
query += "FROM employeee ";

// Perform the query
ResultSet rs =
statement.executeQuery(query);

out.println("<table border>");

// Iterate through each row of rs
while (rs.next())
{
String m_name =
rs.getString("name");
String m_dept =
rs.getString("dept");
String m_jobtitle =
rs.getString("jobtitle");
out.println("<tr>" +
"<td>" + m_name +
"</td>" +
"<td>" + m_dept +
"</td>" +
"<td>" + m_jobtitle +
"</td>" +
"</tr>");
}

out.println("</table></body></html>");
statement.close();
}
catch(Exception ex)
{
out.println("<HTML>" +
"<Head><Title>" +
"Bedrock: Error" +
"</Title></Head>\n<Body>"
+
"<P>SQL error in doGet: "
+
ex.getMessage() +
"</P></Body></HTML>");
return;
}
out.close();
}
}
when i compile i donot get any errors and class file
is created
when i display it in webbrowser as
http://localhost:8080/examples/servlet/ShowBedrock
the result is as follows

Bedrock
SQL error in doGet: null

and i am working in linux
please can any one help me with this problem i would
like to know the solution very soon.please i have to
do my project regarding this .i have not yet started
the coding of my project.i am stuck up with this
configuration.
if the above steps what i have done is wrong please
tell me step by step how i have to do
please reply soon

________________________________________________________________________
Yahoo! India Matrimony: Find your life partner online
Go to: http://yahoo.shaadi.com/india-matrimony

#2Simon Moses
ks_moses@yahoo.com
In reply to: java unix (#1)
Re: preoblem in jdbc postgresql and tomcat

hi,

some corrections.

CLASSPATH

my preference of jars in CLASSPATH is
tools.jar:pg74.215.jdbc3.jar:others:.

see the code change in Class.forName and url.

try{

Class.forName("org.postgresql.Driver");
Connection con =
DriverManager.getConnection("jdbc:postgresql://localhost:5432/dbname","postgres","postgres");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select col1 from
test");

while(rs.next()){
out.println(rs.getString(1)+"<br>");

}
}catch(Exception e){
out.println(e);

}

if the above code works then you try same in jsp.

--Simon Moses.
Bangalore,
India.

--- java unix <javaunix123@yahoo.co.in> wrote:

hi,
i have installed postgresql-7.4.1,and
tomcat4.1.31and
j2sdk1.4.2_07
for tomcat i have included path
/usr/local/jakarta-tomcat-4.1.31
for j2sdk path set is /usr/local/j2sdk1.4.2_07
and CLASSPATH SET IS:

/usr/local/pgsql/pg73jdbc3.jar:/usr/local/pgsql/pg74.215.jdbc3.jar:/usr/local/pg

sql/postgresql-8.0-310.jdbc3.jar:/usr/local/j2sdk1.4.2_07/lib/tools.jar:/usr/loc

al/j2sdk1.4.2_07/jre/lib/rt.jar:/usr/local/jakarta-tomcat-4.1.31/common/lib/serv

let.jar:/usr/local/jakarta-tomcat-4.1.31/common/lib:/usr/local/pgsql:/usr/local/

j2sdk1.4.2_07/jre/lib:/usr/local/j2sdk1.4.2_07/lib
i have included all the paths

all the above path are set in /etc/profile/
and i set path for all three jar

files:/usr/local/pgsql/pg73jdbc3.jar:/usr/local/pgsql/pg74.215.jdbc3.jar:/usr/local/pg

sql/postgresql-8.0-310.jdbc3.jar

when i run this simple java program i get
error as follows:
[root@roopesh bin]# javac
PostgreSQLTest.java//compile
PostgreSQLTest.java: In class `PostgreSQLTest':
PostgreSQLTest.java: In method
`PostgreSQLTest.main(java.lang.String[])':
PostgreSQLTest.java:7: Exception
`java.lang.ClassNotFoundException' must be caught,
or
it must be declared in the `throws' clause of
`main'.

Class.forName("postgresql.Driver").newInstance();
^
PostgreSQLTest.java:7: Exception
`java.lang.InstantiationException' must be caught,
or
it must be declared in the `throws' clause of
`main'.

Class.forName("postgresql.Driver").newInstance();
^
PostgreSQLTest.java:7: Exception
`java.lang.IllegalAccessException' must be caught,
or
it must be declared in the `throws' clause of
`main'.

Class.forName("postgresql.Driver").newInstance();

my PostgreSQLTest.java is as below
import java.sql.*;

class PostgreSQLTest {
public static void main (String[] args) {
try {
Driver driver = (Driver)

Class.forName("postgresql.Driver").newInstance();
DriverManager.registerDriver(driver);

String url = "jdbc:postgresql:javatest";
Connection con =
DriverManager.getConnection(url, "postgres", "");
Statement stm = con.createStatement();

stm.setQueryTimeout(10);
ResultSet rs = stm.executeQuery("select col1
from test");

rs.next();

System.out.println(rs.getString(1));

} catch (SQLException e) {

System.out.println("Exception!");
System.out.println(e.toString());
}
}
}
This is one simple example

Now when i tried to perform operations with tomcat
simple sevlet works but with database operations i
get
the following errors
file is:ShowBedrock.java
import javax.servlet.*;
import javax.servlet.http.*;

public class ShowBedrock extends HttpServlet
{
public String getServletInfo()
{
return "Servlet connects to PostgreSQL
database
and displays result of a SELECT";
}

private Connection dbcon; // Connection for
scope
of ShowBedrock

// "init" sets up a database connection
public void init(ServletConfig config) throws
ServletException
{
String loginUser = "postgres";
String loginPasswd = "roopesh";
String loginUrl =
"jdbc:postgresql://localhost/bedrock";

// Load the PostgreSQL driver
try
{

Class.forName("org.postgresql.Driver");
dbcon =
DriverManager.getConnection(loginUrl, loginUser,
loginPasswd);
}
catch (ClassNotFoundException ex)
{

System.err.println("ClassNotFoundException: " +
ex.getMessage());
throw new ServletException("Class not
found Error");
}
catch (SQLException ex)
{
System.err.println("SQLException: " +
ex.getMessage());
}
}

//
Use http GET

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html"); //
Response mime type

=== message truncated ===

**************************
Visit My Home Page
http://www.geocities.com/ks_moses
updated: 28 Sep 2004.
Simon Moses
**************************

__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/

#3David Gagnon
dgagnon@siunik.com
In reply to: java unix (#1)
Re: preoblem in jdbc postgresql and tomcat

Hi,

I see 2 jdbc version in your classpath pg73jdbc3.jar and
pg74.215.jdbc3.jar.

For Tomcat you just have to drop the jdbc Jar file in

/usr/local/jakarta-tomcat-4.1.31/common/lib

No classpath to change.. Nothing.

After create an entry in Server.xml or create a Context.xml to define your datasource. See Tomcat Doc.

Works oki for me

/David

java unix wrote:

Show quoted text

hi,
i have installed postgresql-7.4.1,and tomcat4.1.31and
j2sdk1.4.2_07
for tomcat i have included path
/usr/local/jakarta-tomcat-4.1.31
for j2sdk path set is /usr/local/j2sdk1.4.2_07
and CLASSPATH SET IS:
/usr/local/pgsql/pg73jdbc3.jar:/usr/local/pgsql/pg74.215.jdbc3.jar:/usr/local/pg
sql/postgresql-8.0-310.jdbc3.jar:/usr/local/j2sdk1.4.2_07/lib/tools.jar:/usr/loc
al/j2sdk1.4.2_07/jre/lib/rt.jar:/usr/local/jakarta-tomcat-4.1.31/common/lib/serv
let.jar:/usr/local/jakarta-tomcat-4.1.31/common/lib:/usr/local/pgsql:/usr/local/
j2sdk1.4.2_07/jre/lib:/usr/local/j2sdk1.4.2_07/lib
i have included all the paths

all the above path are set in /etc/profile/
and i set path for all three jar
files:/usr/local/pgsql/pg73jdbc3.jar:/usr/local/pgsql/pg74.215.jdbc3.jar:/usr/local/pg
sql/postgresql-8.0-310.jdbc3.jar

when i run this simple java program i get
error as follows:
[root@roopesh bin]# javac PostgreSQLTest.java//compile
PostgreSQLTest.java: In class `PostgreSQLTest':
PostgreSQLTest.java: In method
`PostgreSQLTest.main(java.lang.String[])':
PostgreSQLTest.java:7: Exception
`java.lang.ClassNotFoundException' must be caught, or
it must be declared in the `throws' clause of `main'.

Class.forName("postgresql.Driver").newInstance();
^
PostgreSQLTest.java:7: Exception
`java.lang.InstantiationException' must be caught, or
it must be declared in the `throws' clause of `main'.

Class.forName("postgresql.Driver").newInstance();
^
PostgreSQLTest.java:7: Exception
`java.lang.IllegalAccessException' must be caught, or
it must be declared in the `throws' clause of `main'.

Class.forName("postgresql.Driver").newInstance();

my PostgreSQLTest.java is as below
import java.sql.*;

class PostgreSQLTest {
public static void main (String[] args) {
try {
Driver driver = (Driver)

Class.forName("postgresql.Driver").newInstance();
DriverManager.registerDriver(driver);

String url = "jdbc:postgresql:javatest";
Connection con =
DriverManager.getConnection(url, "postgres", "");
Statement stm = con.createStatement();

stm.setQueryTimeout(10);
ResultSet rs = stm.executeQuery("select col1
from test");

rs.next();

System.out.println(rs.getString(1));

} catch (SQLException e) {

System.out.println("Exception!");
System.out.println(e.toString());
}
}
}
This is one simple example

Now when i tried to perform operations with tomcat
simple sevlet works but with database operations i get
the following errors
file is:ShowBedrock.java
import javax.servlet.*;
import javax.servlet.http.*;

public class ShowBedrock extends HttpServlet
{
public String getServletInfo()
{
return "Servlet connects to PostgreSQL database
and displays result of a SELECT";
}

private Connection dbcon; // Connection for scope
of ShowBedrock

// "init" sets up a database connection
public void init(ServletConfig config) throws
ServletException
{
String loginUser = "postgres";
String loginPasswd = "roopesh";
String loginUrl =
"jdbc:postgresql://localhost/bedrock";

// Load the PostgreSQL driver
try
{
Class.forName("org.postgresql.Driver");
dbcon =
DriverManager.getConnection(loginUrl, loginUser,
loginPasswd);
}
catch (ClassNotFoundException ex)
{

System.err.println("ClassNotFoundException: " +
ex.getMessage());
throw new ServletException("Class not
found Error");
}
catch (SQLException ex)
{
System.err.println("SQLException: " +
ex.getMessage());
}
}

//
Use http GET

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html"); //
Response mime type

// Output stream to STDOUT
PrintWriter out = response.getWriter();

out.println("<HTML><Head><Title>Bedrock</Title></Head>");
out.println("<Body><H1>Bedrock</H1>");

try
{
// Declare our statement
Statement statement =
dbcon.createStatement();

String query = "SELECT name, dept, ";
query += " jobtitle ";
query += "FROM employeee ";

// Perform the query
ResultSet rs =
statement.executeQuery(query);

out.println("<table border>");

// Iterate through each row of rs
while (rs.next())
{
String m_name =
rs.getString("name");
String m_dept =
rs.getString("dept");
String m_jobtitle =
rs.getString("jobtitle");
out.println("<tr>" +
"<td>" + m_name +
"</td>" +
"<td>" + m_dept +
"</td>" +
"<td>" + m_jobtitle +
"</td>" +
"</tr>");
}

out.println("</table></body></html>");
statement.close();
}
catch(Exception ex)
{
out.println("<HTML>" +
"<Head><Title>" +
"Bedrock: Error" +
"</Title></Head>\n<Body>"
+
"<P>SQL error in doGet: "
+
ex.getMessage() +
"</P></Body></HTML>");
return;
}
out.close();
}
}
when i compile i donot get any errors and class file
is created
when i display it in webbrowser as
http://localhost:8080/examples/servlet/ShowBedrock
the result is as follows

Bedrock
SQL error in doGet: null

and i am working in linux
please can any one help me with this problem i would
like to know the solution very soon.please i have to
do my project regarding this .i have not yet started
the coding of my project.i am stuck up with this
configuration.
if the above steps what i have done is wrong please
tell me step by step how i have to do
please reply soon

________________________________________________________________________
Yahoo! India Matrimony: Find your life partner online
Go to: http://yahoo.shaadi.com/india-matrimony

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org