Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package controllers;
- import org.springframework.web.servlet.ModelAndView;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RequestMethod;
- import hibernate.Movies;
- @Controller
- public class SubmitDataController {
- private ModelAndView modelandview;
- @RequestMapping(value = "/submit", method = RequestMethod.POST)
- public @ResponseBody
- ModelAndView submitDeleteChanges(
- @RequestParam("surname") String movies) { //instead of receiving surname, will need to find a way to receive all checked items and will need to make. Will need to call it something else, then make adjustment in books.jsp
- System.out.println("TESTING FORM");
- System.out.println(movies);
- return modelandview;
- }
- public ModelAndView titlePage() {
- return modelandview;
- }
- public ModelAndView getModelandview() {
- return modelandview;
- }
- public void setModelandview(ModelAndView modelandview) {
- this.modelandview = modelandview;
- }
- }
- package dao;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.springframework.web.servlet.ModelAndView;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.Query;
- import hibernate.Books;
- import hibernate.Music;
- import hibernate.Contacts;
- import hibernate.Movies;
- import java.util.List;
- //DAO = data access object. This class is the interface between the code and the database.
- public class HibernateDataDAO {
- private SessionFactory sessionFactory;
- private Transaction transaction;
- public void insertBooks(Books books) {
- Session session = sessionFactory.openSession();
- try {
- transaction = session.beginTransaction();
- session.save(books);
- transaction.commit();
- }
- catch (Exception e) {
- if (transaction != null) transaction.rollback();
- throw e;
- }
- finally {
- session.close();
- }
- }
- public void deleteBooks(Books books, int booksKey){
- Session session = sessionFactory.openSession();
- try{
- transaction = session.beginTransaction();
- Object persistentInstance = session.load(hibernate.Books.class, booksKey);
- if (persistentInstance != null) {
- session.delete(persistentInstance);
- transaction.commit();
- }
- }catch(HibernateException e){
- if(transaction!= null) transaction.rollback();
- e.printStackTrace();
- }
- finally{
- session.close();
- }
- }
- @SuppressWarnings("unchecked")
- public ModelAndView getBooks(ModelAndView modelandview){
- Session session = sessionFactory.openSession();
- List<Books> books = null;
- try{
- transaction = session.beginTransaction();
- Query query = session.createQuery("select books from Books as books");
- books = (List<Books>) query.list();
- modelandview.addObject("books", books);
- System.out.println("TitletAuthort#");
- for(Books book: books) {
- System.out.println(book.getTitle() + "t" + book.getAuthor() + "t" + book.getBooksKey());
- }
- transaction.commit();
- }catch(HibernateException e){
- if(transaction!= null) transaction.rollback();
- e.printStackTrace();
- }
- finally{
- session.close();
- }
- return modelandview;
- }
- public void insertMusic(Music music) {
- Session session = sessionFactory.openSession();
- try {
- transaction = session.beginTransaction();
- session.save(music);
- transaction.commit();
- }
- catch (Exception e) {
- if (transaction != null) transaction.rollback();
- throw e;
- }
- finally {
- session.close();
- }
- }
- public void deleteMusic(Music music, int musicKey){
- Session session = sessionFactory.openSession();
- try{
- transaction = session.beginTransaction();
- Object persistentInstance = session.load(hibernate.Music.class, musicKey);
- if (persistentInstance != null) {
- session.delete(persistentInstance);
- transaction.commit();
- }
- }catch(HibernateException e){
- if(transaction!= null) transaction.rollback();
- e.printStackTrace();
- }
- finally{
- session.close();
- }
- }
- @SuppressWarnings("unchecked")
- public ModelAndView getMusic(ModelAndView modelandview){
- Session session = sessionFactory.openSession();
- List<Music> music = null;
- try{
- transaction = session.beginTransaction();
- Query query = session.createQuery("select music from Music as music");
- music = (List<Music>) query.list();
- modelandview.addObject("music", music);
- System.out.println("TitletArtistt#");
- for(Music musics: music) {
- System.out.println(musics.getTitle() + "t" + musics.getArtist() + "t" + musics.getMusicKey());
- }
- transaction.commit();
- }catch(HibernateException e){
- if(transaction!= null) transaction.rollback();
- e.printStackTrace();
- }
- finally{
- session.close();
- }
- return modelandview;
- }
- public void insertMovies(Movies movies) {
- Session session = sessionFactory.openSession();
- try {
- transaction = session.beginTransaction();
- session.save(movies);
- transaction.commit();
- }
- catch (Exception e) {
- if (transaction != null) transaction.rollback();
- throw e;
- }
- finally {
- session.close();
- }
- }
- public void deleteMovies(Movies movies, int movieKey){
- Session session = sessionFactory.openSession();
- try{
- transaction = session.beginTransaction();
- Object persistentInstance = session.load(hibernate.Movies.class, movieKey);
- if (persistentInstance != null) {
- session.delete(persistentInstance);
- transaction.commit();
- }
- }catch(HibernateException e){
- if(transaction!= null) transaction.rollback();
- e.printStackTrace();
- }
- finally{
- session.close();
- }
- }
- @SuppressWarnings("unchecked")
- public ModelAndView getMovies(ModelAndView modelandview){
- Session session = sessionFactory.openSession();
- List<Movies> movies = null;
- try{
- transaction = session.beginTransaction();
- Query query = session.createQuery("select movies from Movies as movies");
- movies = (List<Movies>) query.list();
- modelandview.addObject("movies", movies);
- System.out.println("TitletRatingt#");
- for(Movies movie: movies) {
- System.out.println(movie.getTitle() + "t" + movie.getRating() + "t" + movie.getMovieKey());
- }
- transaction.commit();
- }catch(HibernateException e){
- if(transaction!= null) transaction.rollback();
- e.printStackTrace();
- }
- finally{
- session.close();
- }
- return modelandview;
- }
- public void insertContacts(Contacts contacts) {
- Session session = sessionFactory.openSession();
- try {
- transaction = session.beginTransaction();
- session.save(contacts);
- transaction.commit();
- }
- catch (Exception e) {
- if (transaction != null) transaction.rollback();
- throw e;
- }
- finally {
- session.close();
- }
- }
- public void deleteContacts(Contacts contacts, int contactKey){
- Session session = sessionFactory.openSession();
- try{
- transaction = session.beginTransaction();
- Object persistentInstance = session.load(hibernate.Contacts.class, contactKey);
- if (persistentInstance != null) {
- session.delete(persistentInstance);
- transaction.commit();
- }
- }catch(HibernateException e){
- if(transaction!= null) transaction.rollback();
- e.printStackTrace();
- }
- finally{
- session.close();
- }
- }
- @SuppressWarnings("unchecked")
- public ModelAndView getContacts(ModelAndView modelandview){
- Session session = sessionFactory.openSession();
- List<Contacts> contacts = null;
- try{
- transaction = session.beginTransaction();
- Query query = session.createQuery("select contacts from Contacts as contacts");
- contacts = (List<Contacts>) query.list();
- modelandview.addObject("contacts", contacts);
- System.out.println("NametEmailtPhonet#");
- for(Contacts contact: contacts) {
- System.out.println(contact.getName() + "t" + contact.getEmail() + "t" + contact.getPhone() + contact.getContactKey());
- }
- transaction.commit();
- }catch(HibernateException e){
- if(transaction!= null) transaction.rollback();
- e.printStackTrace();
- }
- finally{
- session.close();
- }
- return modelandview;
- }
- public SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
- }
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <mvc:annotation-driven />
- <context:component-scan base-package="controllers" />
- <mvc:resources mapping="/resources/**" location="/resources/" />
- <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
- <!-- This viewResolver is looking in the directory listed below. ModelAndView is integrated into spring and automatically looks for viewResolver -->
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix">
- <value>/WEB-INF/pages/</value>
- </property>
- <property name="suffix">
- <value>.jsp</value>
- </property>
- </bean>
- <!-- When I make indexController, this BEAN sets the MODELANDVIEW attributes with the value INDEX. All these BEAN statements are instantiated at beginning of the program. These beans are injected in appropriate classes -->
- <!-- All the red text in console, you can actually see the beans being injected -->
- <bean id="indexController" class="controllers.IndexController">
- <property name="modelandview" value="index"/>
- </bean>
- <bean id="testController" class="controllers.TestController">
- <property name="modelandview" value="test"/>
- <property name="testModel" ref="testModel"/>
- </bean>
- <bean id="entertainmentModel" class="models.EntertainmentModel">
- <property name="data" ref="hibernateDataDao"/>
- </bean>
- <bean id="testModel" class="models.TestModel">
- <property name="data" ref="hibernateDataDao"/>
- </bean>
- <bean id="hibernateDataDao" class="dao.HibernateDataDAO">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <bean id="entertainmentController" class="controllers.EntertainmentController">
- <property name="bookModel" value="books"/>
- <property name="movieModel" value="movies"/>
- <property name="musicModel" value="music"/>
- <property name="contactsModel" value="contacts"/>
- <property name="model" ref="entertainmentModel"/>
- </bean>
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/databaseproject" />
- <property name="username" value="root" />
- <property name="password" value="Someone1" />
- </bean>
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="mappingResources">
- <list>
- <value>HibernateData.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <value>
- hibernate.dialect=org.hibernate.dialect.MySQLDialect
- hibernate.default_schema=databaseproject
- </value>
- </property>
- </bean>
- </beans>
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
- <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 5 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Database Project</title>
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <spring:url value="/webjars/bootstrap/3.2.0/css/bootstrap.min.css" var="bootstrap"/>
- <spring:url value="/webjars/jquery/1.11.1/jquery.min.js" var="jquery"/>
- <spring:url value="/webjars/bootstrap/3.2.0/js/bootstrap.min.js" var="bootstrapJS"/>
- <spring:url value="/webjars/bootstrap-table/1.9.1/dist/bootstrap-table.min.css" var="bootstrapTable"/>
- <spring:url value="/webjars/bootstrap-table/1.9.1/dist/bootstrap-table.min.js" var="bootstrapTableJS"/>
- <spring:url value="/resources/css/entertainment.css" var="entertainmentCss"/>
- <spring:url value="/resources/js/script.js" var="tableJS"/>
- <link href="${bootstrap}" rel="stylesheet"/>
- <link href="${bootstrapTable}" rel="stylesheet"/>
- <link href="${entertainmentCss}" rel="stylesheet"/>
- </head>
- <body>
- <div class="site-wrapper">
- <div class="site-wrapper-inner">
- <div class="cover-container">
- <div class="masthead clearfix">
- <div class="inner">
- <h3 class="masthead-brand">Entertainment App</h3>
- <nav>
- <ul class="nav masthead-nav">
- <li class="active"><a href="#">Home</a></li>
- </ul>
- </nav>
- </div>
- </div>
- <div class="inner cover">
- <div class="entertainmentDisplay">
- <h1>Movies</h1>
- <table class="zebra-style" id="dtable">
- <thead>
- <tr>
- <th scope="col">
- <input type="checkbox" class="checkall" />
- </th>
- <th scope="col">Title</th>
- <th scope="col">Rating</th>
- <th scope="col">#</th>
- </tr>
- </thead>
- <tbody>
- <c:forEach var="movies" items="${movies}">
- <tr>
- <td> <input type="checkbox" class="checkbox" /></td>
- <td>${movies.title} </td>
- <td>${movies.rating}</td>
- <td>${movies.movieKey}</td>
- </tr>
- </c:forEach>
- <tfoot>
- <tr>
- <!--<th colspan="2"><a href="javascript:;" class="btn btn-default deleteall" title="dtable">Delete Selected Items</a></th>-->
- <th colspan="2"><span href="javascript:;" id= "formsubmit" class="btn btn-default deleteall" title="dtable">Delete Selected Items</span></th>
- <th colspan="2"><a href="#" class="btn btn-default">Add Item</a></th>
- <div>
- </div>
- </tr>
- </tfoot>
- </tbody>
- </table>
- </div>
- </div>
- <div class="mastfoot">
- <div class="inner">
- <p><span class="bottomColor">Created by <a href="http://www.w.com">W</a> & <a href="http://www.s.com">S</a></span></p>
- </div>
- </div>
- </div>
- </div>
- </div>
- <script src="${jquery}"></script>
- <script src="${bootstrapJS}"></script>
- <script src="${bootstrapTableJS}"></script>
- <script src="${tableJS}"></script>
- </body>
- </html>
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="hibernate.Books" table="books">
- <id name="booksKey" column="booksKey" type="int">
- <generator class="increment"/>
- </id>
- <property name="title" column="title" type="string"/>
- <property name="author" column="author" type="string"/>
- </class>
- <class name="hibernate.Movies" table="movies">
- <id name="movieKey" column="movieKey" type="int">
- <generator class="increment"/>
- </id>
- <property name="title" column="title" type="string"/>
- <property name="rating" column="rating" type="string"/>
- </class>
- <class name="hibernate.Music" table="music">
- <id name="musicKey" column="musicKey" type="int">
- <generator class="increment"/>
- </id>
- <property name="title" column="title" type="string"/>
- <property name="artist" column="artist" type="string"/>
- </class>
- <class name="hibernate.Contacts" table="contacts">
- <id name="contactKey" column="contactKey" type="int">
- <generator class="increment"/>
- </id>
- <property name="name" column="name" type="string"/>
- <property name="email" column="email" type="string"/>
- <property name="phone" column="phone" type="string"/>
- </class>
- </hibernate-mapping>
- $(function () {
- var $result = $('#eventsResult');
- $('#eventsTable').on('all.bs.table', function (e, name, args) {
- console.log('Event:', name, ', data:', args);
- })
- .on('click-row.bs.table', function (e, row, $element) {
- $result.text('Event: click-row.bs.table');
- })
- .on('dbl-click-row.bs.table', function (e, row, $element) {
- $result.text('Event: dbl-click-row.bs.table');
- })
- .on('sort.bs.table', function (e, name, order) {
- $result.text('Event: sort.bs.table');
- })
- .on('check.bs.table', function (e, row) {
- $result.text('Event: check.bs.table');
- })
- .on('uncheck.bs.table', function (e, row) {
- $result.text('Event: uncheck.bs.table');
- })
- .on('check-all.bs.table', function (e) {
- $result.text('Event: check-all.bs.table');
- })
- .on('uncheck-all.bs.table', function (e) {
- $result.text('Event: uncheck-all.bs.table');
- })
- .on('load-success.bs.table', function (e, data) {
- $result.text('Event: load-success.bs.table');
- })
- .on('load-error.bs.table', function (e, status) {
- $result.text('Event: load-error.bs.table');
- })
- .on('column-switch.bs.table', function (e, field, checked) {
- $result.text('Event: column-switch.bs.table');
- })
- .on('page-change.bs.table', function (e, number, size) {
- $result.text('Event: page-change.bs.table');
- })
- .on('search.bs.table', function (e, text) {
- $result.text('Event: search.bs.table');
- });
- });
- $(document).ready(function () {
- zebra();
- $('.checkall').on('click', function () {
- var $this = $(this),
- // Test to see if it is checked
- checked = $this.prop('checked'),
- //Find all the checkboxes
- cbs = $this.closest('table').children('tbody').find('.checkbox');
- // Check or Uncheck them.
- cbs.prop('checked', checked);
- //toggle the selected class to all the trs
- cbs.closest('tr').toggleClass('selected', checked);
- });
- $('tbody tr').on('click', function () {
- var $this = $(this).toggleClass('selected');
- $this.find('.checkbox').prop('checked', $this.hasClass('selected'));
- if(!$this.hasClass('selected')) {
- $this.closest('table').children('thead').find('.checkall').prop('checked', false);
- }
- });
- $('.delete_single').on('click', function(e){
- e.preventDefault();
- //Dont let the click bubble up to the tr
- e.stopPropagation();
- var $this = $(this),
- c = confirm('Are you sure you want to delete this row?');
- if(!c) { return false;}
- $this.closest('tr').fadeOut(function(){ $(this).remove(); zebra();});
- });
- $('span.deleteall').on('click', function(e){
- e.preventDefault();
- var $this = $(this),
- $trows = $this.closest('table').children('tbody').find('tr.selected'),
- sel = !!$trows.length;
- // Don't confirm delete if no rows selected.
- if(!sel){
- alert('No rows selected');
- return false;
- }
- var c = confirm('Are you sure you want to delete the slected rows?');
- if(!c) { return false; }
- $trows.fadeOut(function(){ $trows.remove(); zebra(); });
- });
- //would be better if zebra was done in pure css
- function zebra(){
- $(".zebra-style").find('tbody')
- .find('.odd').removeClass('odd').end()
- .find('tr:odd').addClass("odd");
- };
- });
- $(document).ready(function(){
- $('#formsubmit').click(function(){
- $.post("submit",
- {fname: $('#fname').val(), surname: $('#surname').val()},
- function(){;
- }
- );
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement