Advertisement
Guest User

Untitled

a guest
Mar 28th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) {
  4.         Book book1 = new Book(1, "AAAA", "Sienkiewicz Maciej", "TELEFONIA");
  5.         Book book2 = new Book(2, "BBBB", "Matkowski Adam", "BIOLOGIA");
  6.         Book book3 = new Book(3, "CCCC", "Heft Paula", "FLORYSTYKA");
  7.         String tableName = "biblioteka";
  8.         Connection connection = connect(tableName);
  9.         String tablica = "ksiazki";
  10.         stworzTablice(connection, tablica);
  11.         dodajNowy(book1,tableName, tablica);
  12.         dodajNowy(book2,tableName, tablica);
  13.         dodajNowy(book3,tableName, tablica);
  14.         szukaj(tableName, tablica);
  15.     }
  16.  
  17.     public static Connection connect(String tableName) {
  18.         Connection connect = null;
  19.         try {
  20.             Class.forName("org.sqlite.JDBC");
  21.             connect = DriverManager.getConnection("jdbc:sqlite:" + tableName + ".db");
  22.             System.out.println("Polaczylismy sie z baza: " + tableName);
  23.         } catch (Exception e) {
  24.             System.out.println("Blad polaczenie");
  25.         }
  26.         return connect;
  27.     }
  28.  
  29.     public static void stworzTablice(Connection connection, String tablica) {
  30.         Statement statement = null;
  31.         try {
  32.             statement = connection.createStatement();
  33.             String tablicaSql = "CREATE TABLE "+tablica+
  34.                     " (ID INT PRIMARY KEY NOT NULL,"+
  35.                     " ISBN CHAR(50) NOT NULL, " +
  36.                     " TITLE TEXT, "+
  37.                     " AUTHOR TEXT)";
  38.             statement.executeUpdate(tablicaSql);
  39.             statement.close();
  40.             connection.close();
  41.         } catch (Exception e) {
  42.         }
  43.     }
  44.  
  45.  
  46.     public static  void dodajNowy(Book book, String table, String tablica){
  47.         Connection connect = null;
  48.         Statement statement = null;
  49.         try{
  50.             Class.forName("org.sqlite.JDBC");
  51.             connect = DriverManager.getConnection("jdbc:sqlite:"+table+".db");
  52.             statement = connect.createStatement();
  53.             System.out.println(book.getId());
  54.             String polecenie = "INSERT INTO "+tablica+" (ID, ISBN, AUTHOR, TITLE) VALUES ("+book.getId()+", '"+book.getIsbn()+"', \""+book.getAuthor()+"\", \""+book.getTitle()+"\")";
  55.             statement.executeUpdate(polecenie);
  56.             statement.close();
  57.             connect.close();
  58.             System.out.println("Polecenie wykonane!");
  59.  
  60.         }catch(Exception e){
  61.             e.printStackTrace();
  62.  
  63.         }
  64.     }
  65.  
  66.     public static void szukaj(String table, String tablica){
  67.         Connection connect = null;
  68.         Statement statement = null;
  69.         try{
  70.             Class.forName("org.sqlite.JDBC");
  71.             connect = DriverManager.getConnection("jdbc:sqlite:"+table+".db");
  72.             statement = connect.createStatement();
  73.             String polecenie = "SELECT * FROM "+tablica+ " WHERE id BETWEEN 2 AND 3";
  74.             ResultSet query = statement.executeQuery(polecenie);
  75.             System.out.println("Wyszukane elementy:");
  76.             while(query.next()){
  77.                     int id = query.getInt("ID");
  78.                 System.out.println("ID:  " +id);
  79.                 System.out.println("ISBN:   "+query.getString("ISBN"));
  80.                 System.out.println("AUTHOR:   "+query.getString("AUTHOR"));
  81.                 System.out.println("TITLE:   "+query.getString("TITLE"));
  82.                 System.out.println("=======================================================");
  83.             }
  84.             query.close();
  85.             statement.close();
  86.             connect.close();
  87.  
  88.         }catch(Exception e){
  89.             System.out.println("Napotkalismy problem!");
  90.             e.printStackTrace();
  91.         }
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement