public List<Book> getBookList() {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<Book> list = new ArrayList<>();
try {
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库 参数一:.../.../数据库名称 参数二:账号 参数三:密码
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lucene", "root", "123");
//获得句柄
preparedStatement = connection.prepareStatement("select * from book");
//获得结果集
resultSet = preparedStatement.executeQuery();
//组装结果
while(resultSet.next()) {
Book book = new Book();
book.setId(resultSet.getInt("id"));
book.setName(resultSet.getString("name"));
book.setPrice(resultSet.getFloat("price"));
book.setPic(resultSet.getString("pic"));
book.setDescription(resultSet.getString("description"));
list.add(book);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (preparedStatement!=null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return list;
}
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<Book> list = new ArrayList<>();
try {
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库 参数一:.../.../数据库名称 参数二:账号 参数三:密码
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lucene", "root", "123");
//获得句柄
preparedStatement = connection.prepareStatement("select * from book");
//获得结果集
resultSet = preparedStatement.executeQuery();
//组装结果
while(resultSet.next()) {
Book book = new Book();
book.setId(resultSet.getInt("id"));
book.setName(resultSet.getString("name"));
book.setPrice(resultSet.getFloat("price"));
book.setPic(resultSet.getString("pic"));
book.setDescription(resultSet.getString("description"));
list.add(book);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (preparedStatement!=null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return list;
}