public List<Product> findByIddd(String guess) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; List<Product> products = new ArrayList<>(); try { conn = DBHelper.getConnection(); String sql = "select productid, category, cname, ename, image, " + "listprice, unitcost, descn " + "from products where productid = ? "; pstmt = conn.prepareStatement(sql); pstmt.setString(1, guess); rs = pstmt.executeQuery(); while (rs.next()) { Product p = new Product(); p.setProductid(rs.getString("productid")); p.setCategory(rs.getString("category")); p.setCname(rs.getString("cname")); p.setEname(rs.getString("ename")); p.setImage(rs.getString("image")); p.setListprice(rs.getDouble("listprice")); p.setUnitcost(rs.getDouble("unitcost")); p.setDescn(rs.getString("descn")); products.add(p); } } catch (SQLException e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { } } if (conn != null) { try { conn.close(); } catch (SQLException e) { } } } return products; }改成返回输出所有包含guess关键字的数据要怎么改
时间: 2024-01-16 19:02:21 浏览: 186
要查询所有包含 `guess` 关键字的数据,可以将 SQL 语句中的 `where` 子句改为使用 `like` 操作符进行模糊匹配。具体的改动如下所示:
```
public List<Product> findByIddd(String guess) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Product> products = new ArrayList<>();
try {
conn = DBHelper.getConnection();
String sql = "select productid, category, cname, ename, image, " +
"listprice, unitcost, descn " +
"from products " +
"where cname like ? or ename like ?"; // 改动的语句
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + guess + "%"); // 模糊匹配
pstmt.setString(2, "%" + guess + "%"); // 模糊匹配
rs = pstmt.executeQuery();
while (rs.next()) {
Product p = new Product();
p.setProductid(rs.getString("productid"));
p.setCategory(rs.getString("category"));
p.setCname(rs.getString("cname"));
p.setEname(rs.getString("ename"));
p.setImage(rs.getString("image"));
p.setListprice(rs.getDouble("listprice"));
p.setUnitcost(rs.getDouble("unitcost"));
p.setDescn(rs.getString("descn"));
products.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
return products;
}
```
这样修改后,该方法会查询出所有包含 `guess` 关键字的数据,无论是在 `cname` 列还是在 `ename` 列中出现的。
阅读全文
相关推荐










package book.library.dao;
import book.library.beans.BookBeans;
import book.library.util.DBCon;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BookDAO {
private Connection conn;
public BookDAO() throws SQLException {
this.conn = DBCon.getConnection();
}
public void addBook(BookBeans book) throws SQLException {
String sql = "INSERT INTO books (title, price, author, publisher, stock, modifiedStock) VALUES (?, ?, ?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, book.getTitle());
pstmt.setDouble(2, book.getPrice());
pstmt.setString(3, book.getAuthor());
pstmt.setString(4, book.getPublisher());
pstmt.setInt(5, book.getStock());
pstmt.executeUpdate();
}
}
public void updateBook(BookBeans book) throws SQLException {
String sql = "UPDATE books SET title = ?, price = ?, author = ?, publisher = ?, stock = ? WHERE id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, book.getTitle());
pstmt.setDouble(2, book.getPrice());
pstmt.setString(3, book.getAuthor());
pstmt.setString(4, book.getPublisher());
pstmt.setInt(5, book.getStock());
pstmt.setInt(6, book.getId());
pstmt.executeUpdate();
}
}
public void deleteBook(int id) throws SQLException {
String sql = "DELETE FROM books WHERE id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
}
}
public List<BookBeans> getAllBooks() throws SQLException {
List<BookBeans> books = new ArrayList<>();
String sql = "SELECT * FROM books";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
BookBeans book = new BookBeans();
book.setId(rs.getInt("id"));
book.setTitle(rs.getString("title"));
book.setPrice(rs.getDouble("price"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setStock(rs.getInt("stock"));
books.add(book);
}
}
return books;
}
}
这是后台代码,请修改。<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="https://jakarta.ee/xml/ns/jakartaee/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>图书信息展示</title>
</head>
<body>
图书信息
图书名称
图书价格
图书作者
出版社
存放数量
修改数量
删除
<c:forEach var="book" items="${books}">
${book.title}
${book.price}
${book.author}
${book.publisher}
${book.stock}
修改
删除
</c:forEach>
添加图书信息
</body>
</html>这是前台代码,请修改。



