Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.sql.*;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.*;
- public class PostStory extends HttpServlet {
- private static final String DB_URL = "jdbc:h2:D://Speak Out/database/stories";
- private static final String USER = "";
- private static final String PASS = "";
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException
- {
- Connection conn = null;
- Statement stmt = null;
- try {
- Class.forName("org.h2.Driver");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- stmt = conn.createStatement();
- String title = request.getParameter("txtTitle");
- String story = request.getParameter("txtStory");
- if ((title == "") | (story == "")) {
- response.sendRedirect("/index.jsp");
- stmt.close();
- conn.close();
- }
- Date date = new Date();
- SimpleDateFormat dateFormat = new SimpleDateFormat("'Posted on' MMMMMM d',' y 'at' h:mm a",
- Locale.ENGLISH);
- String sql = "INSERT INTO STORIES (title, contents, post_date) VALUES ('"+title+"', " +
- "'"+story+"', '"+dateFormat.format(date)+"')";
- stmt.executeUpdate(sql);
- response.sendRedirect("/index.jsp");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (stmt!=null)
- conn.close();
- } catch (SQLException ignored) {
- }
- try {
- if (conn!= null)
- conn.close();
- } catch (SQLException se) {
- se.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement