FileDocCategorySizeDatePackage
DBAccess.javaAPI DocExample3737Mon Oct 16 19:44:06 BST 2000None

DBAccess

public class DBAccess extends HttpServlet

Fields Summary
private Connection
con
Constructors Summary
Methods Summary
public voiddestroy()


     
     try
      {  con.close();
      }
      catch( SQLException e )
		{	e.printStackTrace();
		}
   
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

  res.setContentType( "text/html" );
      PrintWriter out = res.getWriter();
      out.println( "<html>" );
      out.println( "<head>" );
      out.println( "<title>Access PostGres</title>" );
      out.println( "</head>" );
      out.println( "<body>" );
      out.println( "<h1>Accessing PostGres:</h1>" );
   	out.println( "<P> </P>" );
   	String service = req.getParameter( "service" );
   	try
   	{	if( service.equals( "listAll" ) )
   		{	listAll( out );
   		}
   		if( service.equals( "select" ) )
   		{	select( out, req.getParameter( "field" ), req.getParameter( "value" ) );
   		}
   	}
   	catch( SQLException e )
   	{	e.printStackTrace();	// to console/log
   		out.println( e ); // on web page too
   	}
   	out.println( "</body>" );
      out.println( "</html>" );
      out.close();
   
public voidinit(javax.servlet.ServletConfig config)

	super.init( config );
		// Make connection to database
		try
		{ 	new postgresql.Driver();
			con = DriverManager.getConnection( "jdbc:postgresql://localhost/ucjava",
															"ucjava", "ucJava" );
		}
		catch( SQLException e )
		{	e.printStackTrace();
			// ought to abort in some way
		}
 	
private voidlistAll(java.io.PrintWriter out)

	Statement stmt = con.createStatement();
		ResultSet results = stmt.executeQuery( "SELECT * FROM students;" );
		ResultSetMetaData rsmd = results.getMetaData();
		
		out.println( "<TABLE cols=" + rsmd.getColumnCount() + ">");
		out.println( "<TR>" );
		int column;
		// Column headings
		for( column=1; column <= rsmd.getColumnCount(); column++ )
		{	out.println( "<TD><B>" + rsmd.getColumnLabel( column ) + "</B></TD>" );
		}
		out.println( "</TR>" );
		// Data rows
		while( results.next() == true )
		{	out.println( "<TR>" );
			for( column = 1; column <= rsmd.getColumnCount(); column++ )
			{	out.println( "<TD>" );
				out.println( results.getString( column ) );
				out.println( "</TD>" );
			}
			out.println( "</TR>" );
		}
		out.println( "</TABLE>" );
	
private voidselect(java.io.PrintWriter out, java.lang.String field, java.lang.String value)

	Statement stmt = con.createStatement();
		ResultSet results = stmt.executeQuery( "SELECT * FROM students WHERE "
			+ field + "= \'" + value + "\';");
		ResultSetMetaData rsmd = results.getMetaData();
		
		out.println( "<TABLE cols=" + rsmd.getColumnCount() + ">");
		out.println( "<TR>" );
		int column;
		// Column headings
		for( column=1; column <= rsmd.getColumnCount(); column++ )
		{	out.println( "<TD><B>" + rsmd.getColumnLabel( column ) + "</B></TD>" );
		}
		out.println( "</TR>" );
		// Data rows
		while( results.next() == true )
		{	out.println( "<TR>" );
			for( column = 1; column <= rsmd.getColumnCount(); column++ )
			{	out.println( "<TD>" );
				out.println( results.getString( column ) );
				out.println( "</TD>" );
			}
			out.println( "</TR>" );
		}
		out.println( "</TABLE>" );