Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- Book book1 = new Book(1, "AAAA", "Sienkiewicz Maciej", "TELEFONIA");
- Book book2 = new Book(2, "BBBB", "Matkowski Adam", "BIOLOGIA");
- Book book3 = new Book(3, "CCCC", "Heft Paula", "FLORYSTYKA");
- String tableName = "biblioteka";
- Connection connection = connect(tableName);
- String tablica = "ksiazki";
- stworzTablice(connection, tablica);
- dodajNowy(book1,tableName, tablica);
- dodajNowy(book2,tableName, tablica);
- dodajNowy(book3,tableName, tablica);
- szukaj(tableName, tablica);
- }
- public static Connection connect(String tableName) {
- Connection connect = null;
- try {
- Class.forName("org.sqlite.JDBC");
- connect = DriverManager.getConnection("jdbc:sqlite:" + tableName + ".db");
- System.out.println("Polaczylismy sie z baza: " + tableName);
- } catch (Exception e) {
- System.out.println("Blad polaczenie");
- }
- return connect;
- }
- public static void stworzTablice(Connection connection, String tablica) {
- Statement statement = null;
- try {
- statement = connection.createStatement();
- String tablicaSql = "CREATE TABLE "+tablica+
- " (ID INT PRIMARY KEY NOT NULL,"+
- " ISBN CHAR(50) NOT NULL, " +
- " TITLE TEXT, "+
- " AUTHOR TEXT)";
- statement.executeUpdate(tablicaSql);
- statement.close();
- connection.close();
- } catch (Exception e) {
- }
- }
- public static void dodajNowy(Book book, String table, String tablica){
- Connection connect = null;
- Statement statement = null;
- try{
- Class.forName("org.sqlite.JDBC");
- connect = DriverManager.getConnection("jdbc:sqlite:"+table+".db");
- statement = connect.createStatement();
- System.out.println(book.getId());
- String polecenie = "INSERT INTO "+tablica+" (ID, ISBN, AUTHOR, TITLE) VALUES ("+book.getId()+", '"+book.getIsbn()+"', \""+book.getAuthor()+"\", \""+book.getTitle()+"\")";
- statement.executeUpdate(polecenie);
- statement.close();
- connect.close();
- System.out.println("Polecenie wykonane!");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- public static void szukaj(String table, String tablica){
- Connection connect = null;
- Statement statement = null;
- try{
- Class.forName("org.sqlite.JDBC");
- connect = DriverManager.getConnection("jdbc:sqlite:"+table+".db");
- statement = connect.createStatement();
- String polecenie = "SELECT * FROM "+tablica+ " WHERE id BETWEEN 2 AND 3";
- ResultSet query = statement.executeQuery(polecenie);
- System.out.println("Wyszukane elementy:");
- while(query.next()){
- int id = query.getInt("ID");
- System.out.println("ID: " +id);
- System.out.println("ISBN: "+query.getString("ISBN"));
- System.out.println("AUTHOR: "+query.getString("AUTHOR"));
- System.out.println("TITLE: "+query.getString("TITLE"));
- System.out.println("=======================================================");
- }
- query.close();
- statement.close();
- connect.close();
- }catch(Exception e){
- System.out.println("Napotkalismy problem!");
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement