Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.29 KB | None | 0 0
  1. package controllers;
  2.  
  3. import org.springframework.web.servlet.ModelAndView;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9.  
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.ResponseBody;
  13. import org.springframework.web.bind.annotation.RequestParam;
  14. import org.springframework.web.bind.annotation.RequestMethod;
  15.  
  16. import hibernate.Movies;
  17.  
  18. @Controller
  19. public class SubmitDataController {
  20. private ModelAndView modelandview;
  21.  
  22.  
  23. @RequestMapping(value = "/submit", method = RequestMethod.POST)
  24. public @ResponseBody
  25. ModelAndView submitDeleteChanges(
  26. @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
  27. System.out.println("TESTING FORM");
  28. System.out.println(movies);
  29. return modelandview;
  30. }
  31.  
  32.  
  33. public ModelAndView titlePage() {
  34. return modelandview;
  35. }
  36.  
  37. public ModelAndView getModelandview() {
  38. return modelandview;
  39. }
  40.  
  41. public void setModelandview(ModelAndView modelandview) {
  42. this.modelandview = modelandview;
  43. }
  44.  
  45. }
  46.  
  47. package dao;
  48.  
  49. import org.hibernate.SessionFactory;
  50.  
  51. import org.hibernate.Transaction;
  52. import org.springframework.web.servlet.ModelAndView;
  53. import org.hibernate.HibernateException;
  54. import org.hibernate.Session;
  55. import org.hibernate.Query;
  56.  
  57. import hibernate.Books;
  58. import hibernate.Music;
  59. import hibernate.Contacts;
  60. import hibernate.Movies;
  61.  
  62. import java.util.List;
  63.  
  64. //DAO = data access object. This class is the interface between the code and the database.
  65. public class HibernateDataDAO {
  66. private SessionFactory sessionFactory;
  67. private Transaction transaction;
  68.  
  69. public void insertBooks(Books books) {
  70. Session session = sessionFactory.openSession();
  71.  
  72. try {
  73. transaction = session.beginTransaction();
  74.  
  75. session.save(books);
  76.  
  77. transaction.commit();
  78. }
  79. catch (Exception e) {
  80. if (transaction != null) transaction.rollback();
  81. throw e;
  82. }
  83. finally {
  84. session.close();
  85. }
  86. }
  87.  
  88. public void deleteBooks(Books books, int booksKey){
  89. Session session = sessionFactory.openSession();
  90.  
  91. try{
  92. transaction = session.beginTransaction();
  93.  
  94. Object persistentInstance = session.load(hibernate.Books.class, booksKey);
  95.  
  96. if (persistentInstance != null) {
  97. session.delete(persistentInstance);
  98. transaction.commit();
  99. }
  100. }catch(HibernateException e){
  101. if(transaction!= null) transaction.rollback();
  102. e.printStackTrace();
  103. }
  104. finally{
  105. session.close();
  106. }
  107. }
  108.  
  109. @SuppressWarnings("unchecked")
  110. public ModelAndView getBooks(ModelAndView modelandview){
  111. Session session = sessionFactory.openSession();
  112. List<Books> books = null;
  113.  
  114. try{
  115. transaction = session.beginTransaction();
  116.  
  117. Query query = session.createQuery("select books from Books as books");
  118.  
  119. books = (List<Books>) query.list();
  120. modelandview.addObject("books", books);
  121. System.out.println("TitletAuthort#");
  122.  
  123. for(Books book: books) {
  124. System.out.println(book.getTitle() + "t" + book.getAuthor() + "t" + book.getBooksKey());
  125. }
  126.  
  127. transaction.commit();
  128. }catch(HibernateException e){
  129. if(transaction!= null) transaction.rollback();
  130. e.printStackTrace();
  131. }
  132. finally{
  133. session.close();
  134. }
  135. return modelandview;
  136. }
  137.  
  138. public void insertMusic(Music music) {
  139. Session session = sessionFactory.openSession();
  140.  
  141. try {
  142. transaction = session.beginTransaction();
  143.  
  144. session.save(music);
  145.  
  146. transaction.commit();
  147. }
  148. catch (Exception e) {
  149. if (transaction != null) transaction.rollback();
  150. throw e;
  151. }
  152. finally {
  153. session.close();
  154. }
  155. }
  156.  
  157. public void deleteMusic(Music music, int musicKey){
  158. Session session = sessionFactory.openSession();
  159.  
  160. try{
  161. transaction = session.beginTransaction();
  162.  
  163. Object persistentInstance = session.load(hibernate.Music.class, musicKey);
  164.  
  165. if (persistentInstance != null) {
  166. session.delete(persistentInstance);
  167. transaction.commit();
  168. }
  169. }catch(HibernateException e){
  170. if(transaction!= null) transaction.rollback();
  171. e.printStackTrace();
  172. }
  173. finally{
  174. session.close();
  175. }
  176. }
  177.  
  178. @SuppressWarnings("unchecked")
  179. public ModelAndView getMusic(ModelAndView modelandview){
  180. Session session = sessionFactory.openSession();
  181. List<Music> music = null;
  182.  
  183. try{
  184. transaction = session.beginTransaction();
  185.  
  186. Query query = session.createQuery("select music from Music as music");
  187.  
  188. music = (List<Music>) query.list();
  189. modelandview.addObject("music", music);
  190. System.out.println("TitletArtistt#");
  191.  
  192. for(Music musics: music) {
  193. System.out.println(musics.getTitle() + "t" + musics.getArtist() + "t" + musics.getMusicKey());
  194. }
  195.  
  196. transaction.commit();
  197. }catch(HibernateException e){
  198. if(transaction!= null) transaction.rollback();
  199. e.printStackTrace();
  200. }
  201. finally{
  202. session.close();
  203. }
  204. return modelandview;
  205. }
  206.  
  207.  
  208. public void insertMovies(Movies movies) {
  209. Session session = sessionFactory.openSession();
  210.  
  211. try {
  212. transaction = session.beginTransaction();
  213.  
  214. session.save(movies);
  215.  
  216. transaction.commit();
  217. }
  218. catch (Exception e) {
  219. if (transaction != null) transaction.rollback();
  220. throw e;
  221. }
  222. finally {
  223. session.close();
  224. }
  225. }
  226.  
  227. public void deleteMovies(Movies movies, int movieKey){
  228. Session session = sessionFactory.openSession();
  229.  
  230. try{
  231. transaction = session.beginTransaction();
  232.  
  233. Object persistentInstance = session.load(hibernate.Movies.class, movieKey);
  234.  
  235. if (persistentInstance != null) {
  236. session.delete(persistentInstance);
  237. transaction.commit();
  238. }
  239. }catch(HibernateException e){
  240. if(transaction!= null) transaction.rollback();
  241. e.printStackTrace();
  242. }
  243. finally{
  244. session.close();
  245. }
  246. }
  247.  
  248. @SuppressWarnings("unchecked")
  249. public ModelAndView getMovies(ModelAndView modelandview){
  250. Session session = sessionFactory.openSession();
  251. List<Movies> movies = null;
  252.  
  253. try{
  254. transaction = session.beginTransaction();
  255.  
  256. Query query = session.createQuery("select movies from Movies as movies");
  257.  
  258. movies = (List<Movies>) query.list();
  259. modelandview.addObject("movies", movies);
  260. System.out.println("TitletRatingt#");
  261.  
  262. for(Movies movie: movies) {
  263. System.out.println(movie.getTitle() + "t" + movie.getRating() + "t" + movie.getMovieKey());
  264. }
  265.  
  266. transaction.commit();
  267. }catch(HibernateException e){
  268. if(transaction!= null) transaction.rollback();
  269. e.printStackTrace();
  270. }
  271. finally{
  272. session.close();
  273. }
  274. return modelandview;
  275. }
  276.  
  277. public void insertContacts(Contacts contacts) {
  278. Session session = sessionFactory.openSession();
  279.  
  280. try {
  281. transaction = session.beginTransaction();
  282.  
  283. session.save(contacts);
  284.  
  285. transaction.commit();
  286. }
  287. catch (Exception e) {
  288. if (transaction != null) transaction.rollback();
  289. throw e;
  290. }
  291. finally {
  292. session.close();
  293. }
  294. }
  295.  
  296. public void deleteContacts(Contacts contacts, int contactKey){
  297. Session session = sessionFactory.openSession();
  298.  
  299. try{
  300. transaction = session.beginTransaction();
  301.  
  302. Object persistentInstance = session.load(hibernate.Contacts.class, contactKey);
  303.  
  304. if (persistentInstance != null) {
  305. session.delete(persistentInstance);
  306. transaction.commit();
  307. }
  308. }catch(HibernateException e){
  309. if(transaction!= null) transaction.rollback();
  310. e.printStackTrace();
  311. }
  312. finally{
  313. session.close();
  314. }
  315. }
  316.  
  317. @SuppressWarnings("unchecked")
  318. public ModelAndView getContacts(ModelAndView modelandview){
  319. Session session = sessionFactory.openSession();
  320. List<Contacts> contacts = null;
  321.  
  322. try{
  323. transaction = session.beginTransaction();
  324.  
  325. Query query = session.createQuery("select contacts from Contacts as contacts");
  326.  
  327. contacts = (List<Contacts>) query.list();
  328. modelandview.addObject("contacts", contacts);
  329. System.out.println("NametEmailtPhonet#");
  330.  
  331. for(Contacts contact: contacts) {
  332. System.out.println(contact.getName() + "t" + contact.getEmail() + "t" + contact.getPhone() + contact.getContactKey());
  333. }
  334.  
  335. transaction.commit();
  336.  
  337. }catch(HibernateException e){
  338. if(transaction!= null) transaction.rollback();
  339. e.printStackTrace();
  340. }
  341. finally{
  342. session.close();
  343. }
  344. return modelandview;
  345. }
  346.  
  347. public SessionFactory getSessionFactory() {
  348. return sessionFactory;
  349. }
  350.  
  351. public void setSessionFactory(SessionFactory sessionFactory) {
  352. this.sessionFactory = sessionFactory;
  353. }
  354. }
  355.  
  356. <beans xmlns="http://www.springframework.org/schema/beans"
  357. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  358. xmlns:mvc="http://www.springframework.org/schema/mvc"
  359. xmlns:context="http://www.springframework.org/schema/context"
  360. xsi:schemaLocation="http://www.springframework.org/schema/beans
  361. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  362. http://www.springframework.org/schema/mvc
  363. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  364. http://www.springframework.org/schema/context
  365. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  366.  
  367. <mvc:annotation-driven />
  368. <context:component-scan base-package="controllers" />
  369.  
  370. <mvc:resources mapping="/resources/**" location="/resources/" />
  371. <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
  372. <!-- This viewResolver is looking in the directory listed below. ModelAndView is integrated into spring and automatically looks for viewResolver -->
  373. <bean id="viewResolver"
  374. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  375. <property name="prefix">
  376. <value>/WEB-INF/pages/</value>
  377. </property>
  378. <property name="suffix">
  379. <value>.jsp</value>
  380. </property>
  381. </bean>
  382. <!-- 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 -->
  383. <!-- All the red text in console, you can actually see the beans being injected -->
  384. <bean id="indexController" class="controllers.IndexController">
  385. <property name="modelandview" value="index"/>
  386. </bean>
  387.  
  388. <bean id="testController" class="controllers.TestController">
  389. <property name="modelandview" value="test"/>
  390. <property name="testModel" ref="testModel"/>
  391. </bean>
  392.  
  393. <bean id="entertainmentModel" class="models.EntertainmentModel">
  394. <property name="data" ref="hibernateDataDao"/>
  395. </bean>
  396.  
  397. <bean id="testModel" class="models.TestModel">
  398. <property name="data" ref="hibernateDataDao"/>
  399. </bean>
  400.  
  401. <bean id="hibernateDataDao" class="dao.HibernateDataDAO">
  402. <property name="sessionFactory" ref="sessionFactory"/>
  403. </bean>
  404.  
  405. <bean id="entertainmentController" class="controllers.EntertainmentController">
  406. <property name="bookModel" value="books"/>
  407. <property name="movieModel" value="movies"/>
  408. <property name="musicModel" value="music"/>
  409. <property name="contactsModel" value="contacts"/>
  410. <property name="model" ref="entertainmentModel"/>
  411. </bean>
  412.  
  413. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  414. destroy-method="close">
  415. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  416. <property name="url" value="jdbc:mysql://localhost:3306/databaseproject" />
  417. <property name="username" value="root" />
  418. <property name="password" value="Someone1" />
  419. </bean>
  420.  
  421. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  422. <property name="dataSource" ref="dataSource"/>
  423. <property name="mappingResources">
  424. <list>
  425. <value>HibernateData.hbm.xml</value>
  426. </list>
  427. </property>
  428. <property name="hibernateProperties">
  429. <value>
  430. hibernate.dialect=org.hibernate.dialect.MySQLDialect
  431. hibernate.default_schema=databaseproject
  432. </value>
  433. </property>
  434. </bean>
  435. </beans>
  436.  
  437. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
  438. <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
  439. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  440. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 5 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  441.  
  442. <html>
  443. <head>
  444. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  445. <title>Database Project</title>
  446.  
  447. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  448.  
  449. <spring:url value="/webjars/bootstrap/3.2.0/css/bootstrap.min.css" var="bootstrap"/>
  450. <spring:url value="/webjars/jquery/1.11.1/jquery.min.js" var="jquery"/>
  451. <spring:url value="/webjars/bootstrap/3.2.0/js/bootstrap.min.js" var="bootstrapJS"/>
  452. <spring:url value="/webjars/bootstrap-table/1.9.1/dist/bootstrap-table.min.css" var="bootstrapTable"/>
  453. <spring:url value="/webjars/bootstrap-table/1.9.1/dist/bootstrap-table.min.js" var="bootstrapTableJS"/>
  454. <spring:url value="/resources/css/entertainment.css" var="entertainmentCss"/>
  455. <spring:url value="/resources/js/script.js" var="tableJS"/>
  456.  
  457. <link href="${bootstrap}" rel="stylesheet"/>
  458. <link href="${bootstrapTable}" rel="stylesheet"/>
  459. <link href="${entertainmentCss}" rel="stylesheet"/>
  460. </head>
  461.  
  462. <body>
  463. <div class="site-wrapper">
  464. <div class="site-wrapper-inner">
  465. <div class="cover-container">
  466. <div class="masthead clearfix">
  467. <div class="inner">
  468. <h3 class="masthead-brand">Entertainment App</h3>
  469. <nav>
  470. <ul class="nav masthead-nav">
  471. <li class="active"><a href="#">Home</a></li>
  472. </ul>
  473. </nav>
  474. </div>
  475. </div>
  476. <div class="inner cover">
  477. <div class="entertainmentDisplay">
  478. <h1>Movies</h1>
  479. <table class="zebra-style" id="dtable">
  480. <thead>
  481. <tr>
  482. <th scope="col">
  483. <input type="checkbox" class="checkall" />
  484. </th>
  485. <th scope="col">Title</th>
  486. <th scope="col">Rating</th>
  487. <th scope="col">#</th>
  488. </tr>
  489. </thead>
  490. <tbody>
  491. <c:forEach var="movies" items="${movies}">
  492. <tr>
  493. <td> <input type="checkbox" class="checkbox" /></td>
  494. <td>${movies.title} </td>
  495. <td>${movies.rating}</td>
  496. <td>${movies.movieKey}</td>
  497. </tr>
  498. </c:forEach>
  499. <tfoot>
  500. <tr>
  501. <!--<th colspan="2"><a href="javascript:;" class="btn btn-default deleteall" title="dtable">Delete Selected Items</a></th>-->
  502. <th colspan="2"><span href="javascript:;" id= "formsubmit" class="btn btn-default deleteall" title="dtable">Delete Selected Items</span></th>
  503. <th colspan="2"><a href="#" class="btn btn-default">Add Item</a></th>
  504. <div>
  505.  
  506. </div>
  507. </tr>
  508. </tfoot>
  509. </tbody>
  510. </table>
  511. </div>
  512. </div>
  513. <div class="mastfoot">
  514. <div class="inner">
  515. <p><span class="bottomColor">Created by <a href="http://www.w.com">W</a> & <a href="http://www.s.com">S</a></span></p>
  516. </div>
  517. </div>
  518. </div>
  519. </div>
  520. </div>
  521.  
  522. <script src="${jquery}"></script>
  523. <script src="${bootstrapJS}"></script>
  524. <script src="${bootstrapTableJS}"></script>
  525. <script src="${tableJS}"></script>
  526. </body>
  527. </html>
  528.  
  529. <?xml version="1.0" encoding="utf-8"?>
  530. <!DOCTYPE hibernate-mapping PUBLIC
  531. "-//Hibernate/Hibernate Mapping DTD//EN"
  532. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  533.  
  534. <hibernate-mapping>
  535. <class name="hibernate.Books" table="books">
  536. <id name="booksKey" column="booksKey" type="int">
  537. <generator class="increment"/>
  538. </id>
  539. <property name="title" column="title" type="string"/>
  540. <property name="author" column="author" type="string"/>
  541. </class>
  542.  
  543. <class name="hibernate.Movies" table="movies">
  544. <id name="movieKey" column="movieKey" type="int">
  545. <generator class="increment"/>
  546. </id>
  547. <property name="title" column="title" type="string"/>
  548. <property name="rating" column="rating" type="string"/>
  549. </class>
  550.  
  551. <class name="hibernate.Music" table="music">
  552. <id name="musicKey" column="musicKey" type="int">
  553. <generator class="increment"/>
  554. </id>
  555. <property name="title" column="title" type="string"/>
  556. <property name="artist" column="artist" type="string"/>
  557. </class>
  558.  
  559. <class name="hibernate.Contacts" table="contacts">
  560. <id name="contactKey" column="contactKey" type="int">
  561. <generator class="increment"/>
  562. </id>
  563. <property name="name" column="name" type="string"/>
  564. <property name="email" column="email" type="string"/>
  565. <property name="phone" column="phone" type="string"/>
  566. </class>
  567. </hibernate-mapping>
  568.  
  569. $(function () {
  570. var $result = $('#eventsResult');
  571.  
  572. $('#eventsTable').on('all.bs.table', function (e, name, args) {
  573. console.log('Event:', name, ', data:', args);
  574. })
  575. .on('click-row.bs.table', function (e, row, $element) {
  576. $result.text('Event: click-row.bs.table');
  577. })
  578. .on('dbl-click-row.bs.table', function (e, row, $element) {
  579. $result.text('Event: dbl-click-row.bs.table');
  580. })
  581. .on('sort.bs.table', function (e, name, order) {
  582. $result.text('Event: sort.bs.table');
  583. })
  584. .on('check.bs.table', function (e, row) {
  585. $result.text('Event: check.bs.table');
  586. })
  587. .on('uncheck.bs.table', function (e, row) {
  588. $result.text('Event: uncheck.bs.table');
  589. })
  590. .on('check-all.bs.table', function (e) {
  591. $result.text('Event: check-all.bs.table');
  592. })
  593. .on('uncheck-all.bs.table', function (e) {
  594. $result.text('Event: uncheck-all.bs.table');
  595. })
  596. .on('load-success.bs.table', function (e, data) {
  597. $result.text('Event: load-success.bs.table');
  598. })
  599. .on('load-error.bs.table', function (e, status) {
  600. $result.text('Event: load-error.bs.table');
  601. })
  602. .on('column-switch.bs.table', function (e, field, checked) {
  603. $result.text('Event: column-switch.bs.table');
  604. })
  605. .on('page-change.bs.table', function (e, number, size) {
  606. $result.text('Event: page-change.bs.table');
  607. })
  608. .on('search.bs.table', function (e, text) {
  609. $result.text('Event: search.bs.table');
  610. });
  611. });
  612.  
  613. $(document).ready(function () {
  614. zebra();
  615.  
  616. $('.checkall').on('click', function () {
  617. var $this = $(this),
  618. // Test to see if it is checked
  619. checked = $this.prop('checked'),
  620. //Find all the checkboxes
  621. cbs = $this.closest('table').children('tbody').find('.checkbox');
  622. // Check or Uncheck them.
  623. cbs.prop('checked', checked);
  624. //toggle the selected class to all the trs
  625. cbs.closest('tr').toggleClass('selected', checked);
  626. });
  627. $('tbody tr').on('click', function () {
  628. var $this = $(this).toggleClass('selected');
  629. $this.find('.checkbox').prop('checked', $this.hasClass('selected'));
  630. if(!$this.hasClass('selected')) {
  631. $this.closest('table').children('thead').find('.checkall').prop('checked', false);
  632. }
  633. });
  634. $('.delete_single').on('click', function(e){
  635. e.preventDefault();
  636. //Dont let the click bubble up to the tr
  637. e.stopPropagation();
  638. var $this = $(this),
  639. c = confirm('Are you sure you want to delete this row?');
  640. if(!c) { return false;}
  641. $this.closest('tr').fadeOut(function(){ $(this).remove(); zebra();});
  642. });
  643. $('span.deleteall').on('click', function(e){
  644. e.preventDefault();
  645. var $this = $(this),
  646. $trows = $this.closest('table').children('tbody').find('tr.selected'),
  647. sel = !!$trows.length;
  648.  
  649. // Don't confirm delete if no rows selected.
  650. if(!sel){
  651. alert('No rows selected');
  652. return false;
  653. }
  654. var c = confirm('Are you sure you want to delete the slected rows?');
  655. if(!c) { return false; }
  656. $trows.fadeOut(function(){ $trows.remove(); zebra(); });
  657. });
  658.  
  659.  
  660. //would be better if zebra was done in pure css
  661. function zebra(){
  662. $(".zebra-style").find('tbody')
  663. .find('.odd').removeClass('odd').end()
  664. .find('tr:odd').addClass("odd");
  665. };
  666. });
  667.  
  668. $(document).ready(function(){
  669. $('#formsubmit').click(function(){
  670.  
  671. $.post("submit",
  672. {fname: $('#fname').val(), surname: $('#surname').val()},
  673. function(){;
  674.  
  675. }
  676. );
  677. });
  678. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement