Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DAO
- package eksamensklasser_blog;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import dk.au.hum.imv.persistence.db.DatabasePersistent;
- import dk.au.hum.imv.persistence.db.JDBCConnectionFactory;
- public class BlogDao extends DatabasePersistent {
- public static Blog getBlogById(long id) {
- Blog result = null;
- Connection con = null;
- try {
- con = JDBCConnectionFactory.getNewConnection();
- String sql = "SELECT * FROM blog WHERE id_blog = ?";
- PreparedStatement prepStat = con.prepareStatement(sql);
- prepStat.setLong(1, id);
- ResultSet res = prepStat.executeQuery();
- while (res.next()) {
- result = createBlogFromResultSet(res);
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- } finally {
- JDBCConnectionFactory.closeConnection(con);
- }
- return result;
- }
- public static ArrayList<Blog> getAllBlogs() {
- ArrayList<Blog> result = new ArrayList<Blog>();
- Connection con = null;
- try {
- con = JDBCConnectionFactory.getNewConnection();
- String sql = "SELECT * FROM blog";
- PreparedStatement prep = con.prepareStatement(sql);
- ResultSet res = prep.executeQuery();
- while (res.next()) {
- Blog blog = createBlogFromResultSet(res);
- result.add(blog);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- JDBCConnectionFactory.closeConnection(con);
- }
- return result;
- }
- static Blog createBlogFromResultSet(ResultSet res) throws SQLException {
- Blog blog = null;
- String subject = res.getString("blog.blog_subject");
- int id = res.getInt("blog.id_blog");
- blog = new Blog(subject, id);
- return blog;
- }
- public static void saveBlog(Blog blog) {
- Connection con = null;
- try {
- con = JDBCConnectionFactory.getNewConnection();
- String sql = "";
- if (!blog.isInDatabase()) { //insert
- sql = "INSERT INTO blog (blog_subject) " +
- "VALUES (?)";
- } else { //update
- sql = "UPDATE blog SET blog_subject = ?" +
- "WHERE id = ?";
- }
- PreparedStatement prep = con.prepareStatement(sql);
- prep.setString(1, blog.getBlogSubject());
- if (blog.isInDatabase()) { //hvis bogen er i DB skal vi huske at sætte kriteriet for hvilken bog vi skal opdatere
- prep.setLong(2, blog.getDatabaseId());
- }
- prep.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- JDBCConnectionFactory.closeConnection(con);
- }
- }
- public static ArrayList<Blog> findBlog(String searchString) {
- ArrayList<Blog> result = new ArrayList<Blog>();
- Connection con = null;
- try {
- con = JDBCConnectionFactory.getNewConnection();
- String sql = "SELECT * FROM blog WHERE id_blog LIKE ? OR blog_subject LIKE ?";
- PreparedStatement prep = con.prepareStatement(sql);
- prep.setString(1, "%" + searchString + "%"); //vi tilføjer % til vores søgning, så vi søger på alt der starter med brugerens input
- prep.setString(2, "%" + searchString + "%");//uden % på begge sider af searchString, ville den ikke kunne søge på tværs af ordene
- ResultSet res = prep.executeQuery();
- while (res.next()) {
- Blog blog = createBlogFromResultSet(res);
- result.add(blog);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- JDBCConnectionFactory.closeConnection(con);
- }
- return result;
- }
- }
- //OPRET NYT OBJEKT + SØG - SØGNING VIRKER
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <jsp:include page = "css/Eksamen.css/"/>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Opret en blog</title>
- </head>
- <%@ page import="dk.au.hum.imv.persistence.db.JDBCConnectionFactory, eksamensklasser_blog.*, java.util.*, java.sql.*"%>
- <%
- String title = request.getParameter("titel");
- String createButton = request.getParameter("create");
- if (createButton != null) {
- JDBCConnectionFactory.initManualConnectionHandling("jdbc:mysql://student.hum.au.dk/u201506592",
- "u201506592", "wu2b9H2R", "com.mysql.jdbc.Driver");
- Blog blog1 = new Blog(title);
- BlogDao.saveBlog(blog1);
- }
- %>
- <body>
- <h1>Opret en ny blog</h1>
- <form action="display_blog.jsp" method="post">
- <fieldset>
- <table>
- <tr>
- <td align="right">Blog-emne:</td>
- <td><input type="text" name="title"></td>
- </tr>
- <tr>
- <td></td>
- <td><br><input type="submit" name ="create" value="Opret"></td>
- </table>
- </fieldset>
- </form>
- <h1>Søg efter blog</h1>
- <form action="display_blog.jsp" method="get">
- <fieldset>
- <input type="text" name="search"/>
- <input type="submit" value="Søg"/>
- </fieldset>
- </form>
- </body>
- </html>
- //VIS NYT OBJEKT OG/ELLER SØGNING
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Create blog/search blog</title>
- </head>
- <%@ page import="dk.au.hum.imv.persistence.db.JDBCConnectionFactory, eksamensklasser_blog.*, java.util.*, java.sql.*"%>
- <%
- JDBCConnectionFactory.initManualConnectionHandling("jdbc:mysql://student.hum.au.dk/u201506592",
- "u201506592", "wu2b9H2R", "com.mysql.jdbc.Driver");
- ArrayList<Blog> blogs = null;
- String search = request.getParameter("search");
- if (search != null && !search.equals("")) {
- blogs = BlogDao.findBlog(search);
- } else {
- blogs = BlogDao.getAllBlogs();
- }
- %>
- <body>
- <%
- String titel = request.getParameter("title");
- %>
- <h1>Nu har du oprettet en blog med emnet:</h1>
- <table>
- <tr>
- <td><%= titel %></td>
- </tr>
- </table>
- <h1>Du fandt følgende blogs:</h1>
- <table>
- <tr>
- <th>Emne</th>
- </tr>
- <%
- for (Blog blog : blogs) {
- %>
- <tr>
- <td><%=blog.getBlogSubject()%></td>
- </tr>
- <%
- }
- %>
- </table>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement