Advertisement
Guest User

Untitled

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