如何告诉mysql表将下一个匹配的记录检索到我的默认表模型

时间:2018-05-03 05:52:35

标签: java mysql jtable defaulttablemodel data-retrieval

如何告诉我的java程序将下一个匹配的记录检索到我的默认表模型中。

以下是我到目前为止的家庭工作。使用jTable tb1和默认的桌子型号dtm对我来说是强制性的。

 private void Show_My_LettersActionPerformed(java.awt.event.ActionEvent evt) {                                                
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            System.out.println("Connecting to a selected database...");
            conn = DriverManager.getConnection(url, "root", "root");
            System.out.println("Connected database successfully...");

            System.out.println("Creating statement...");
            stmt = conn.createStatement();
            String sql = "SELECT * from LCI where SUB_ID = '" + SUB_ID_.getText() + "' AND L_DATE = '" + DATE.getText() + "'";
            ResultSet rs1, rs2, rs3, rs4, rs5, rs6, rs7;
            rs1=j.getData("select COUNT(*) from LCI");

            try (ResultSet rs = stmt.executeQuery(sql)) {
                DefaultTableModel dtm = (DefaultTableModel) tb1.getModel();
                while (rs.next()) {
                    String L_ID_ = rs.getString("L_ID");
                    String L_DATE_ = rs.getString("L_DATE");
                    String heading = rs.getString("HEADING");
                    String sub_id = rs.getString("SUB_ID");

                    System.out.print("ID: " + L_ID_);
                    System.out.print(", Letter date: " + L_DATE_);
                    System.out.print(", Heading " + heading);
                    System.out.println(", Subject ID " + sub_id);


/* This gives the correct out put when debug is done.
    But the below code doesn't retrive the full out put. 
    It gives only the very first record matching with the user inputs*/


                    Vector v = new Vector();
                    Vector v1 = new Vector();
                    Vector v2 = new Vector();
                    Vector v3 = new Vector();

                    JOptionPane.showMessageDialog(this, "Done");
                    dtm.getColumnName(1);
                    v.addElement(rs.getString(1));
                    dtm.addColumn(v);

                    dtm.getColumnName(3);
                    v1.addElement(rs.getString(3));
                    dtm.addColumn(v1);

                    dtm.getColumnName(10);
                    v2.addElement(rs.getString(10));
                    dtm.addColumn(v2);
                    // stmt.executeQuery("SELECT * FROM   LCI WHERE  L_ID IN(SELECT (L_ID + 1)FROM   LCI  WHERE  L_DATE = '"+DATE.getText()+"'");

                  dtm.addRow(v3);
                 //stmt.executeQuery("SELECT * FROM   LCI WHERE  L_ID IN(SELECT (L_ID '"+(+1)+"')FROM   LCI  WHERE  L_DATE = '"+DATE.getText()+"'");
                }
            }
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }// TODO add your handling code here:
    }   

0 个答案:

没有答案