Refresh loop for chat program using Java and MySQL

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I wrote a chat program in Java/MySQL and did the refreshing part of the chat frame with this code:



//Thats the connecting part and reading out of the consisting data base
sq = new sqlCom("db4free.net","3306");
ResultSet r = sq.executeQuery("select * from chat where chat.sichtbar = 1");
i = 0;
try
while(r.next())
jTextArea1.append(r.getString("chat.users")+" : "+r.getString("chat.kommentar")+"n");
i++;


catch(Exception e)
System.out.println(e);

//Here does the "refreshing" starts
Thread th = new Thread()
public void run()
while(sq.isConnected())
ResultSet r = sq.executeQuery("select * from chat where chat.sichtbar = 1");
for(int x = 0 ;x<i;x++)
try
r.next();

catch(Exception e)
System.out.println(e);


try
while(r.next())
jTextArea1.append(r.getString("chat.users")+" : "+r.getString("chat.kommentar")+"n");
i++;


catch(Exception e)
System.out.println(e);



;
th.start();
}


I am pretty sure this isn't a good way of doing this. I already had the idea of doing this with an observer where it sends a signal to the other clients to refresh. So I wanted to ask if this, how I did it, is a way of doing it or if I have to change something.







share|improve this question



























    up vote
    0
    down vote

    favorite












    I wrote a chat program in Java/MySQL and did the refreshing part of the chat frame with this code:



    //Thats the connecting part and reading out of the consisting data base
    sq = new sqlCom("db4free.net","3306");
    ResultSet r = sq.executeQuery("select * from chat where chat.sichtbar = 1");
    i = 0;
    try
    while(r.next())
    jTextArea1.append(r.getString("chat.users")+" : "+r.getString("chat.kommentar")+"n");
    i++;


    catch(Exception e)
    System.out.println(e);

    //Here does the "refreshing" starts
    Thread th = new Thread()
    public void run()
    while(sq.isConnected())
    ResultSet r = sq.executeQuery("select * from chat where chat.sichtbar = 1");
    for(int x = 0 ;x<i;x++)
    try
    r.next();

    catch(Exception e)
    System.out.println(e);


    try
    while(r.next())
    jTextArea1.append(r.getString("chat.users")+" : "+r.getString("chat.kommentar")+"n");
    i++;


    catch(Exception e)
    System.out.println(e);



    ;
    th.start();
    }


    I am pretty sure this isn't a good way of doing this. I already had the idea of doing this with an observer where it sends a signal to the other clients to refresh. So I wanted to ask if this, how I did it, is a way of doing it or if I have to change something.







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I wrote a chat program in Java/MySQL and did the refreshing part of the chat frame with this code:



      //Thats the connecting part and reading out of the consisting data base
      sq = new sqlCom("db4free.net","3306");
      ResultSet r = sq.executeQuery("select * from chat where chat.sichtbar = 1");
      i = 0;
      try
      while(r.next())
      jTextArea1.append(r.getString("chat.users")+" : "+r.getString("chat.kommentar")+"n");
      i++;


      catch(Exception e)
      System.out.println(e);

      //Here does the "refreshing" starts
      Thread th = new Thread()
      public void run()
      while(sq.isConnected())
      ResultSet r = sq.executeQuery("select * from chat where chat.sichtbar = 1");
      for(int x = 0 ;x<i;x++)
      try
      r.next();

      catch(Exception e)
      System.out.println(e);


      try
      while(r.next())
      jTextArea1.append(r.getString("chat.users")+" : "+r.getString("chat.kommentar")+"n");
      i++;


      catch(Exception e)
      System.out.println(e);



      ;
      th.start();
      }


      I am pretty sure this isn't a good way of doing this. I already had the idea of doing this with an observer where it sends a signal to the other clients to refresh. So I wanted to ask if this, how I did it, is a way of doing it or if I have to change something.







      share|improve this question













      I wrote a chat program in Java/MySQL and did the refreshing part of the chat frame with this code:



      //Thats the connecting part and reading out of the consisting data base
      sq = new sqlCom("db4free.net","3306");
      ResultSet r = sq.executeQuery("select * from chat where chat.sichtbar = 1");
      i = 0;
      try
      while(r.next())
      jTextArea1.append(r.getString("chat.users")+" : "+r.getString("chat.kommentar")+"n");
      i++;


      catch(Exception e)
      System.out.println(e);

      //Here does the "refreshing" starts
      Thread th = new Thread()
      public void run()
      while(sq.isConnected())
      ResultSet r = sq.executeQuery("select * from chat where chat.sichtbar = 1");
      for(int x = 0 ;x<i;x++)
      try
      r.next();

      catch(Exception e)
      System.out.println(e);


      try
      while(r.next())
      jTextArea1.append(r.getString("chat.users")+" : "+r.getString("chat.kommentar")+"n");
      i++;


      catch(Exception e)
      System.out.println(e);



      ;
      th.start();
      }


      I am pretty sure this isn't a good way of doing this. I already had the idea of doing this with an observer where it sends a signal to the other clients to refresh. So I wanted to ask if this, how I did it, is a way of doing it or if I have to change something.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jan 26 at 1:25









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Jan 25 at 22:35









      MaestroD

      61




      61

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186010%2frefresh-loop-for-chat-program-using-java-and-mysql%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186010%2frefresh-loop-for-chat-program-using-java-and-mysql%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          ADO Stream Object