Java录入车险信息的页面以及后台相关页面的代码
时间: 2024-04-07 08:32:03 浏览: 94
很抱歉,我无法为您提供完整的Java录入车险信息的页面和后台相关页面的代码,因为这需要根据具体的需求和技术栈进行设计和开发。不同的项目有不同的需求,采用的技术和代码实现也会有所不同。
不过,我可以为您提供一些参考代码,以帮助您了解Java录入车险信息的页面和后台相关页面的实现方式。
前端页面代码示例(使用HTML和JavaScript):
```html
<form id="car-insurance-form" action="#" method="post">
<label for="car-brand">车辆品牌:</label>
<input type="text" id="car-brand" name="carBrand" required>
<br>
<label for="car-model">车辆型号:</label>
<input type="text" id="car-model" name="carModel" required>
<br>
<label for="car-number">车牌号:</label>
<input type="text" id="car-number" name="carNumber" required>
<br>
<label for="insurance-start-date">保险起始日期:</label>
<input type="date" id="insurance-start-date" name="insuranceStartDate" required>
<br>
<label for="insurance-end-date">保险结束日期:</label>
<input type="date" id="insurance-end-date" name="insuranceEndDate" required>
<br>
<label for="insurance-type">保险类型:</label>
<select id="insurance-type" name="insuranceType" required>
<option value="">请选择保险类型</option>
<option value="1">交强险</option>
<option value="2">商业险</option>
</select>
<br>
<button type="submit">提交</button>
</form>
<script>
// 使用JavaScript对车牌号进行格式验证
const carNumberInput = document.getElementById('car-number');
carNumberInput.addEventListener('input', () => {
const value = carNumberInput.value.trim().toUpperCase();
if (!/^[A-Z]{1}[A-HJ-NP-Z]{1}\d{5}$/.test(value)) {
carNumberInput.setCustomValidity('请输入正确的车牌号');
} else {
carNumberInput.setCustomValidity('');
}
});
</script>
```
后端页面代码示例(使用Java Servlet和JSP):
```java
@WebServlet("/car-insurance")
public class CarInsuranceServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String DB_URL = "jdbc:mysql://localhost:3306/car_insurance";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<CarInsuranceInfo> carInsuranceList = getCarInsuranceList();
request.setAttribute("carInsuranceList", carInsuranceList);
request.getRequestDispatcher("/WEB-INF/car-insurance.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String carBrand = request.getParameter("carBrand");
String carModel = request.getParameter("carModel");
String carNumber = request.getParameter("carNumber");
String insuranceStartDate = request.getParameter("insuranceStartDate");
String insuranceEndDate = request.getParameter("insuranceEndDate");
String insuranceType = request.getParameter("insuranceType");
saveCarInsuranceInfo(carBrand, carModel, carNumber, insuranceStartDate, insuranceEndDate, insuranceType);
response.sendRedirect(request.getContextPath() + "/car-insurance");
}
private void saveCarInsuranceInfo(String carBrand, String carModel, String carNumber, String insuranceStartDate, String insuranceEndDate, String insuranceType) {
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
String sql = "INSERT INTO car_insurance_info (car_brand, car_model, car_number, insurance_start_date, insurance_end_date, insurance_type) VALUES (?, ?, ?, ?, ?, ?)";
try (PreparedStatement statement = conn.prepareStatement(sql)) {
statement.setString(1, carBrand);
statement.setString(2, carModel);
statement.setString(3, carNumber);
statement.setString(4, insuranceStartDate);
statement.setString(5, insuranceEndDate);
statement.setString(6, insuranceType);
statement.executeUpdate();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private List<CarInsuranceInfo> getCarInsuranceList() {
List<CarInsuranceInfo> carInsuranceList = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
String sql = "SELECT * FROM car_insurance_info";
try (Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql)) {
while (resultSet.next()) {
int id = resultSet.getInt("id");
String carBrand = resultSet.getString("car_brand");
String carModel = resultSet.getString("car_model");
String carNumber = resultSet.getString("car_number");
String insuranceStartDate = resultSet.getString("insurance_start_date");
String insuranceEndDate = resultSet.getString("insurance_end_date");
String insuranceType = resultSet.getString("insurance_type");
CarInsuranceInfo carInsuranceInfo = new CarInsuranceInfo(id, carBrand, carModel, carNumber, insuranceStartDate, insuranceEndDate, insuranceType);
carInsuranceList.add(carInsuranceInfo);
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return carInsuranceList;
}
}
```
后端页面对应的JSP代码:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>车险信息管理</title>
<style>
table {
border-collapse: collapse;
}
td, th {
border: 1px solid #ddd;
padding: 8px;
}
</style>
</head>
<body>
<h1>车险信息管理</h1>
<form action="${pageContext.request.contextPath}/car-insurance" method="post">
<label for="car-brand">车辆品牌:</label>
<input type="text" id="car-brand" name="carBrand" required>
<br>
<label for="car-model">车辆型号:</label>
<input type="text" id="car-model" name="carModel" required>
<br>
<label for="car-number">车牌号:</label>
<input type="text" id="car-number" name="carNumber" required>
<br>
<label for="insurance-start-date">保险起始日期:</label>
<input type="date" id="insurance-start-date" name="insuranceStartDate" required>
<br>
<label for="insurance-end-date">保险结束日期:</label>
<input type="date" id="insurance-end-date" name="insuranceEndDate" required>
<br>
<label for="insurance-type">保险类型:</label>
<select id="insurance-type" name="insuranceType" required>
<option value="">请选择保险类型</option>
<option value="1">交强险</option>
<option value="2">商业险</option>
</select>
<br>
<button type="submit">提交</button>
</form>
<hr>
<h2>车险信息列表</h2>
<table>
<tr>
<th>ID</th>
<th>车辆品牌</th>
<th>车辆型号</th>
<th>车牌号</th>
<th>保险起始日期</th>
<th>保险结束日期</th>
<th>保险类型</th>
</tr>
<c:forEach items="${carInsuranceList}" var="carInsurance">
<tr>
<td>${carInsurance.id}</td>
<td>${carInsurance.carBrand}</td>
<td>${carInsurance.carModel}</td>
<td>${carInsurance.carNumber}</td>
<td>${carInsurance.insuranceStartDate}</td>
<td>${carInsurance.insuranceEndDate}</td>
<td>${carInsurance.insuranceType == '1' ? '交强险' : '商业险'}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
以上代码示例仅供参考,实际项目中还需要根据具体需求进行调整和优化。
阅读全文