Skip to content
TheChatArchiveThe Chat Archive
Perplexitypplx-70b-online· PerplexityHugging Face Arena Human Preference 55K

package cn.edu.wtbu.ec.im.isad; import java.sql.*; import java.util.ArrayList; import…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Upstagesolar-10.7b-instruct-v1.0’s answer.
  1. User

    package cn.edu.wtbu.ec.im.isad;

    import java.sql.*; import java.util.ArrayList; import java.util.List;

    public class BookDAO { private static final String INSERT_BOOK_SQL = "INSERT INTO books (title, author, available) VALUES (?, ?, ?)"; private static final String SELECT_BOOK_BY_ID = "SELECT * FROM books WHERE id = ?"; private static final String SELECT_ALL_BOOKS = "SELECT * FROM books LIMIT ?, ?"; private static final String UPDATE_BOOK_SQL = "UPDATE books SET title = ?, author = ?, available = ? WHERE id = ?"; private static final String DELETE_BOOK_SQL = "DELETE FROM books WHERE id = ?"; private static final String SEARCH_BOOKS = "SELECT * FROM books WHERE title LIKE ? OR author LIKE ? LIMIT ?, ?";

    public Book createBook(Book book) {
    	try (Connection connection = DatabaseUtil.getConnection();
    			PreparedStatement preparedStatement = connection.prepareStatement(INSERT_BOOK_SQL,
    					Statement.RETURN_GENERATED_KEYS)) {
    		preparedStatement.setString(1, book.getTitle());
    		preparedStatement.setString(2, book.getAuthor());
    		preparedStatement.setBoolean(3, book.isAvailable());
    		preparedStatement.executeUpdate();
    
    		ResultSet resultSet = preparedStatement.getGeneratedKeys();
    		if (resultSet.next()) {
    			int id = resultSet.getInt(1);
    			book.setId(id);
    		}
    	} catch (SQLException e) {
    		e.printStackTrace();
    	}
    	return book;
    }
    
    public Book getBookById(int id) {
    	Book book = null;
    	try (Connection connection = DatabaseUtil.getConnection();
    			PreparedStatement preparedStatement = connection.prepareStatement(SELECT_BOOK_BY_ID)) {
    		preparedStatement.setInt(1, id);
    		ResultSet resultSet = preparedStatement.executeQuery();
    
    		if (resultSet.next()) {
    			String title = resultSet.getString("title");
    			String author = resultSet.getString("author");
    			boolean available = resultSet.getBoolean("available");
    
    			book = new Book(id, title, author, available);
    		}
    	} catch (SQLException e) {
    		e.printStackTrace();
    	}
    	return book;
    }
    
    public List<Book> getAllBooks(int offset, int limit) {
    	List<Book> books = new ArrayList<>();
    	try (Connection connection = DatabaseUtil.getConnection();
    			PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_BOOKS)) {
    		preparedStatement.setInt(1, offset);
    		preparedStatement.setInt(2, limit);
    		ResultSet resultSet = preparedStatement.executeQuery();
    
    		while (resultSet.next()) {
    			int id = resultSet.getInt("id");
    			String title = resultSet.getString("title");
    			String author = resultSet.getString("author");
    			boolean available = resultSet.getBoolean("available");
    
    			books.add(new Book(id, title, author, available));
    		}
    	} catch (SQLException e) {
    		e.printStackTrace();
    	}
    	return books;
    }
    
    public boolean updateBook(Book book) {
    	boolean rowUpdated = false;
    	try (Connection connection = DatabaseUtil.getConnection();
    			PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_BOOK_SQL)) {
    		preparedStatement.setString(1, book.getTitle());
    		preparedStatement.setString(2, book.getAuthor());
    		preparedStatement.setBoolean(3, book.isAvailable());
    		preparedStatement.setInt(4, book.getId());
    
    		rowUpdated = preparedStatement.executeUpdate() > 0;
    	} catch (SQLException e) {
    		e.printStackTrace();
    	}
    	return rowUpdated;
    }
    
    public boolean deleteBook(int id) {
    	boolean rowDeleted = false;
    	try (Connection connection = DatabaseUtil.getConnection();
    			PreparedStatement preparedStatement = connection.prepareStatement(DELETE_BOOK_SQL)) {
    		preparedStatement.setInt(1, id);
    
    		rowDeleted = preparedStatement.executeUpdate() > 0;
    	} catch (SQLException e) {
    		e.printStackTrace();
    	}
    	return rowDeleted;
    }
    
    public List<Book> searchBooks(String keyword, int offset, int limit) {
    	List<Book> books = new ArrayList<>();
    	try (Connection connection = DatabaseUtil.getConnection();
    			PreparedStatement preparedStatement = connection.prepareStatement(SEARCH_BOOKS)) {
    		preparedStatement.setString(1, "%" + keyword + "%");
    		preparedStatement.setString(2, "%" + keyword + "%");
    		preparedStatement.setInt(3, offset);
    		preparedStatement.setInt(4, limit);
    		ResultSet resultSet = preparedStatement.executeQuery();
    
    		while (resultSet.next()) {
    			int id = resultSet.getInt("id");
    			String title = resultSet.getString("title");
    			String author = resultSet.getString("author");
    			boolean available = resultSet.getBoolean("available");
    
    			books.add(new Book(id, title, author, available));
    		}
    	} catch (SQLException e) {
    		e.printStackTrace();
    	}
    	return books;
    }
    

    } 请完整补充上述代码的剩下内容

  2. Perplexity Assistant

    The provided code is a Java class called BookDAO that serves as a data access object for a book management system. It includes methods to create, get, update, and delete books from a database, as well as search for books based on a keyword. Here's a brief overview of the class and its methods:

    • CREATE BOOK: The createBook method takes a Book object as input, inserts it into the database, and returns the updated book object with its ID.
    • GET BOOK BY ID: The getBookById method takes an integer ID as input and returns a Book object corresponding to the book with the given ID.
    • GET ALL BOOKS: The getAllBooks method takes two integer parameters (offset and limit) and returns a list of books from the database with the specified offset and limit.
    • UPDATE BOOK: The updateBook method takes a Book object as input and updates the book in the database if it exists. It returns a boolean value indicating whether the book was updated or not.
    • DELETE BOOK: The deleteBook method takes an integer ID as input and deletes the book with the given ID from the database. It returns a boolean value indicating whether the book was deleted or not.
    • SEARCH BOOKS: The searchBooks method takes a string keyword and two integer parameters (offset and limit) as input and returns a list of books from the database that match the given keyword.

    The BookDAO class uses prepared statements for interacting with the database, which helps prevent SQL injection attacks. The class also handles exceptions and provides error messages when necessary.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.