How to select item name from jList in netbeans?












0















i have been trying to retrive data from mysql in the list and that is working fine but the problem is i'm not able to select the name of the list..
i want to select name (not index or value) so that i can complete my where clause



Here is my Code :-



private void proceed(){

PreparedStatement stmt = null;
Connection conn = null;
ResultSet rs=null;

String i = jList1.getSelectedValue();




try {

Class.forName("java.sql.DriverManager");

conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");

stmt = conn.prepareStatement("select * from hotelbookings where GuestName = '"+i+"'");

rs = stmt.executeQuery();

if (rs.next()){

String BN = rs.getString("BookNo");
String GN = rs.getString("GuestName");
String AD = rs.getString("Address");
String NOD = rs.getString("No_of_Days");
String PN = rs.getString("PhoneNo");
String ID = rs.getString("ID_Proof");
String CN = rs.getString("Country");
String ARD = rs.getString("Arival_Date");
String DRD = rs.getString("Departure_Date");

NewJFrame1_1 second = new NewJFrame1_1(BN,GN,AD,NOD,PN,ID,CN,ARD,DRD);
second.setVisible(true);

}

} catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}


}


Here is the image of my list ..Show List retreives the data from mysql ..and proceed button is used to take the selected name for modification



Here is The code for Show List Button:-



private void fillList(){

PreparedStatement stmt = null;
Connection conn = null;


try {
Class.forName("java.sql.DriverManager");

conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");

stmt = conn.prepareStatement("select GuestName from hotelbookings");

stmt.executeQuery();

ResultSet rs = stmt.getResultSet();
int i =0;
DefaultListModel info = new DefaultListModel();

while (rs.next()){
String data = new String[100];
data[i] = rs.getString("GuestName");
jList1.setModel(info);
info.addElement(data[i]);
i = i + 1;
jList1 = new JList(info);
}

}
catch(Exception e){

JOptionPane.showMessageDialog (this, e.getMessage());
}




}


fillList class:-



private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {



     fillList();


}










