Using two combobox to set conditions for a search in a database and displaying in jtable












1















I need to display the details of students in a particular stream of a schoolclass in Jtable from a database containing all the names of students in the school. I have two jComboboxes, on to select which class and the other to select the stream. I am asking for a way to define these two conditions in order to display all the students in a particular stream in a jtable. I apologize in advance if my code is messy.



public Classes() {
initComponents();
show_student();
}

public Connection getConnection() {
Connection con = null;

try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sms", "root", "");
} catch(Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
return con;
}

public ArrayList<Individualclass> studentList(String ValToSearch) {
ArrayList<Individualclass> list = new
ArrayList<Individualclass>();

Statement st;
ResultSet rs;

try {
Connection con=getConnection();
st = con.createStatement();
String searchQuery = "SELECT * FROM `students` WHERE CONCAT(`firstName`, `surname`, `otherNames`, `regNo`) LIKE '%"+ValToSearch+"%'";
rs = st.executeQuery("searchQuery ");

Individualclass ic;

while(rs.next()) {
ic = new Individualclass(
rs.getString("firstName"),
rs.getString("surname"),
rs.getString("otherNames"),
rs.getInt("regNo")

);
list.add(ic);
}
} catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
return list;
}

public void findStudents() {

}

private void sClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sClassActionPerformed
try {
Connection con = getConnection();
String fetch_row = "SELECT * FROM students where sClass=?";
PreparedStatement pst = con.prepareStatement(fetch_row);
pst.setString(1, (String) sClass.getSelectedItem());
ResultSet rs = pst.executeQuery();

while(rs.next()) {
Individualclass ic = new Individualclass(rs.getString("firstName"),rs.getString("surname"),rs.getString("otherNames"),rs.getInt("regNo"));
}
} catch(Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}//GEN-LAST:event_sClassActionPerformed

private void streamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_streamActionPerformed
try {
Connection con = getConnection();
String fetch_row = "SELECT * FROM students where stream=?";
PreparedStatement pst = con.prepareStatement(fetch_row);
pst.setString(1, (String)stream.getSelectedItem());
ResultSet rs = pst.executeQuery();

while(rs.next()) {
Individualclass ic = new Individualclass(rs.getString("firstName"),rs.getString("surname"),rs.getString("otherNames"),rs.getInt("regNo"));
}
} catch(Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}//GEN-LAST:event_streamActionPerformed

public void show_student() {
ArrayList<Individualclass> list = new ArrayList<Individualclass>();
DefaultTableModel model = (DefaultTableModel)jTable_Display_Student.getModel();
Object row = new Object[13];
for(int i = 0; i < list.size(); i++) {
row[0] = list.get(i).getFirstName();
row[1] = list.get(i).getsurname();
row[2] = list.get(i).getOtherNames();
row[3] = list.get(i).getregNo();

model.addRow(row);
}
}









