Results 1 to 7 of 7

Thread: MySQL

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default MySQL

    Hey,
    I use this method to get the news from my mySQL database:
    Java Code:
        public void getNews() {
    
            sM("Connecting to DB", Color.BLACK);
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                sM("ERROR ON LINE 158 : " + ex.toString() + "", Color.RED);
            }
            Connection con = null;
            try {
                con = (Connection) DriverManager.getConnection("jdbc:mysql://da****t/****w", "d******", "k***");
            } catch (SQLException ex) {
                sM("ERROR ON LINE 164 : " + ex.toString() + "", Color.RED);
            }
            PreparedStatement statement = null;
            try {
                statement = (PreparedStatement) con.prepareStatement("select * from news ORDER BY `id` DESC LIMIT 0 , 1");
                sM("Getting the news", Color.BLACK);
            } catch (SQLException ex) {
                sM("ERROR ON LINE 171 : " + ex.toString() + "", Color.RED);
            }
            ResultSet result = null;
            try {
                result = statement.executeQuery();
            } catch (SQLException ex) {
                sM("ERROR ON LINE 177 : " + ex.toString() + "", Color.RED);
            }
    
            try {
                while (result.next()) {
                    newsLabel1.setText(result.getString(2));
                }
            } catch (SQLException ex) {
                sM("ERROR ON LINE 184 : " + ex.toString() + "", Color.RED);
            }
            sM("Done!", Color.GREEN);
            try {
                sM("Closing the connection", Color.RED);
                con.close();
            } catch (SQLException ex) {
                sM("ERROR ON LINE 191 : " + ex.toString() + "", Color.RED);
            }
            sM("Done!", Color.GREEN);
            
            sM("-------------------------------------------------", Color.GREEN);
        }
    When I run my program, it takes a while to load because of it, and when I call the method again it has to connect to the database again and it freezes the program. I was just wondering where can I get a better method to get the data from MySQL?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't do database code, but one problem I'm guessing you may be having is a concurrency issue with your GUI. I'm guessing (based on your other threads) that this is being done within a Swing GUI, and that you are not calling your database request code in a background thread. If this is so, then you are at risk for freezing your GUI since the GUI thread (the EDT) will need to wait for the database query and whatnot to complete before it will be able to paint itself or interact with the user.

    If I'm right, then one perceived speed improvement -- it won't speed up your database access speed but will make the GUI seem more responsive -- would be to do all database related stuff in a background thread, a SwingWorker. Please read here for more:

    Concurrency In Swing

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    I don't do database code, but one problem I'm guessing you may be having is a concurrency issue with your GUI. I'm guessing (based on your other threads) that this is being done within a Swing GUI, and that you are not calling your database request code in a background thread. If this is so, then you are at risk for freezing your GUI since the GUI thread (the EDT) will need to wait for the database query and whatnot to complete before it will be able to paint itself or interact with the user.

    If I'm right, then one perceived speed improvement -- it won't speed up your database access speed but will make the GUI seem more responsive -- would be to do all database related stuff in a background thread, a SwingWorker. Please read here for more:

    Concurrency In Swing
    This is confusing. :S :(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by PhQ View Post
    This is confusing. :S :(
    Of course it's confusing. But it will get less confusing with time. Guaranteed.

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Can I use threads for this instead?
    I've heard that threads can run multiple tasks at the same time?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by PhQ View Post
    Can I use threads for this instead?
    I've heard that threads can run multiple tasks at the same time?
    Yes you sure can, but if you do, you must take care to call most Swing things on the Swing Event Dispatch Thread (EDT). Also, please note that all a SwingWorker is is a way to do just this -- background threading, but in a way that makes it easier for the background thread to interact with Swing. There's really nothing that a SwingWorker does that can't be done with plain vanilla background threads, just you'll find that it's often easier or cleaner looking to do it with a SwingWorker.

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    Yes you sure can, but if you do, you must take care to call most Swing things on the Swing Event Dispatch Thread (EDT). Also, please note that all a SwingWorker is is a way to do just this -- background threading, but in a way that makes it easier for the background thread to interact with Swing. There's really nothing that a SwingWorker does that can't be done with plain vanilla background threads, just you'll find that it's often easier or cleaner looking to do it with a SwingWorker.
    yay, I've added it. :)
    Thanks for helping me.
    Last edited by PhQ; 04-03-2010 at 02:06 PM.

Similar Threads

  1. mysql help
    By messi_fcb in forum JDBC
    Replies: 1
    Last Post: 03-27-2010, 10:56 PM
  2. SWT and mysql
    By ashin in forum SWT / JFace
    Replies: 4
    Last Post: 07-09-2009, 05:46 AM
  3. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 02:57 AM
  4. mySQL HELP!!!
    By ace84 in forum New To Java
    Replies: 0
    Last Post: 04-19-2008, 11:39 PM
  5. JSP and MySQL
    By Ed in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-04-2007, 06:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •