Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.66 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package librarymanagementsys;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.util.ArrayList;
  13. import librarymanagementsys.Book;
  14. import java.sql.Date;
  15.  
  16. public class DbOperations {
  17.  
  18.     /*String url = "jdbc:mysql://localhost:3306/library";
  19.     String userName = "root";
  20.     String password = "";
  21.     Connection con = null;
  22.     PreparedStatement pst = null;
  23.    
  24.     */
  25.    
  26.     private static String url = "jdbc:mysql://localhost:3306/library";
  27.     private static String driverName = "org.sqlite.JDBC";
  28.     private static String username = "root";
  29.     private static String password = "";
  30.     private static Connection con;
  31.     PreparedStatement pst = null;
  32.     ResultSet rs;
  33.  
  34.     public boolean addBooks(Book bk) {
  35.  
  36.          try {
  37.             Class.forName(driverName);
  38.            
  39.                con = DriverManager.getConnection(url, username, password);
  40.                String qurrey = "INSERT INTO books(BookName,AuthorName,ISBNnumber,Length,isCheckedOut,CheckedOutDate,DueDate,LoanedTo) VALUES(?,?,?,?,?,?,?,?)";
  41.                pst = con.prepareStatement(qurrey);
  42.  
  43.           //  pst.setInt(1, 0);
  44.             pst.setString(1, bk.getItemName());
  45.             pst.setString(2, bk.getAuthorName());
  46.             pst.setString(3, bk.getISBNnumber());
  47.             pst.setInt(4, bk.getLoanPeriodLength());
  48.             pst.setBoolean(5, false);
  49.  
  50.             // java.sql.Date date = new java.sql.Date(0);
  51.             pst.setNull(6, java.sql.Types.DATE);
  52.             pst.setNull(7, java.sql.Types.DATE);
  53.             pst.setString(8, "");
  54.  
  55.             //  pst.setDate(7,date);                                  // pst.setDate(7,new java.sql.Date(bk.getCheckOutDate().getTime()));
  56.             // pst.setDate(8,date);                                    // pst.setDate(8,new java.sql.Date(bk.getDueDate().getTime()));
  57.             //  pst.setString(9,"Kasun");                                    // pst.setString(9, bk.getLoanedTo());
  58.             pst.executeUpdate();
  59.             return true;
  60.         } catch (Exception e) {
  61.             System.out.print(e);
  62.             return false;
  63.         } finally {
  64.             try {
  65.                 if (pst != null) {
  66.                     pst.close();
  67.                 }
  68.                 if (con != null) {
  69.                     con.close();
  70.                 }
  71.             } catch (Exception e) {
  72.                 System.out.print(e);
  73.             }
  74.         }
  75.     }
  76.  
  77.     public ArrayList<Book> getBooks() {
  78.  
  79.         try {
  80.             //  System.out.println("getBooks 1");
  81.             ArrayList<Book> bkList = new ArrayList<Book>();
  82.  
  83.             con = DriverManager.getConnection("jdbc:sqlite:library.sqlite");
  84.             String qurrey = "SELECT * FROM books";
  85.             pst = con.prepareStatement(qurrey);
  86.             // System.out.println("getBooks 2");
  87.             rs = pst.executeQuery();
  88.             System.out.println("getBooks 3");
  89.             while (rs.next()) {
  90.                 Book bk = new Book();
  91.                 bk.setLibID(rs.getInt(1));
  92.                 bk.setItemName(rs.getString(2));
  93.                 bk.setAuthorName(rs.getString(3));
  94.  
  95.                 bk.setCheckedOut(rs.getBoolean(6));
  96.                 System.out.println(rs.getBoolean(6));
  97.  
  98.                 bk.setDueDate(rs.getDate(8));
  99.  
  100.                 bkList.add(bk);
  101.                 // System.out.println("getBooks loop");
  102.  
  103.             }
  104.             System.out.println("getBooks 4");
  105.             return bkList;
  106.  
  107.         } catch (Exception e) {
  108.             System.out.print(e);
  109.             // System.out.println("getBooks exception 1");
  110.             return null;
  111.  
  112.         } finally {
  113.             try {
  114.                 if (pst != null) {
  115.                     pst.close();
  116.                 }
  117.                 if (con != null) {
  118.                     con.close();
  119.                 }
  120.                 //return null;
  121.                 // System.out.println("getBooks final");
  122.             } catch (Exception e) {
  123.                 System.out.print(e);
  124.                 // System.out.println("getBooks exception 2");
  125.                 // return null;
  126.             }
  127.         }
  128.  
  129.     }
  130.  
  131.     public void updateCheckOut(Book bk) {
  132.         System.out.println("updateCheckOut 1");
  133.         java.sql.Date cksqldae = new java.sql.Date(bk.getCheckOutDate().getTime());
  134.         java.sql.Date Dusqldae = new java.sql.Date(bk.getDueDate().getTime());
  135.         System.out.println("updateCheckOut 2");
  136.         try {
  137.             con = DriverManager.getConnection("jdbc:sqlite:library.sqlite");
  138.             System.out.println("updateCheckOut befor Qurrey");
  139.  
  140.             int temp;
  141.             if (bk.isCheckedOut()) {
  142.                 temp = 1;
  143.             } else {
  144.                 temp = 0;
  145.             }
  146.  
  147.             String qurrey = "UPDATE books SET BookName ='" + bk.getItemName() + "', AuthorName='" + bk.getAuthorName()
  148.                     + "', isCheckedOut='" + temp + "', CheckedOutDate= '" + cksqldae + "', DueDate= '" + Dusqldae + "', LoanedTo = '" + bk.getLoanedTo()
  149.                     + "' WHERE BookID='" + bk.getLibID() + "'";
  150.  
  151.             pst = con.prepareStatement(qurrey);
  152.  
  153.             pst.executeUpdate();
  154.  
  155.             System.out.println("updateCheckOut after execution");
  156.  
  157.         } catch (Exception e) {
  158.             System.out.print(e);
  159.  
  160.         } finally {
  161.             try {
  162.                 if (pst != null) {
  163.                     pst.close();
  164.                 }
  165.                 if (con != null) {
  166.                     con.close();
  167.                 }
  168.             } catch (Exception e) {
  169.                 System.out.print(e);
  170.             }
  171.         }
  172.  
  173.     }
  174.  
  175.     public int getSelectBook(int ID) {
  176.  
  177.         try {
  178.             System.out.println("getSelectBook 1");
  179.             ArrayList<Book> bkList = new ArrayList<Book>();
  180.  
  181.            con = DriverManager.getConnection("jdbc:sqlite:library.sqlite");
  182.             String qurrey = "SELECT * FROM books WHERE BookID ='" + ID + "'";
  183.             pst = con.prepareStatement(qurrey);
  184.             System.out.println("getSelectBook 2");
  185.             rs = pst.executeQuery();
  186.             System.out.println("getBooks 3");
  187.  
  188.             while (rs.next()) {
  189.                 Book bk = new Book();
  190.                 /*bk.setLibID(rs.getInt(1));
  191.                 bk.setItemName(rs.getString(2));
  192.                 bk.setAuthorName(rs.getString(3));
  193.                 bk.setCheckedOut(rs.getBoolean(6));
  194.                 System.out.println(rs.getBoolean(6));
  195.                 bk.setDueDate(rs.getDate(8));
  196.                  */
  197.  
  198.                 bk.setLoanPeriodLength(rs.getInt(5));
  199.                
  200.                 bkList.add(bk);
  201.                 System.out.println("getSelectBook loop");
  202.  
  203.             }
  204.             int temp = bkList.get(0).getLoanPeriodLength();
  205.             return temp;
  206.         } catch (Exception e) {
  207.             System.out.print(e);
  208.             System.out.println("getSelectBook exception 1");
  209.             return 124563;
  210.  
  211.         } finally {
  212.             try {
  213.                 if (pst != null) {
  214.                     pst.close();
  215.                 }
  216.                 if (con != null) {
  217.                     con.close();
  218.                 }
  219.                 //return null;
  220.                 System.out.println("getSelectBook final");
  221.             } catch (Exception e) {
  222.                 System.out.print(e);
  223.                 System.out.println("getSelectBook exception 2");
  224.                 // return null;
  225.             }
  226.         }
  227.  
  228.     }
  229.  
  230.     public void returnBook(Book bk) {
  231.  
  232.         try {
  233.             con = DriverManager.getConnection("jdbc:sqlite:library.sqlite");
  234.            
  235.            java.sql.Date sqldate = null;
  236.            
  237.              String qurrey = "UPDATE books SET BookName ='" + bk.getItemName() + "', AuthorName='" + bk.getAuthorName()
  238.                     + "', isCheckedOut='" + 0 + "', CheckedOutDate= ?, DueDate= ?, LoanedTo = '' WHERE BookID='" + bk.getLibID() + "'";
  239.              pst = con.prepareStatement(qurrey);
  240.              
  241.              pst.setNull(1, java.sql.Types.DATE);
  242.              pst.setNull(2, java.sql.Types.DATE);
  243.              
  244.              
  245.            
  246.              pst.executeUpdate();
  247.  
  248.            
  249.            
  250.  
  251.         } catch (Exception e) {
  252.             System.out.print(e);
  253.            
  254.         } finally {
  255.             try {
  256.                 if (pst != null) {
  257.                     pst.close();
  258.                 }
  259.                 if (con != null) {
  260.                     con.close();
  261.                 }
  262.             } catch (Exception e) {
  263.                 System.out.print(e);
  264.             }
  265.         }
  266.  
  267.     }
  268.    
  269.     public int checkRefarance(Book bkrefarance) {
  270.  
  271.         try {
  272.            
  273.             int ID = bkrefarance.getLibID();
  274.             System.out.println("getSelectBook 1");
  275.             ArrayList<Book> bkList = new ArrayList<Book>();
  276.  
  277.             con = DriverManager.getConnection("jdbc:sqlite:library.sqlite");
  278.             String qurrey = "SELECT * FROM books WHERE BookID ='" + ID + "'";
  279.             pst = con.prepareStatement(qurrey);
  280.             System.out.println("getSelectBook 2");
  281.             rs = pst.executeQuery();
  282.             System.out.println("getBooks 3");
  283.  
  284.             while (rs.next()) {
  285.                 Book bk = new Book();
  286.                 /*bk.setLibID(rs.getInt(1));
  287.                 bk.setItemName(rs.getString(2));
  288.                 bk.setAuthorName(rs.getString(3));
  289.                 bk.setCheckedOut(rs.getBoolean(6));
  290.                 System.out.println(rs.getBoolean(6));
  291.                 bk.setDueDate(rs.getDate(8));
  292.                  */
  293.  
  294.                 bk.setLoanPeriodLength(rs.getInt(5));
  295.                
  296.                 bkList.add(bk);
  297.                 System.out.println("getSelectBook loop");
  298.  
  299.             }
  300.             int temp = bkList.get(0).getLoanPeriodLength();
  301.             return temp;
  302.         } catch (Exception e) {
  303.             System.out.print(e);
  304.             System.out.println("getSelectBook exception 1");
  305.             return 124563;
  306.  
  307.         } finally {
  308.             try {
  309.                 if (pst != null) {
  310.                     pst.close();
  311.                 }
  312.                 if (con != null) {
  313.                     con.close();
  314.                 }
  315.                 //return null;
  316.                 System.out.println("getSelectBook final");
  317.             } catch (Exception e) {
  318.                 System.out.print(e);
  319.                 System.out.println("getSelectBook exception 2");
  320.                 // return null;
  321.             }
  322.         }
  323.  
  324.     }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement