1.SQL : SELECT * FROM tablename WHERE 保養廠資訊 LIKE '%桃園市%'
2.不跳轉網頁:參考這篇介紹 jsp/ajax
模糊查詢的簡單範例
JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>保養廠信息</title>
</head>
<body>
<h1>保養廠信息</h1>
<form method="get" action="">
<label>縣市:</label>
<select name="location" onchange="this.form.submit()">
<option value="">請選擇</option>
<option value="臺北市">臺北市</option>
<option value="新北市">新北市</option>
<option value="桃園市">桃園市</option>
<option value="臺中市">臺中市</option>
<option value="臺南市">臺南市</option>
<option value="高雄市">高雄市</option>
</select>
</form>
<table>
<tr>
<th>ID</th>
<th>保養廠名稱</th>
<th>縣市</th>
</tr>
<c:forEach var="maintenance" items="${maintenances}">
<tr>
<td>${maintenance.id}</td>
<td>${maintenance.factory}</td>
<td>${maintenance.location}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
後端 Java
public class Maintenance {
private int id;
private String factory;
private String location;
// getter和setter方法
}
Service 層
@Service
public class MaintenanceService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Maintenance> findMaintenanceByLocation(String location) {
String sql = "SELECT * FROM maintenance WHERE location LIKE '%" + location + "%'";
List<Maintenance> maintenances = jdbcTemplate.query(sql, new BeanPropertyRowMapper(Maintenance.class));
return maintenances;
}
}