Привет, я пытаюсь сделать книжный магазин простым приложением из книги Vishal Layka Learn Java для веб-разработки. У меня проблема в клиенте уровня доступа к данным BookAPP. java, который должен использовать интерфейс BookDAO. Я включил коды BookAPP, BookDAO, BookDAOImpl.
Ошибки: метод «невозможно создать» c метод «ссылка на не-статус» c из типа bookDAO Список книг = BookDAO.findAllBooks (); Список книг = 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 интерфейс
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) {
}
}