简体   繁体   中英

Java interface implemantation in client

Hi im trying to do bookstore simple app from book Vishal Layka Learn Java for Web Development. I have a problem in client of data access layer BookAPP.java which should use BookDAO interface. I have included BookAPP, BookDAO, BookDAOImpl codes.

Errors: cannot-make-a-static-reference-to-the-non-static-method from the type bookDAO List books = BookDAO.findAllBooks(); List books = BookDAO.searchBooksByKeyWord(keyWord);

package com.apress.books.client;
import java.util.List;


import com.apress.books.dao.BookDAO;
import com.apress.books.dao.BookDAOImpl;
import com.apress.books.model.Book;



public class BookApp {
    private static BookDAO bookDao = new BookDAOImpl();

    public static void main(String[] args) {

        // Show list of all books 
        System.err.println("List of all books: ");
        findAllBooks();
        System.out.println();

        //Seach books by keyword
        System.out.println("Search books with keyword 'Groovy' in title: ");

        searchBooks("Groovy");
        System.out.println();

        System.err.println("Searching books with keyword 'Josh' for author name: ");
        searchBooks("Josh");

    }

    private static void findAllBooks() {
        List<Book> books = BookDAO.findAllBooks();
        for (Book book : books) {
            System.out.println(book);
        }
    }

    private static void searchBooks(String keyWord) {
        List<Book> books = BookDAO.searchBooksByKeyWord(keyWord);
        for (Book book : books) {
            System.out.println(book);
        }
    }   
}

BookDAO.java interface

package com.apress.books.dao;

import java.util.List;

import com.apress.books.model.Book;
import com.apress.books.model.Category;

public interface BookDAO {

    public List<Book>findAllBooks();

    public List<Book>searchBooksByKeyWord(String keyWord);

    public List<Category>findAllCategories();

    public void insert(Book book);

    public void update(Book book);

    public void delete(Book bookId);
}

BookDAOImpl

package com.apress.books.dao;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.apress.books.model.Author;
import com.apress.books.model.Book;
import com.apress.books.model.Category;

// Uwaga w książce Brak Insert Update Delete

public class BookDAOImpl implements BookDAO {

    static {

        try {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch (ClassNotFoundException ex){}
    }

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/książki", "root", "admin");

    }

    private void closeConnection(Connection connection) {

        if (connection == null)
            return;
        try {
            connection.close();
        }catch (SQLException ex) {      
        }       
    }


    public List<Book>findAllBooks() {
        List<Book> result = new ArrayList<>();
        List<Author> authorList = new ArrayList<>();

        String sql = "select * from książki inner join autor on książka.id = autor.id_książki";

        Connection connection = null;   

    try {
        connection = getConnection();
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            Book book = new Book();
            Author author = new Author();
            book.setId(resultSet.getLong("id"));
            book.setBookTitle(resultSet.getString("tytuł_książki"));
            book.setCategoryId(resultSet.getLong("ID_KATEGORII"));
            author.setBookId(resultSet.getLong("ID_KSIĄŻKI"));
            author.setFirstName(resultSet.getString("imię"));
            author.setLastName(resultSet.getString("nazwisko"));
            authorList.add(author);
            book.setAuthors(authorList);
            book.setPublisherName(resultSet.getString("wydawca"));
            result.add(book);       
        }   
    } catch (SQLException ex) {
        ex.printStackTrace();
    } finally {
        closeConnection(connection);
    }
    return result;

    }



    public List<Book>searchBooksByKeyWord(String keyWord)  {
        List<Book> result = new ArrayList<>();
        List<Author> authorList = new ArrayList<>();

        String sql = "select * from książka inner join author on książka.id = autor.id_książki"
                + " where tutył_książki like '%"
                + keyWord.trim()
                + "%'"
                + " or imię like '%"
                + keyWord.trim()
                + "%'"
                + " or nazwisko like '%" + keyWord.trim() + "%'";

        Connection connection = null;

        try {
            connection = getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Book book = new Book();
                Author author = new Author();

                book.setId(resultSet.getLong("id"));
                book.setBookTitle(resultSet.getString("tytuł_książki"));
                book.setPublisherName(resultSet.getString("wydawca"));

                author.setFirstName(resultSet.getString("imię"));
                author.setLastName(resultSet.getString("nazwisko"));
                author.setBookId(resultSet.getLong("ID_KSIĄŻKI"));
                authorList.add(author);
                book.setAuthors(authorList);
                result.add(book);       
            }   
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            closeConnection(connection);
        }
        return result;

        }


    public List<Category> findAllCategories() {
        List<Category> result = new ArrayList<>();
        String sql = "select * from category";

        Connection connection = null;

        try {
            connection = getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Category category = new Category();

                category.setId(resultSet.getLong("id"));
                category.setCategoryDescription(resultSet.getString("opis_kategorii"));
                result.add(category);
            }   
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            closeConnection(connection);
        }
        return result;

        }


    public void insert(Book book) {
    }

    public void update(Book book) {
    }

    public void delete(Book book) {
    }


    }

findAllBooks() and searchBooksByKeyWord() methods are non-static so you need to create an instance to use them.

BookDAO is an interface with abstract methods having no implementation. BookDAOImpl has the implementations. It is already present in your code: BookDAO bookDao = new BookDAOImpl();

So it should be bookDAO instead of BookDAO :

List<Book> books = bookDAO.findAllBooks();
List<Book> books = bookDAO.searchBooksByKeyWord(keyWord);

As you are using Spring you will have to Autowire you DAO class

public class BookApp {
@Autowire
private BookDAO bookDAO;

//now you can call methods
List<Book> books = bookDAO.findAllBooks();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM