📄 bookbeandao.java
字号:
package com.csthit.store.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.resource.spi.ConnectionManager;
import com.csthit.store.db.DB;
import com.csthit.store.vo.BookBean;
public class BookBeanDAO {
private Connection connection;
private PreparedStatement titlesQuery;
private ResultSet results;
// 返回BookBeans列表
public List getTitles() {
List titlesList = new ArrayList();
// 获取书籍列表
try {
connection = DB.getConn();
titlesQuery = connection
.prepareStatement("SELECT isbn, title, editionNumber, "
+ "copyright, publisherID, imageFile, price "
+ "FROM titles ORDER BY title");
ResultSet results = titlesQuery.executeQuery();
// 读取行数据
while (results.next()) {
BookBean book = new BookBean();
book.setISBN(results.getString("isbn"));
book.setTitle(results.getString("title"));
book.setEditionNumber(results.getInt("editionNumber"));
book.setCopyright(results.getString("copyright"));
book.setPublisherID(results.getInt("publisherID"));
book.setImageFile(results.getString("imageFile"));
book.setPrice(results.getDouble("price"));
titlesList.add(book);
}
}
// 处理数据库驱动和连接异常
// 处理数据库异常
catch (SQLException exception) {
exception.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (results != null) {
results.close();
results = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null && (!connection.isClosed())) {
connection.close();
}
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
}
return titlesList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -