I created a simple servlet that will use a JDBC driver and access a
table in a postgresql database.
It will display the table in HTML formatted output. This is great but
I'd like to extend it's functionality.
I'd like to have a way so that the user can Add, Delete and Update the
table via the web page/servlet.
This is easy enough, but how do I have the servlet update its own output
in the web browser so that it reflects the new changes? Is there a
refresh command or something.
I was thinking having a jsp page where the user inputs the changes, and
then POST it to the servlet.. still not sure how the page gets updated.
Anybody have any example sites/code??
Here's my classes....
Rigby.class is the client class that calls on DBThing to do the database
access.
---
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;
Date birthdate, now;
boolean isColoured = true; // is this table row coloured or not
String database = "rigby";
String username = "nevermind";
String password = "nevermind";
String mdMsg;
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("<h2>Contents of friends:</h2>");
try {
Class.forName("org.postgresql.Driver"); //load the driver
//out.println("<B>Inside try2</B>");
dbt = new DBThing(database, username, password);
mdMsg = dbt.getMetaDataMsg();
out.println(mdMsg + "<BR>");
results = dbt.queryDB("select firstname, surname, email, tel,
birthdate from friends order by surname asc");
//isNull = results.wasNull();
out.println("ResultSet null?: " + results.wasNull() + "<BR>");
//results.last();
//out.println("Current Row: " + results.getRow() + "<BR>");
out.println("<form type=POST action=carts.jsp>");
out.println("<BR>");
out.println("Order by:");
out.println("<SELECT NAME='Order by'>");
out.println("<OPTION>First Name");
out.println("<OPTION>Surname");
out.println("<OPTION>Birthdate");
out.println("</SELECT>");
out.println("<INPUT TYPE=submit name='submit' value='Go'>");
out.println("</form>");
out.println("<BR");
if (results != null)
{
out.println("<TABLE border = 1 cellspacing = 0>");
out.println("<TR>");
//out.println("<TD><B>id</TD></B>"
out.println("<TD><B>First Name</TD></B>"
+"<TD><B>Surname</TD></B>" + "<TD><B>Email</TD></B>" +
"<TD><B>Phone</TD></B>"+"<TD><B>Birthdate</TD></B>");
out.println("</TR>");
//out.println("\n");
while (results.next())
{
if (isColoured == false) {
out.println("<TR>");
isColoured = true;
}
else {
out.println("<TR bgcolor = #CCCCCC>");
isColoured = false;
}
//birthdate = new Date(results.getDate("birthdate"));
//now = new Date();
//out.println("<TD>" + results.getInt("id") + "</TD>");
out.println("<TD>" + results.getString("firstname") +
"</TD>" + "<TD>" + results.getString("surname")+ "</TD>" + "<TD> <A
href = mailto:" + results.getString("email") +">"+
results.getString("email")+ "</A> </TD>" + "<TD>"
+results.getString("tel")+"</TD>" + "<TD>" +
results.getString("birthdate")+"</TD>");
out.println("</TR>");
}
}
else {
out.println("The friends database is empty.<br>");
}
results.close();
out.println("</TABLE>");
} catch (ClassNotFoundException cnf) {
out.println("***Exception:\n"+cnf);
cnf.printStackTrace();
} catch (SQLException se) {
out.println("***Exception:\n"+se);
se.printStackTrace();
}
//out.println("hey2");
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
//out.println("Connection to "+dbmd.getDatabaseProductName()+" "+
// dbmd.getDatabaseProductVersion()+"
successful.\n");
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;
}
public String getMetaDataMsg() throws SQLException
{
String msg;
msg = dbmd.getDatabaseProductName()+"
"+dbmd.getDatabaseProductVersion();
return msg;
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>