Suppose Table 1 in the database is named table1, Table 2 is named table2, Table 3 is named table3, and table4 is named table4. Their structure is as follows:
table1: id, field1, field2, field3, is_selected
table2: id, field1, field2, field3
table3: ID, field1, field2, field3
Table3: id, username
table4: id, value_a, value_b, value_c, user_name
Where the is_selected field in Table 1 is used to identify whether a record is selected.
Here is an example of dao and servlet code:
DAO code:
public class TableDAO {
// 获取数据库连接
public Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
}
// 获取table1表中所有未被选中的记录
public List getUnselectedTable1() throws SQLException, ClassNotFoundException {
List table1List = new ArrayList<>();
Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM table1 WHERE is_selected = 0");
while (rs.next()) {
Table1 table1 = new Table1();
table1.setId(rs.getInt("id"));
table1.setField1(rs.getString("field1"));
table1.setField2(rs.getString("field2"));
table1.setField3(rs.getString("field3"));
table1List.add(table1);
}
rs.close();
stmt.close();
conn.close();
return table1List;
}
// 根据id获取table3表中的用户名
public String getUserNameById(int id) throws SQLException, ClassNotFoundException {
String userName = null;
Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement("SELECT username FROM table3 WHERE id = ?");
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
userName = rs.getString("username");
}
rs.close();
pstmt.close();
conn.close();
return userName;
}
// 将选中的记录标记为已选中
public void updateSelectedTable1(int id) throws SQLException, ClassNotFoundException {
Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement("UPDATE table1 SET is_selected = 1 WHERE id = ?");
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
// 将记录插入table4表
public void insertTable4(String valueA, String valueB, String valueC, String userName) throws SQLException, ClassNotFoundException {
Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO table4 (value_a, value_b, value_c, user_name) VALUES (?, ?, ?, ?)");
pstmt.setString(1, valueA);
pstmt.setString(2, valueB);
pstmt.setString(3, valueC);
pstmt.setString(4, userName);
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
}
Servlet code:
public class SelectServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String valueA = request.getParameter("select1");
String valueB = null;
String valueC = request.getParameter("select2");
HttpSession session = request.getSession();
int userId = (int) session.getAttribute("user_id");
String userName = null;
try {
TableDAO tableDAO = new TableDAO();
// 获取当前用户的用户名
userName = tableDAO.getUserNameById(userId);
// 获取下拉框1选中的值所对应的表1字段2的值
Table1 selectedTable1 = tableDAO.getSelectedTable1(Integer.parseInt(valueA));
if (selectedTable1 != null) {
valueB = selectedTable1.getField2();
// 将选中的记录标记为已选中
tableDAO.updateSelectedTable1(selectedTable1.getId());
// 将记录插入table4表
tableDAO.insertTable4(valueA, valueB, valueC, userName);
// 返回成功响应
response.getWriter().println("success");
} else {
// 下拉框1选中的值已被其他用户选中
response.getWriter().println("failure");
}
} catch (Exception e) {
e.printStackTrace();
// 返回异常响应
response.getWriter().println("error");
}
}
}