<%@ page import="util.DbConnet" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
//获取用户输入的查询内容
String username = request.getParameter("appliance_type");
if(username==null) username="";
String realname = request.getParameter("appliance_name");
if(realname==null) realname="";
//1.数据总数
String sql = "select count(*) as total from living_room_appliances " +
"where appliance_type like ? and appliance_name like ?;";
Object[] params = new Object[]{
"%"+username+"%","%"+realname+"%"
};
ResultSet rs = DbConnet.select(sql, params);
rs.next();
int total = rs.getInt("total");
//2.每页显示的行数
int pageSize = 5;
//3.总页数
double result = (double)total/pageSize;
//向上取整:只要数值带有有效的小数,舍去小数,整数位加一
int pageTotal = (int) Math.ceil(result);
//4.当前页码
String pageNoStr = request.getParameter("pageNo");
pageNoStr = pageNoStr==null?"1":pageNoStr;
int pageNo = Integer.parseInt(pageNoStr);
//获取用户表中的数据,显示出来
sql = "select * from living_room_appliances " +
"where appliance_type like ? and appliance_name like ? " +
"limit ?,?;";
int start = (pageNo - 1) * pageSize;//(当前页码-1)*每页显示的行数
params = new Object[]{
"%"+username+"%","%"+realname+"%",start,pageSize
};
rs = DbConnet.select(sql, params);
%>
<html>
<head>
<title>用户列表</title>
</head>
<body>
<%--搜索区域 S--%>
<form>
<label for="username">账号:</label>
<input type="text" id="username" name="username" value="<%=username%>">
<label for="username">姓名:</label>
<input type="text" id="realname" name="realname" value="<%=realname%>">
<button id="btnSearch" class="primary" type="button">查询</button>
<button id="btnReset" type="button">重置</button>
</form>
<%--搜索区域 E--%>
<%--按钮区域 S--%>
<button id="btnAdd" class="primary" type="button">新增</button>
<%--按钮区域 E--%>
<%--表格区域 S--%>
编号
账号
姓名
操作
<%
while (rs.next()){
%>
<%=rs.getString("appliance_id")%>
<%=rs.getString("appliance_type")%>
<%=rs.getString("appliance_name")%>
<button data-id="<%=rs.getString("id")%>" name="btnEdit" class="primary" type="button">编辑</button>
<button data-id="<%=rs.getString("id")%>" name="btnDelete" class="danger" type="button">删除</button>
<%
}
%>
<%--表格区域 E--%>
<%--页码区域 S--%>
共<%=total%>条数据/每页<%=pageSize%>条
<% if(pageNo>1){%>
首页
上一页
<% } %>
<%-- 1--%>
<% for (int i=1;i<=pageTotal;i++){%>
" class="page">
<%=i%>
<% } %>
<% if(pageTotal>pageNo){%>
下一页
尾页
<% } %>
<%--页码区域 E--%>
<script src="../js/jquery-3.5.1.min.js"></script>
<script src="../js/common.js"></script>
<script>
//绑定搜索按钮的点击事件
$('#btnSearch').on('click', function () {
//获取搜索框中的内容:账号、姓名
let username = $('#username').val();
let realname = $('#realname').val();
window.location.href="list.jsp?appliance_type=" + username + "&realname=" + realname;
});
//绑定重置按钮的点击事件
$('#btnReset').on('click', function () {
window.location.href="list.jsp";
});
//绑定新增按钮的点击事件
$('#btnAdd').on('click', function () {
window.location.href="add.jsp";
});
//绑定行内的编辑按钮点击事件
$('button[name=btnEdit]').on('click', function () {
let id = $(this).attr('data-id');//从当前点击的按钮身上获取data-id的值
window.location.href = 'edit.jsp?id='+id;
});
//绑定行内的删除按钮点击事件
$('button[name=btnDelete]').on('click', function () {
if(confirm("确定要删除吗?")) {
//获取删除按钮所在行的编号(id)
let id = $(this).attr('data-id');//从当前点击的按钮身上获取data-id的值
//无刷新方式提交删除请求
postAction('/user/delete', {id: id}, function (res) {
alert(res.msg);
if (res.result) window.location.href = res.url;
});
}
});
</script>
</body>
</html>


<% //连接数据库 String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/votedb?serverTimezone=UTC&characterEncoding=UTF-8"; String username = "root"; String password = "123456"; Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM vote"); while(rs.next()){ String title = rs.getString("title"); %> <input type="radio" name="option" value="<%=title%>"><%=title%>
<% } } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接 try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } %> 提交投票选中后数据库数据变化




