//Copyright Jane Jakeman W1156718 18 April 2009 //This class connects to database, and selects values from mySql //Passes values to html format and servlet prints values //database close //when user has typed in updated data, submits to servlet/updateDbPages //updateDb continues the insert process which creates a new update whilst //retaining the previous version timestamped //This is all my own research and code written painfully by myself //This code works on a website admin section so that an untrained admin //may easily update the page content text and title of that website page //CALLS UpdateDbPages import java.sql.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class UpdatePageContent extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doPost(req,res); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String connectionURL = "jdbc:mysql://localhost/databasenameRemoved"; Connection connection=null; res.setContentType("text/html"); PrintWriter out = res.getWriter(); String fPageCat = req.getParameter("formPageCat"); try { Class.forName("org.gjt.mm.mysql.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost/databaseNameRemoved","usernameRemoved","passwordRemoved"); Statement stmt = null; ResultSet rs = null; stmt = connection.createStatement(); rs = stmt.executeQuery("SELECT * FROM TBLPAGES WHERE pageCat=('"+fPageCat+"')ORDER BY pageDate DESC LIMIT 1"); while(rs.next()) { // out.print(rs.getObject(1).toString());//id // out.print("\n"); String b=(rs.getObject(2).toString());//pageTitle String c=(rs.getObject(3).toString());//pageText String d=(rs.getObject(4).toString());//pageCat String e=(rs.getObject(5).toString());//pageDate //timestamp format// //put variables into html form text fields out.print("
"); out.print(""); out.print(""); out.print(""); out.close(); connection = null; } } catch(ClassNotFoundException e) { out.println("Couldn't load database driver: " + e.getMessage()); } catch (SQLException e) { throw new ServletException("Servlet Could not display records.", e); } catch(Exception e) { out.println(e); } finally { try { if(connection != null) connection.close(); } catch (SQLException ignored) { out.println(ignored); } } out.close(); } }