Re: Tomcat and JDK 1.3.1 Exception problems...

From: Toby <toby(at)paperjet(dot)com>
To: Michael Hanna <zen(at)hwcn(dot)org>, pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Tomcat and JDK 1.3.1 Exception problems...
Date: 2002-08-23 16:24:48
Message-ID: 5.1.0.14.0.20020823170730.00a71f48@mail.flirble.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Why can't you catch both? I do a similar thing all over the place in my
projects and it's fine.
do something along the lines of...

DBThing dbt;

try
{
dbt = new DBThing(database, username, password);

// all is fine and dandy, proceed as normal with
// queries etc
}
catch (ClassNotFoundException e)
{
System.out.println("***Exception:\n" + e.getMessage());
e.printStackTrace();
}
catch (SQLException e)
{
System.out.println("***Exception:\n" + e.getMessage());
e.printStackTrace();
}
finally
{
// you're done with dbt. there may have been an exception, in
which
// case you've already handled it, or things may haved worked
fine
// in which case...good.

// add a cleanup() method to DBThing which closes any
// outstanding resultsets, statements or connection
if (dbt != null)
dbt.cleanup();
}

this works, catches both exceptions, does what you want and it works.

also, it may be worth just putting the class.forname bit in a static
initialiser so that it doesn't get needlessly called each and everytime you
instantiate an DBThing object. course, no harm but to be all puritanical
about it, you could skip this. then again, might be nice to have a static
dbdriver variable in DBThing so that whenever you create a connection, it
does the class.forname on the dbdriver variable. if you wanna hotswap
drivers then simply call the (newly added) DBThing.setDBDriver() method
passing in the new name of the driver. but thats for another day.

toby (usually lurking)

At 11:36 23/08/2002 -0400, Michael Hanna wrote:
>Yes, this is a basic Java problem...but I feel stuck.
>
>I'm creating a Tomcat servlet that connects to a local PGSQL DB. I'm
>trying to create a class called DBThing that will do my DB accessing.
>However, I'm running into problems with exceptions. the line
>
>dbt = new DBThing(database, username, password);
>
>needs to catch an SQLException and a ClassNotFoundException. However I
>can't catch both! And the compiler won't let the method throw either!
>
>Are there any other Tomcat users on here?? Is there a better way to access
>the DB??
>
>here are both classes..BTW the Rigby.java won't compile since my final
>attempt to fix it was unsuccessful, so I simply left it... :/
>
>---
>
>Rigby.java
>
>---
>
>
>// Copyright Michael Hanna 2002
>// do not use without permission of author
>
>import java.io.*;
>import javax.servlet.*;
>import javax.servlet.http.*;
>import java.sql.*; // All we need for JDBC
>
>public class Rigby extends HttpServlet {
>
> public void doGet(HttpServletRequest request, HttpServletResponse
> response)throws IOException, ServletException
>{
> PrintWriter out;
> DBThing dbt;
> ResultSet results;
>
> String database = "aDBname";
> String username = "aUserName";
> String password = "aPassword";
>
> //Class.forName("org.postgresql.Driver"); //load the driver
>
> try {
> dbt = new DBThing(database, username, password);
> }
> catch (ClassNotFoundException cnf)
> {
> System.out.println("***Exception:\n"+cnf);
> cnf.printStackTrace();
> }
> try {
> dbt = new DBThing(database, username, password);
> }
> catch (SQLException sqle)
> {
> System.out.println("***Exception:\n"+sqle);
> sqle.printStackTrace();
> }
>
> response.setContentType("text/html");
> out = response.getWriter();
> out.println("<html>");
> out.println("<body>");
> out.println("<head>");
> out.println("<title>Rigby</title>");
> out.println("</head>");
> out.println("<body>");
> out.println("<h1>Contents of friends:</h1>");
>
> results = dbt.queryDB("select * from friends");
>
> if (results != null)
> {
> out.println("<B>id" + "First Name" +"Surname" + "Email" +
> "Phone"+"Bday</B>");
> while (results.next())
> {
> out.println(results.getInt("id") +
> results.getString("firstname") + results.getString("surname")+
> results.getString("email")+results.getInt("tel")+results.getString("date")
> + "\n");
> }
> }
> else {
> out.println("The friends database is empty.<br>");
> }
> results.close();
>
> out.println("</body>");
> out.println("</html>");
>
>}
>}
>
>----
>
>DBThing.java
>
>
>---
>
>// Copyright Michael Hanna 2002
>// do not use without permission of author
>
>import java.sql.*; // All we need for JDBC
>import java.lang.*;
>
>public class DBThing
>{
> Connection db; // A connection to the database
> Statement sql; // Our statement to run queries with
> DatabaseMetaData dbmd; // This is basically info the driver delivers
> // about the DB it just connected to. I use
> // it to get the DB version to confirm the
> // connection in this example.
>
> public DBThing(String database, String username, String password)
> throws ClassNotFoundException, SQLException
> {
>
> Class.forName("org.postgresql.Driver"); //load the driver
>
> db = DriverManager.getConnection("jdbc:postgresql:"+database,
> username,
> password); //connect to the db
>
> dbmd = db.getMetaData(); //get MetaData to confirm connection
>
> sql = db.createStatement(); //create a statement that we can use
> later
>
> }
>
> public ResultSet queryDB(String query) throws SQLException
> {
> ResultSet rs;
> rs = sql.executeQuery(query); // try to query DB
> return rs;
> }
>}
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Paul Stead 2002-08-23 16:24:57 Re: Proper state after insertRow
Previous Message Jason Stewart 2002-08-23 15:55:59 Authentication Problems