Tomcat and JDK 1.3.1 Exception problems... - Mailing list pgsql-jdbc
From | Michael Hanna |
---|---|
Subject | Tomcat and JDK 1.3.1 Exception problems... |
Date | |
Msg-id | 18874756-B6AE-11D6-AFF7-00039308EB2C@hwcn.org Whole thread Raw |
Responses |
Re: Tomcat and JDK 1.3.1 Exception problems...
|
List | pgsql-jdbc |
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; } }
pgsql-jdbc by date: