//Jane Jakeman W1156718 18 April 2009 //This class connects to database, and inputs values to mySql //Collects the current timestamp time from getCurrentJavaSqlTimestamp() //Timestamp creates ability to sort data and display last record //when user has typed in updated data, submits to servlet/updateDbPages //updateDb continues the insert process which updates the database //This code written painfully by Jane Jakeman copyright 2010 //This code is CALLED by UpdatePageContent import java.sql.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class UpdateDbPages extends HttpServlet { public static java.sql.Timestamp getCurrentJavaSqlTimestamp() { java.util.Date date = new java.util.Date(); return new java.sql.Timestamp(date.getTime()); } 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 pTitle = req.getParameter("pageTitle"); String pText = req.getParameter("pageText"); String pCat = req.getParameter("pageCat"); try { // Load the database driver Class.forName("org.gjt.mm.mysql.Driver"); // Get a Connection to the database connection = DriverManager.getConnection(connectionURL, "usernameRemoved", "passwordRemoved"); //prepare the first statement String sql = "insert into TBLPAGES values (?,?,?,?,?)"; PreparedStatement pst = connection.prepareStatement(sql); //INSERT TO TBL PERSON pst.setInt(1,0);//setting auto_increment to default 0, db will create next number pst.setString(2, pTitle); pst.setString(3, pText); pst.setString(4, pCat); java.sql.Timestamp timestamp = getCurrentJavaSqlTimestamp(); pst.setTimestamp(5,timestamp); int numRowsChanged = pst.executeUpdate(); out.println("Database updated successfully, number of pages changed are " + numRowsChanged +"."); out.println("
"); out.println("Please go back to Admin page to log out or to select other update options."); connection = null; pst = 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(); } } /* 2pageTitle 3pageText 4pageCat 5pageDate */ // display records