0
Follow
1
View

For questions about #java#, please answer the experts!(Development tool -eclipse)

gala42 注册会员
2023-02-28 20:07


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");
        }
    }
}


dizheng1982 注册会员
2023-02-28 20:07
; //创建PreparedStatement实例 PreparedStatement pstmt = conn.prepare Statement( sql ) ; //获取前端传来的参数 String valueA = request.get Parameter( "valueA" ) ; String valueC = request.get Parameter( "valueC" ) ; int userId = Integer . parse Int( request . getParameter ( "userId" ) ); //对占位符设置实际的参数 pstmt.set String(1, valueA ) ; //根据valueA从表1中查询出valueB String sql2 = "SELECT valueB FROM table1 WHERE valueA=?" ; PreparedStatement pstmt2 = conn.prepare Statement( sql2 ) ; pstmt2.set String(1, valueA ) ; ResultSet rs = pstmt2.execute Query() ; String valueB = null; if (rs.next () ){ valueB = rs.get String( "valueB" ) ; } //将valueB参数设置到SQL语句中 pstmt.set String(2, valueB ) ; //设置valueC参数 pstmt.set String(3, valueC ) ; //根据userId从表3中查询出name String sql3 = "SELECT name FROM table3 WHERE userId=?" ; PreparedStatement pstmt3 = conn.prepare Statement( sql3 ) ; pstmt3.set Int(1, userId ) ; ResultSet rs2 = pstmt3.execute Query() ; String name = null; if (rs2.next () ){ name = rs2.get String( "name" ) ; } //将name参数设置到SQL语句中 pstmt.set String(4, name ) ; //执行插入操作 pstmt.execute Update() ; SERVLET层: //获取前端传来的参数 String valueA = request.get Parameter( "valueA" ) ; String valueC = request.get Parameter( "valueC" ) ; int userId = Integer . parse Int( request . getParameter ( "userId" ) ); //创建DAO实例 DAO dao = new DAO() ; //调用DAO实例的插入方法,将参数传入 dao.insert(valueA,valueC,userId); .
< !- - - - - >