Methods Summary |
---|
public void | destroy()
try
{ con.close();
}
catch( SQLException e )
{ e.printStackTrace();
}
|
public void | doGet(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 void | init(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 void | listAll(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 void | select(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>" );
|