share|improve this question





























    1















    I need to display the details of students in a particular stream of a schoolclass in Jtable from a database containing all the names of students in the school. I have two jComboboxes, on to select which class and the other to select the stream. I am asking for a way to define these two conditions in order to display all the students in a particular stream in a jtable. I apologize in advance if my code is messy.



    public Classes() {
    initComponents();
    show_student();
    }

    public Connection getConnection() {
    Connection con = null;

    try {
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sms", "root", "");
    } catch(Exception e) {
    JOptionPane.showMessageDialog(null, e.getMessage());
    }
    return con;
    }

    public ArrayList<Individualclass> studentList(String ValToSearch) {
    ArrayList<Individualclass> list = new
    ArrayList<Individualclass>();

    Statement st;
    ResultSet rs;

    try {
    Connection con=getConnection();
    st = con.createStatement();
    String searchQuery = "SELECT * FROM `students` WHERE CONCAT(`firstName`, `surname`, `otherNames`, `regNo`) LIKE '%"+ValToSearch+"%'";
    rs = st.executeQuery("searchQuery ");

    Individualclass ic;

    while(rs.next()) {
    ic = new Individualclass(
    rs.getString("firstName"),
    rs.getString("surname"),
    rs.getString("otherNames"),
    rs.getInt("regNo")

    );
    list.add(ic);
    }
    } catch(Exception ex){
    JOptionPane.showMessageDialog(null, ex.getMessage());
    }
    return list;
    }

    public void findStudents() {

    }

    private void sClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sClassActionPerformed
    try {
    Connection con = getConnection();
    String fetch_row = "SELECT * FROM students where sClass=?";
    PreparedStatement pst = con.prepareStatement(fetch_row);
    pst.setString(1, (String) sClass.getSelectedItem());
    ResultSet rs = pst.executeQuery();

    while(rs.next()) {
    Individualclass ic = new Individualclass(rs.getString("firstName"),rs.getString("surname"),rs.getString("otherNames"),rs.getInt("regNo"));
    }
    } catch(Exception ex) {
    JOptionPane.showMessageDialog(null, ex.getMessage());
    }
    }//GEN-LAST:event_sClassActionPerformed

    private void streamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_streamActionPerformed
    try {
    Connection con = getConnection();
    String fetch_row = "SELECT * FROM students where stream=?";
    PreparedStatement pst = con.prepareStatement(fetch_row);
    pst.setString(1, (String)stream.getSelectedItem());
    ResultSet rs = pst.executeQuery();

    while(rs.next()) {
    Individualclass ic = new Individualclass(rs.getString("firstName"),rs.getString("surname"),rs.getString("otherNames"),rs.getInt("regNo"));
    }
    } catch(Exception ex) {
    JOptionPane.showMessageDialog(null, ex.getMessage());
    }
    }//GEN-LAST:event_streamActionPerformed

    public void show_student() {
    ArrayList<Individualclass> list = new ArrayList<Individualclass>();
    DefaultTableModel model = (DefaultTableModel)jTable_Display_Student.getModel();
    Object row = new Object[13];
    for(int i = 0; i < list.size(); i++) {
    row[0] = list.get(i).getFirstName();
    row[1] = list.get(i).getsurname();
    row[2] = list.get(i).getOtherNames();
    row[3] = list.get(i).getregNo();

    model.addRow(row);
    }
    }









    share|improve this question



























      1












      1








      1








      I need to display the details of students in a particular stream of a schoolclass in Jtable from a database containing all the names of students in the school. I have two jComboboxes, on to select which class and the other to select the stream. I am asking for a way to define these two conditions in order to display all the students in a particular stream in a jtable. I apologize in advance if my code is messy.



      public Classes() {
      initComponents();
      show_student();
      }

      public Connection getConnection() {
      Connection con = null;

      try {
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sms", "root", "");
      } catch(Exception e) {
      JOptionPane.showMessageDialog(null, e.getMessage());
      }
      return con;
      }

      public ArrayList<Individualclass> studentList(String ValToSearch) {
      ArrayList<Individualclass> list = new
      ArrayList<Individualclass>();

      Statement st;
      ResultSet rs;

      try {
      Connection con=getConnection();
      st = con.createStatement();
      String searchQuery = "SELECT * FROM `students` WHERE CONCAT(`firstName`, `surname`, `otherNames`, `regNo`) LIKE '%"+ValToSearch+"%'";
      rs = st.executeQuery("searchQuery ");

      Individualclass ic;

      while(rs.next()) {
      ic = new Individualclass(
      rs.getString("firstName"),
      rs.getString("surname"),
      rs.getString("otherNames"),
      rs.getInt("regNo")

      );
      list.add(ic);
      }
      } catch(Exception ex){
      JOptionPane.showMessageDialog(null, ex.getMessage());
      }
      return list;
      }

      public void findStudents() {

      }

      private void sClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sClassActionPerformed
      try {
      Connection con = getConnection();
      String fetch_row = "SELECT * FROM students where sClass=?";
      PreparedStatement pst = con.prepareStatement(fetch_row);
      pst.setString(1, (String) sClass.getSelectedItem());
      ResultSet rs = pst.executeQuery();

      while(rs.next()) {
      Individualclass ic = new Individualclass(rs.getString("firstName"),rs.getString("surname"),rs.getString("otherNames"),rs.getInt("regNo"));
      }
      } catch(Exception ex) {
      JOptionPane.showMessageDialog(null, ex.getMessage());
      }
      }//GEN-LAST:event_sClassActionPerformed

      private void streamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_streamActionPerformed
      try {
      Connection con = getConnection();
      String fetch_row = "SELECT * FROM students where stream=?";
      PreparedStatement pst = con.prepareStatement(fetch_row);
      pst.setString(1, (String)stream.getSelectedItem());
      ResultSet rs = pst.executeQuery();

      while(rs.next()) {
      Individualclass ic = new Individualclass(rs.getString("firstName"),rs.getString("surname"),rs.getString("otherNames"),rs.getInt("regNo"));
      }
      } catch(Exception ex) {
      JOptionPane.showMessageDialog(null, ex.getMessage());
      }
      }//GEN-LAST:event_streamActionPerformed

      public void show_student() {
      ArrayList<Individualclass> list = new ArrayList<Individualclass>();
      DefaultTableModel model = (DefaultTableModel)jTable_Display_Student.getModel();
      Object row = new Object[13];
      for(int i = 0; i < list.size(); i++) {
      row[0] = list.get(i).getFirstName();
      row[1] = list.get(i).getsurname();
      row[2] = list.get(i).getOtherNames();
      row[3] = list.get(i).getregNo();

      model.addRow(row);
      }
      }









      share|improve this question
















      I need to display the details of students in a particular stream of a schoolclass in Jtable from a database containing all the names of students in the school. I have two jComboboxes, on to select which class and the other to select the stream. I am asking for a way to define these two conditions in order to display all the students in a particular stream in a jtable. I apologize in advance if my code is messy.



      public Classes() {
      initComponents();
      show_student();
      }

      public Connection getConnection() {
      Connection con = null;

      try {
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sms", "root", "");
      } catch(Exception e) {
      JOptionPane.showMessageDialog(null, e.getMessage());
      }
      return con;
      }

      public ArrayList<Individualclass> studentList(String ValToSearch) {
      ArrayList<Individualclass> list = new
      ArrayList<Individualclass>();

      Statement st;
      ResultSet rs;

      try {
      Connection con=getConnection();
      st = con.createStatement();
      String searchQuery = "SELECT * FROM `students` WHERE CONCAT(`firstName`, `surname`, `otherNames`, `regNo`) LIKE '%"+ValToSearch+"%'";
      rs = st.executeQuery("searchQuery ");

      Individualclass ic;

      while(rs.next()) {
      ic = new Individualclass(
      rs.getString("firstName"),
      rs.getString("surname"),
      rs.getString("otherNames"),
      rs.getInt("regNo")

      );
      list.add(ic);
      }
      } catch(Exception ex){
      JOptionPane.showMessageDialog(null, ex.getMessage());
      }
      return list;
      }

      public void findStudents() {

      }

      private void sClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sClassActionPerformed
      try {
      Connection con = getConnection();
      String fetch_row = "SELECT * FROM students where sClass=?";
      PreparedStatement pst = con.prepareStatement(fetch_row);
      pst.setString(1, (String) sClass.getSelectedItem());
      ResultSet rs = pst.executeQuery();

      while(rs.next()) {
      Individualclass ic = new Individualclass(rs.getString("firstName"),rs.getString("surname"),rs.getString("otherNames"),rs.getInt("regNo"));
      }
      } catch(Exception ex) {
      JOptionPane.showMessageDialog(null, ex.getMessage());
      }
      }//GEN-LAST:event_sClassActionPerformed

      private void streamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_streamActionPerformed
      try {
      Connection con = getConnection();
      String fetch_row = "SELECT * FROM students where stream=?";
      PreparedStatement pst = con.prepareStatement(fetch_row);
      pst.setString(1, (String)stream.getSelectedItem());
      ResultSet rs = pst.executeQuery();

      while(rs.next()) {
      Individualclass ic = new Individualclass(rs.getString("firstName"),rs.getString("surname"),rs.getString("otherNames"),rs.getInt("regNo"));
      }
      } catch(Exception ex) {
      JOptionPane.showMessageDialog(null, ex.getMessage());
      }
      }//GEN-LAST:event_streamActionPerformed

      public void show_student() {
      ArrayList<Individualclass> list = new ArrayList<Individualclass>();
      DefaultTableModel model = (DefaultTableModel)jTable_Display_Student.getModel();
      Object row = new Object[13];
      for(int i = 0; i < list.size(); i++) {
      row[0] = list.get(i).getFirstName();
      row[1] = list.get(i).getsurname();
      row[2] = list.get(i).getOtherNames();
      row[3] = list.get(i).getregNo();

      model.addRow(row);
      }
      }






      java mysql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 10:36









      deHaar

      2,45451628




      2,45451628










      asked Nov 23 '18 at 7:43









      Cliffton afandeCliffton afande

      21




      21
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442507%2fusing-two-combobox-to-set-conditions-for-a-search-in-a-database-and-displaying-i%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442507%2fusing-two-combobox-to-set-conditions-for-a-search-in-a-database-and-displaying-i%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin