Macabree

biblioteka

Jun 4th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.58 KB | None | 0 0
  1. package biblioteka;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class Biblioteka
  11. {
  12.     public static final String DRIVER = "org.sqlite.JDBC";
  13.  
  14.     public static final String URL = "jdbc:sqlite:biblioteka.db";
  15.  
  16.     private Connection connection;
  17.     private Statement statement;
  18.  
  19.     public Biblioteka()
  20.     {
  21.         try
  22.         {
  23.             Class.forName(DRIVER);
  24.             connection = DriverManager.getConnection(URL);
  25.             statement = connection.createStatement();
  26.  
  27.         } catch (ClassNotFoundException e)
  28.         {
  29.             System.err.println("Nie udało się nawiązać połączenia z bazą danych !\n");
  30.         } catch (SQLException e)
  31.         {
  32.             System.err.println("Nie udało się załadowac sterownika JDBC !\n");
  33.         }
  34.         createTable();
  35.     }
  36.  
  37.     public void createTable()
  38.     {
  39.         String createCzytelnicy = "CREATE TABLE IF NOT EXISTS czytelnicy(id_czytelnika INTEGER PRIMARY KEY AUTOINCREMENT, imie VARCHAR(20), nazwisko VARCHAR(20), login VARCHAR(10))";
  40.         String createKsiazki = "CREATE TABLE IF NOT EXISTS ksiazki(id_ksiazki INTEGER PRIMARY KEY AUTOINCREMENT, tytul VARCHAR(20), autor VARCHAR(20), czyJest BOOLEAN)";
  41.         String createWypozyczenia = "CREATE TABLE IF NOT EXISTS wypozyczenia(id_wypozycz INTEGER PRIMARY KEY AUTOINCREMENT, id_czytelnika INTEGER, id_ksiazki INTEGER)";
  42.         try
  43.         {
  44.             statement.execute(createCzytelnicy);
  45.             statement.execute(createKsiazki);
  46.             statement.execute(createWypozyczenia);
  47.             statement.close();
  48.         } catch (SQLException e)
  49.         {
  50.             System.err.println("Błąd przy tworzeniu tabel !\n");
  51.         }
  52.     }
  53.  
  54.     public void insertCzytelnik(String imie, String nazwisko, String login)
  55.     {
  56.         try
  57.         {
  58.             PreparedStatement preparedStatement = connection
  59.                     .prepareStatement("INSERT INTO czytelnicy VALUES(NULL, ?, ?, ?)");
  60.             preparedStatement.setString(1, imie);
  61.             preparedStatement.setString(2, nazwisko);
  62.             preparedStatement.setString(3, login);
  63.             preparedStatement.execute();
  64.             preparedStatement.close();
  65.             System.out.println("\nCzytelnik dodany !\n");
  66.         } catch (SQLException e)
  67.         {
  68.             System.err.println(e);
  69.         }
  70.     }
  71.  
  72.     public void insertKsiazka(String tytul, String autor)
  73.     {
  74.         try
  75.         {
  76.             PreparedStatement preparedStatement = connection
  77.                     .prepareStatement("INSERT INTO ksiazki VALUES(NULL, ?, ?, true)");
  78.             preparedStatement.setString(1, tytul);
  79.             preparedStatement.setString(2, autor);
  80.             preparedStatement.execute();
  81.             preparedStatement.close();
  82.             System.out.println("\nKsiążka dodana !\n");
  83.         } catch (SQLException e)
  84.         {
  85.             System.err.println("Błąd przy wstawianiu książki !\n");
  86.         }
  87.     }
  88.  
  89.     public boolean księgozbiór()
  90.     {
  91.         try
  92.         {
  93.             ResultSet resultset = statement
  94.                     .executeQuery("SELECT id_ksiazki, tytul, autor FROM ksiazki WHERE czyJest=TRUE");
  95.             if (resultset.next() == false)
  96.             {
  97.                 System.out.println("Brak książek w księgozbiorze !\n");
  98.                 return false;
  99.             }
  100.             while (resultset.next())
  101.             {
  102.                 System.out.print("ID = " + resultset.getInt("id_ksiazki") + "\n" + "Tytul = "
  103.                         + resultset.getString("tytul") + " \n" + "Autor = " + resultset.getString("autor"));
  104.                 System.out.println("\n");
  105.             }
  106.             System.out.println();
  107.             resultset.close();
  108.             return true;
  109.         } catch (SQLException e)
  110.         {
  111.             System.err.println("Problem z wyświetleniem danych !\n");
  112.             return false;
  113.         }
  114.     }
  115.  
  116.     public void listaCzytelnikow()
  117.     {
  118.         try
  119.         {
  120.             ResultSet resultset = statement.executeQuery("SELECT * FROM czytelnicy");
  121.             if (resultset.next() == false)
  122.             {
  123.                 System.out.println("Brak czytelników !\n");
  124.                 // return false;
  125.             }
  126.             while (resultset.next())
  127.             {
  128.                 System.out.print("ID = " + resultset.getInt("id_czytelnika") + "\n" + "Imię = "
  129.                         + resultset.getString("imie") + " \n" + "Nazwisko = " + resultset.getString("nazwisko") + " \n"
  130.                         + "Login = " + resultset.getString("login"));
  131.                 System.out.println("\n");
  132.             }
  133.             resultset.close();
  134.             // return true;
  135.         } catch (SQLException e)
  136.         {
  137.             System.err.println("Problem z wyświetleniem danych !\n");
  138.             // return false;
  139.         }
  140.     }
  141.  
  142.     public boolean listaWypozyczonychKsiazek()
  143.     {
  144.         try
  145.         {
  146.             ResultSet resultSet = statement
  147.                     .executeQuery("SELECT id_ksiazki, tytul, autor FROM ksiazki WHERE czyJest = false");
  148.             if (resultSet.next() == false)
  149.             {
  150.                 System.out.println("Brak wypożyczonych książek !");
  151.                 return false;
  152.             }
  153.             while (resultSet.next())
  154.             {
  155.                 System.out.println(resultSet.getInt("id_ksiazki") + " " + resultSet.getString("tytul") + " - "
  156.                         + resultSet.getString("autor"));
  157.             }
  158.             System.out.println();
  159.             resultSet.close();
  160.             return true;
  161.         } catch (SQLException e)
  162.         {
  163.             System.err.println("Błąd z wyświetleniem wypożyczonych książek!\n");
  164.             return false;
  165.         }
  166.     }
  167.  
  168.     public void wypozyczKsiazke(int idczytelnika, int idksiazki)
  169.     {
  170.         try
  171.         {
  172.             PreparedStatement preparedStatement = connection
  173.                     .prepareStatement("UPDATE ksiazki set czyJest = ? where id_ksiazki = ?");
  174.             preparedStatement.setBoolean(1, false);
  175.             preparedStatement.setInt(2, idksiazki);
  176.             preparedStatement.execute();
  177.             preparedStatement.close();
  178.             System.out.println("Miłego czytania !\n");
  179.         } catch (SQLException e)
  180.         {
  181.             System.err.println("Wystąpił problem z operacją wypożyczenia !\n");
  182.             System.err.println(e);
  183.         }
  184.  
  185.     }
  186.  
  187.     public void oddajKsiazke(int idksiazki)
  188.     {
  189.         try
  190.         {
  191.             PreparedStatement operation1 = connection
  192.                     .prepareStatement("UPDATE ksiazki SET czyJest = ? WHERE id_ksiazki= ?");
  193.             operation1.setBoolean(1, true);
  194.             operation1.setInt(2, idksiazki);
  195.             operation1.execute();
  196.             operation1.close();
  197.             PreparedStatement operation2 = connection.prepareStatement("DELETE from wypozyczenia where id_ksiazki = ?");
  198.             operation2.setInt(1, idksiazki);
  199.             operation2.execute();
  200.             operation2.close();
  201.             System.out.println("Dziękujemy za oddanie książki !");
  202.         } catch (SQLException e)
  203.         {
  204.             System.err.println("Wystąpił problem z operacją oddawania !\n");
  205.         }
  206.     }
  207.  
  208.     public void usunTabele()
  209.     {
  210.         try
  211.         {
  212.             String usunCzytelnikow = "DROP TABLE czytelnicy";
  213.             String usunKsiazki = "DROP TABLE ksiazki";
  214.             statement.execute(usunCzytelnikow);
  215.             statement.execute(usunKsiazki);
  216.  
  217.             statement.close();
  218.             System.out.println("Operacja usuwania powiodła się!\n");
  219.         } catch (SQLException e)
  220.         {
  221.             System.err.println("Operacja usuwania nie powiodła się!\n");
  222.             System.err.println(e);
  223.         }
  224.     }
  225.  
  226.     public void closeConnection()
  227.     {
  228.         try
  229.         {
  230.             connection.close();
  231.         } catch (SQLException e)
  232.         {
  233.             System.err.println("Problem z zamknięciem połączenia !\n");
  234.         }
  235.     }
  236. }
Add Comment
Please, Sign In to add comment