0% found this document useful (0 votes)
49 views

Book DAO

This Java class defines a BookDAO that provides CRUD database operations for books stored in a database table. It includes methods for inserting, listing, deleting, updating, and getting book records from the database by connecting to MySQL using JDBC. The class establishes a database connection, executes SQL statements using prepared statements, and closes the connection to perform these data access operations on the book table.

Uploaded by

Rajesh Vanzara
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Book DAO

This Java class defines a BookDAO that provides CRUD database operations for books stored in a database table. It includes methods for inserting, listing, deleting, updating, and getting book records from the database by connecting to MySQL using JDBC. The class establishes a database connection, executes SQL statements using prepared statements, and closes the connection to perform these data access operations on the book table.

Uploaded by

Rajesh Vanzara
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.alpabvoc.virtuallearning.

controller;

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;

/**
* AbstractDAO.java
* This DAO class provides CRUD database operations for the table book
* in the database.
* @author www.codejava.net
*
*/
public class BookDAO {
private String jdbcURL;
private String jdbcUsername;
private String jdbcPassword;
private Connection jdbcConnection;

public BookDAO(String jdbcURL, String jdbcUsername, String jdbcPassword) {


this.jdbcURL = jdbcURL;
this.jdbcUsername = jdbcUsername;
this.jdbcPassword = jdbcPassword;
}

protected void connect() throws SQLException {


if (jdbcConnection == null || jdbcConnection.isClosed()) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new SQLException(e);
}
jdbcConnection = DriverManager.getConnection(
jdbcURL, jdbcUsername,
jdbcPassword);
}
}

protected void disconnect() throws SQLException {


if (jdbcConnection != null && !jdbcConnection.isClosed()) {
jdbcConnection.close();
}
}

public boolean insertBook(Book book) throws SQLException {


String sql = "INSERT INTO book (title, author, price) VALUES
(?, ?, ?)";
connect();

PreparedStatement statement = jdbcConnection.prepareStatement(sql);


statement.setString(1, book.getTitle());
statement.setString(2, book.getAuthor());
statement.setFloat(3, book.getPrice());
boolean rowInserted = statement.executeUpdate() > 0;
statement.close();
disconnect();
return rowInserted;
}

public List<Book> listAllBooks() throws SQLException {


List<Book> listBook = new ArrayList<>();

String sql = "SELECT * FROM book";

connect();

Statement statement = jdbcConnection.createStatement();


ResultSet resultSet = statement.executeQuery(sql);

while (resultSet.next()) {
int id = resultSet.getInt("book_id");
String title = resultSet.getString("title");
String author = resultSet.getString("author");
float price = resultSet.getFloat("price");

Book book = new Book(id, title, author, price);


listBook.add(book);
}

resultSet.close();
statement.close();

disconnect();

return listBook;
}

public boolean deleteBook(Book book) throws SQLException {


String sql = "DELETE FROM book where book_id = ?";

connect();

PreparedStatement statement = jdbcConnection.prepareStatement(sql);


statement.setInt(1, book.getId());

boolean rowDeleted = statement.executeUpdate() > 0;


statement.close();
disconnect();
return rowDeleted;
}

public boolean updateBook(Book book) throws SQLException {


String sql = "UPDATE book SET title = ?, author = ?, price = ?";
sql += " WHERE book_id = ?";
connect();

PreparedStatement statement = jdbcConnection.prepareStatement(sql);


statement.setString(1, book.getTitle());
statement.setString(2, book.getAuthor());
statement.setFloat(3, book.getPrice());
statement.setInt(4, book.getId());
boolean rowUpdated = statement.executeUpdate() > 0;
statement.close();
disconnect();
return rowUpdated;
}

public Book getBook(int id) throws SQLException {


Book book = null;
String sql = "SELECT * FROM book WHERE book_id = ?";

connect();

PreparedStatement statement = jdbcConnection.prepareStatement(sql);


statement.setInt(1, id);

ResultSet resultSet = statement.executeQuery();

if (resultSet.next()) {
String title = resultSet.getString("title");
String author = resultSet.getString("author");
float price = resultSet.getFloat("price");

book = new Book(id, title, author, price);


}

resultSet.close();
statement.close();

return book;
}
}

You might also like