public List<Courses> getCourseByLikeISBN(String searchKey) { Connection con = null; PreparedStatement pre = null; ResultSet resultSet = null; try { con = DBUtils.getConnection(); String sql = "select * from td_courses where isbn LIKE ?"; pre = con.prepareStatement(sql); searchKey = searchKey+'%'; pre.setString(1, searchKey); resultSet = pre.executeQuery(); List<Courses> res = new ArrayList<>(); while (resultSet.next()) { String isbns = resultSet.getString("isbn"); String bookName = resultSet.getString("book_name"); String author = resultSet.getString("author"); String content = resultSet.getString("content"); String press = resultSet.getString("press"); String tname = resultSet.getString("tname"); Date pressTime = resultSet.getDate("press_time"); Courses course = new Courses(); course.setIsbn(isbns); course.setBookName(bookName); course.setAuthor(author); course.setContent(content); course.setPress(press); course.setPressTime(pressTime); course.setTname(tname); res.add(course); } return res; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtils.closeConnection(con, pre, resultSet); } return null; } }
时间: 2024-02-14 14:24:12 浏览: 65
List-of-golf-courses-in-China-中国高尔夫球场列表-golflive.zip
这是一个 Java 方法,用于从数据库中根据 ISBN 模糊查询课程信息。该方法首先获取数据库连接,然后构造 SQL 语句,在查询中使用了占位符 "?",以避免 SQL 注入攻击。接着将查询关键字加上 "%",并将其设置为占位符的值,执行查询并将结果存储在一个 List 中。最后,将查询所用的连接、预处理语句和结果集关闭,返回查询结果,如果查询失败,则返回 null。
阅读全文