share|improve this question





























    0















    i have been trying to retrive data from mysql in the list and that is working fine but the problem is i'm not able to select the name of the list..
    i want to select name (not index or value) so that i can complete my where clause



    Here is my Code :-



    private void proceed(){

    PreparedStatement stmt = null;
    Connection conn = null;
    ResultSet rs=null;

    String i = jList1.getSelectedValue();




    try {

    Class.forName("java.sql.DriverManager");

    conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");

    stmt = conn.prepareStatement("select * from hotelbookings where GuestName = '"+i+"'");

    rs = stmt.executeQuery();

    if (rs.next()){

    String BN = rs.getString("BookNo");
    String GN = rs.getString("GuestName");
    String AD = rs.getString("Address");
    String NOD = rs.getString("No_of_Days");
    String PN = rs.getString("PhoneNo");
    String ID = rs.getString("ID_Proof");
    String CN = rs.getString("Country");
    String ARD = rs.getString("Arival_Date");
    String DRD = rs.getString("Departure_Date");

    NewJFrame1_1 second = new NewJFrame1_1(BN,GN,AD,NOD,PN,ID,CN,ARD,DRD);
    second.setVisible(true);

    }

    } catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
    }


    }


    Here is the image of my list ..Show List retreives the data from mysql ..and proceed button is used to take the selected name for modification



    Here is The code for Show List Button:-



    private void fillList(){

    PreparedStatement stmt = null;
    Connection conn = null;


    try {
    Class.forName("java.sql.DriverManager");

    conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");

    stmt = conn.prepareStatement("select GuestName from hotelbookings");

    stmt.executeQuery();

    ResultSet rs = stmt.getResultSet();
    int i =0;
    DefaultListModel info = new DefaultListModel();

    while (rs.next()){
    String data = new String[100];
    data[i] = rs.getString("GuestName");
    jList1.setModel(info);
    info.addElement(data[i]);
    i = i + 1;
    jList1 = new JList(info);
    }

    }
    catch(Exception e){

    JOptionPane.showMessageDialog (this, e.getMessage());
    }




    }


    fillList class:-



    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {



         fillList();


    }










    share|improve this question



























      0












      0








      0


      0






      i have been trying to retrive data from mysql in the list and that is working fine but the problem is i'm not able to select the name of the list..
      i want to select name (not index or value) so that i can complete my where clause



      Here is my Code :-



      private void proceed(){

      PreparedStatement stmt = null;
      Connection conn = null;
      ResultSet rs=null;

      String i = jList1.getSelectedValue();




      try {

      Class.forName("java.sql.DriverManager");

      conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");

      stmt = conn.prepareStatement("select * from hotelbookings where GuestName = '"+i+"'");

      rs = stmt.executeQuery();

      if (rs.next()){

      String BN = rs.getString("BookNo");
      String GN = rs.getString("GuestName");
      String AD = rs.getString("Address");
      String NOD = rs.getString("No_of_Days");
      String PN = rs.getString("PhoneNo");
      String ID = rs.getString("ID_Proof");
      String CN = rs.getString("Country");
      String ARD = rs.getString("Arival_Date");
      String DRD = rs.getString("Departure_Date");

      NewJFrame1_1 second = new NewJFrame1_1(BN,GN,AD,NOD,PN,ID,CN,ARD,DRD);
      second.setVisible(true);

      }

      } catch(Exception e){
      JOptionPane.showMessageDialog(null, e);
      }


      }


      Here is the image of my list ..Show List retreives the data from mysql ..and proceed button is used to take the selected name for modification



      Here is The code for Show List Button:-



      private void fillList(){

      PreparedStatement stmt = null;
      Connection conn = null;


      try {
      Class.forName("java.sql.DriverManager");

      conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");

      stmt = conn.prepareStatement("select GuestName from hotelbookings");

      stmt.executeQuery();

      ResultSet rs = stmt.getResultSet();
      int i =0;
      DefaultListModel info = new DefaultListModel();

      while (rs.next()){
      String data = new String[100];
      data[i] = rs.getString("GuestName");
      jList1.setModel(info);
      info.addElement(data[i]);
      i = i + 1;
      jList1 = new JList(info);
      }

      }
      catch(Exception e){

      JOptionPane.showMessageDialog (this, e.getMessage());
      }




      }


      fillList class:-



      private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {



           fillList();


      }










      share|improve this question
















      i have been trying to retrive data from mysql in the list and that is working fine but the problem is i'm not able to select the name of the list..
      i want to select name (not index or value) so that i can complete my where clause



      Here is my Code :-



      private void proceed(){

      PreparedStatement stmt = null;
      Connection conn = null;
      ResultSet rs=null;

      String i = jList1.getSelectedValue();




      try {

      Class.forName("java.sql.DriverManager");

      conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");

      stmt = conn.prepareStatement("select * from hotelbookings where GuestName = '"+i+"'");

      rs = stmt.executeQuery();

      if (rs.next()){

      String BN = rs.getString("BookNo");
      String GN = rs.getString("GuestName");
      String AD = rs.getString("Address");
      String NOD = rs.getString("No_of_Days");
      String PN = rs.getString("PhoneNo");
      String ID = rs.getString("ID_Proof");
      String CN = rs.getString("Country");
      String ARD = rs.getString("Arival_Date");
      String DRD = rs.getString("Departure_Date");

      NewJFrame1_1 second = new NewJFrame1_1(BN,GN,AD,NOD,PN,ID,CN,ARD,DRD);
      second.setVisible(true);

      }

      } catch(Exception e){
      JOptionPane.showMessageDialog(null, e);
      }


      }


      Here is the image of my list ..Show List retreives the data from mysql ..and proceed button is used to take the selected name for modification



      Here is The code for Show List Button:-



      private void fillList(){

      PreparedStatement stmt = null;
      Connection conn = null;


      try {
      Class.forName("java.sql.DriverManager");

      conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");

      stmt = conn.prepareStatement("select GuestName from hotelbookings");

      stmt.executeQuery();

      ResultSet rs = stmt.getResultSet();
      int i =0;
      DefaultListModel info = new DefaultListModel();

      while (rs.next()){
      String data = new String[100];
      data[i] = rs.getString("GuestName");
      jList1.setModel(info);
      info.addElement(data[i]);
      i = i + 1;
      jList1 = new JList(info);
      }

      }
      catch(Exception e){

      JOptionPane.showMessageDialog (this, e.getMessage());
      }




      }


      fillList class:-



      private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {



           fillList();


      }







      java mysql database netbeans jlist






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 11:01







      Shivii44

















      asked Jan 2 at 13:23









      Shivii44Shivii44

      13




      13
























          1 Answer
          1






          active

          oldest

          votes


















          0














          private void fillList(){

          PreparedStatement stmt = null;
          Connection conn = null;

          try {
          Class.forName("java.sql.DriverManager");
          conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");
          stmt = conn.prepareStatement("select GuestName from hotelbookings");
          stmt.executeQuery();
          ResultSet rs = stmt.getResultSet();
          int i =0;
          DefaultListModel info = new DefaultListModel();

          while (rs.next()){
          String data = new String[100];
          data[i] = rs.getString("GuestName");
          info.addElement(data[i]);
          i = i + 1;
          }
          jList1 = new JList(info);
          } catch(Exception e){
          JOptionPane.showMessageDialog (this, e.getMessage());
          }

          }





          share|improve this answer


























          • not working ...

            – Shivii44
            Jan 3 at 5:18











          • i have already created list by retriving data from mysql .. but i'm not able to select the name ... from the list ... thanks in advance ..any further help is appreciated ..

            – Shivii44
            Jan 3 at 5:23











          • firstly you select the row(name) in jList1 in user interface of your application. Then press button to call the proceed() method

            – Rathnayake
            Jan 3 at 5:49











          • yahh ...i did exactly same .. but no help... i have attached an image ..see that

            – Shivii44
            Jan 3 at 6:09













          • @Shivii44 please try with above edited answer

            – Rathnayake
            Jan 3 at 8:52













          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%2f54007171%2fhow-to-select-item-name-from-jlist-in-netbeans%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          private void fillList(){

          PreparedStatement stmt = null;
          Connection conn = null;

          try {
          Class.forName("java.sql.DriverManager");
          conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");
          stmt = conn.prepareStatement("select GuestName from hotelbookings");
          stmt.executeQuery();
          ResultSet rs = stmt.getResultSet();
          int i =0;
          DefaultListModel info = new DefaultListModel();

          while (rs.next()){
          String data = new String[100];
          data[i] = rs.getString("GuestName");
          info.addElement(data[i]);
          i = i + 1;
          }
          jList1 = new JList(info);
          } catch(Exception e){
          JOptionPane.showMessageDialog (this, e.getMessage());
          }

          }





          share|improve this answer


























          • not working ...

            – Shivii44
            Jan 3 at 5:18











          • i have already created list by retriving data from mysql .. but i'm not able to select the name ... from the list ... thanks in advance ..any further help is appreciated ..

            – Shivii44
            Jan 3 at 5:23











          • firstly you select the row(name) in jList1 in user interface of your application. Then press button to call the proceed() method

            – Rathnayake
            Jan 3 at 5:49











          • yahh ...i did exactly same .. but no help... i have attached an image ..see that

            – Shivii44
            Jan 3 at 6:09













          • @Shivii44 please try with above edited answer

            – Rathnayake
            Jan 3 at 8:52


















          0














          private void fillList(){

          PreparedStatement stmt = null;
          Connection conn = null;

          try {
          Class.forName("java.sql.DriverManager");
          conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");
          stmt = conn.prepareStatement("select GuestName from hotelbookings");
          stmt.executeQuery();
          ResultSet rs = stmt.getResultSet();
          int i =0;
          DefaultListModel info = new DefaultListModel();

          while (rs.next()){
          String data = new String[100];
          data[i] = rs.getString("GuestName");
          info.addElement(data[i]);
          i = i + 1;
          }
          jList1 = new JList(info);
          } catch(Exception e){
          JOptionPane.showMessageDialog (this, e.getMessage());
          }

          }





          share|improve this answer


























          • not working ...

            – Shivii44
            Jan 3 at 5:18











          • i have already created list by retriving data from mysql .. but i'm not able to select the name ... from the list ... thanks in advance ..any further help is appreciated ..

            – Shivii44
            Jan 3 at 5:23











          • firstly you select the row(name) in jList1 in user interface of your application. Then press button to call the proceed() method

            – Rathnayake
            Jan 3 at 5:49











          • yahh ...i did exactly same .. but no help... i have attached an image ..see that

            – Shivii44
            Jan 3 at 6:09













          • @Shivii44 please try with above edited answer

            – Rathnayake
            Jan 3 at 8:52
















          0












          0








          0







          private void fillList(){

          PreparedStatement stmt = null;
          Connection conn = null;

          try {
          Class.forName("java.sql.DriverManager");
          conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");
          stmt = conn.prepareStatement("select GuestName from hotelbookings");
          stmt.executeQuery();
          ResultSet rs = stmt.getResultSet();
          int i =0;
          DefaultListModel info = new DefaultListModel();

          while (rs.next()){
          String data = new String[100];
          data[i] = rs.getString("GuestName");
          info.addElement(data[i]);
          i = i + 1;
          }
          jList1 = new JList(info);
          } catch(Exception e){
          JOptionPane.showMessageDialog (this, e.getMessage());
          }

          }





          share|improve this answer















          private void fillList(){

          PreparedStatement stmt = null;
          Connection conn = null;

          try {
          Class.forName("java.sql.DriverManager");
          conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");
          stmt = conn.prepareStatement("select GuestName from hotelbookings");
          stmt.executeQuery();
          ResultSet rs = stmt.getResultSet();
          int i =0;
          DefaultListModel info = new DefaultListModel();

          while (rs.next()){
          String data = new String[100];
          data[i] = rs.getString("GuestName");
          info.addElement(data[i]);
          i = i + 1;
          }
          jList1 = new JList(info);
          } catch(Exception e){
          JOptionPane.showMessageDialog (this, e.getMessage());
          }

          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 4 at 8:12

























          answered Jan 2 at 17:42









          RathnayakeRathnayake

          256314




          256314













          • not working ...

            – Shivii44
            Jan 3 at 5:18











          • i have already created list by retriving data from mysql .. but i'm not able to select the name ... from the list ... thanks in advance ..any further help is appreciated ..

            – Shivii44
            Jan 3 at 5:23











          • firstly you select the row(name) in jList1 in user interface of your application. Then press button to call the proceed() method

            – Rathnayake
            Jan 3 at 5:49











          • yahh ...i did exactly same .. but no help... i have attached an image ..see that

            – Shivii44
            Jan 3 at 6:09













          • @Shivii44 please try with above edited answer

            – Rathnayake
            Jan 3 at 8:52





















          • not working ...

            – Shivii44
            Jan 3 at 5:18











          • i have already created list by retriving data from mysql .. but i'm not able to select the name ... from the list ... thanks in advance ..any further help is appreciated ..

            – Shivii44
            Jan 3 at 5:23











          • firstly you select the row(name) in jList1 in user interface of your application. Then press button to call the proceed() method

            – Rathnayake
            Jan 3 at 5:49











          • yahh ...i did exactly same .. but no help... i have attached an image ..see that

            – Shivii44
            Jan 3 at 6:09













          • @Shivii44 please try with above edited answer

            – Rathnayake
            Jan 3 at 8:52



















          not working ...

          – Shivii44
          Jan 3 at 5:18





          not working ...

          – Shivii44
          Jan 3 at 5:18













          i have already created list by retriving data from mysql .. but i'm not able to select the name ... from the list ... thanks in advance ..any further help is appreciated ..

          – Shivii44
          Jan 3 at 5:23





          i have already created list by retriving data from mysql .. but i'm not able to select the name ... from the list ... thanks in advance ..any further help is appreciated ..

          – Shivii44
          Jan 3 at 5:23













          firstly you select the row(name) in jList1 in user interface of your application. Then press button to call the proceed() method

          – Rathnayake
          Jan 3 at 5:49





          firstly you select the row(name) in jList1 in user interface of your application. Then press button to call the proceed() method

          – Rathnayake
          Jan 3 at 5:49













          yahh ...i did exactly same .. but no help... i have attached an image ..see that

          – Shivii44
          Jan 3 at 6:09







          yahh ...i did exactly same .. but no help... i have attached an image ..see that

          – Shivii44
          Jan 3 at 6:09















          @Shivii44 please try with above edited answer

          – Rathnayake
          Jan 3 at 8:52







          @Shivii44 please try with above edited answer

          – Rathnayake
          Jan 3 at 8:52






















          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%2f54007171%2fhow-to-select-item-name-from-jlist-in-netbeans%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

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas