如何从一行移动到另一行

时间:2015-11-26 18:15:49

标签: java database

this is my shape of database

我已将labelTEXT设置为" T26内容"。然后,我想将labelTEXT内容更改为" T30内容"点击"是按钮" (ps:T26具有T30 IF_YES值)。当我点击"是按钮"再次,labelTEXT内容应更改为T25内容。怎么做?

private void btYesActionPerformed(java.awt.event.ActionEvent evt) {                                     
    Connection conn = connectionClass.GetConnections();
    Statement st;
    ResultSet rs;
    String task;
    task = "T26";
    try {
        st = conn.createStatement();
        rs = st.executeQuery("select * FROM task where ID = '"+task+"'");
        while (rs.next()) {
            String TEXT = rs.getString("TEXT");
            String IF_YES = rs.getString("IF_YES");
            labelTEXT.setText(TEXT);
            task = IF_YES;
        }
    } catch (Exception e) {
    }
}

希望这个足够清楚

1 个答案:

答案 0 :(得分:0)

您将根据用户点击的内容再次查询与新ID对应的行。

编辑: 查找准备好的陈述,因为它们是一种安全的做事方式http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

根据现有代码构建,它应该对查询执行以下操作。必须填写UI和控制流逻辑:

private void btYesActionPerformed(java.awt.event.ActionEvent evt) {                                     
    Connection conn = connectionClass.GetConnections();
    boolean done = false;
    PreparedStatement pst;
    ResultSet rs;
    String task;
    task = "T26"; // Set initial value of task
    // Assuming one row expected (may want to handle error case later)
    query = "SELECT * FROM task WHERE ID = ? LIMIT 1"

    try {
        pst = conn.prepareStatement(query);

        while (!done) {
            pst.setString(1, task)
            rs = pst.executeQuery()
            while (rs.next()) {
                String TEXT = rs.getString("TEXT");
                String IF_YES = rs.getString("IF_YES");
                labelTEXT.setText(TEXT);
                task = IF_YES; // Task is now updated for the next loop
            }
            // TODO: Make sure to set done to false when all tasks complete.
        }

        pst.close()
        conn.close()
    } catch (Exception e) {
    }
}

注意:您可能还需要考虑在需要时对事务进行多次查询https://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html