图书管理系统Dao层利用JDBC调用数据接口代码
时间: 2023-09-08 18:16:47 浏览: 50
以下是使用JDBC调用数据接口的图书管理系统DAO层的示例代码:
```
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BookDao {
// 数据库连接信息
private final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private final String DB_URL = "jdbc:mysql://localhost:3306/bookdb";
private final String USER = "root";
private final String PASS = "123456";
// 查询所有图书信息
public List<Book> getAllBooks() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<Book> bookList = new ArrayList<>();
try {
// 注册 JDBC 驱动器
Class.forName(JDBC_DRIVER);
// 打开连接
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// 执行查询
stmt = conn.createStatement();
String sql = "SELECT * FROM book";
rs = stmt.executeQuery(sql);
// 处理结果集
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setPrice(rs.getDouble("price"));
book.setNumber(rs.getInt("number"));
bookList.add(book);
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
return bookList;
}
// 添加图书信息
public boolean addBook(Book book) {
Connection conn = null;
PreparedStatement pstmt = null;
boolean result = false;
try {
// 注册 JDBC 驱动器
Class.forName(JDBC_DRIVER);
// 打开连接
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// 执行插入
String sql = "INSERT INTO book(name, author, publisher, price, number) VALUES (?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, book.getName());
pstmt.setString(2, book.getAuthor());
pstmt.setString(3, book.getPublisher());
pstmt.setDouble(4, book.getPrice());
pstmt.setInt(5, book.getNumber());
int count = pstmt.executeUpdate();
// 处理结果
if (count > 0) {
result = true;
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
return result;
}
// 更新图书信息
public boolean updateBook(Book book) {
Connection conn = null;
PreparedStatement pstmt = null;
boolean result = false;
try {
// 注册 JDBC 驱动器
Class.forName(JDBC_DRIVER);
// 打开连接
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// 执行更新
String sql = "UPDATE book SET name=?, author=?, publisher=?, price=?, number=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, book.getName());
pstmt.setString(2, book.getAuthor());
pstmt.setString(3, book.getPublisher());
pstmt.setDouble(4, book.getPrice());
pstmt.setInt(5, book.getNumber());
pstmt.setInt(6, book.getId());
int count = pstmt.executeUpdate();
// 处理结果
if (count > 0) {
result = true;
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
return result;
}
// 删除图书信息
public boolean deleteBook(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
boolean result = false;
try {
// 注册 JDBC 驱动器
Class.forName(JDBC_DRIVER);
// 打开连接
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// 执行删除
String sql = "DELETE FROM book WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int count = pstmt.executeUpdate();
// 处理结果
if (count > 0) {
result = true;
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
return result;
}
}
```
这里仅提供了一个简单的示例,实际开发中还需要根据具体情况进行修改和完善。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)