Results 1 to 7 of 7
Thread: MySQL
- 04-02-2010, 11:52 PM #1
MySQL
Hey,
I use this method to get the news from my mySQL database:
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?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); }
-
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
- 04-03-2010, 01:23 AM #3
-
- 04-03-2010, 02:36 AM #5
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.
- 04-03-2010, 12:54 PM #7
Similar Threads
-
mysql help
By messi_fcb in forum JDBCReplies: 1Last Post: 03-27-2010, 09:56 PM -
SWT and mysql
By ashin in forum SWT / JFaceReplies: 4Last Post: 07-09-2009, 04:46 AM -
MySQL/JDBC Mysql query output
By thelinuxguy in forum Advanced JavaReplies: 4Last Post: 02-13-2009, 01:57 AM -
mySQL HELP!!!
By ace84 in forum New To JavaReplies: 0Last Post: 04-19-2008, 10:39 PM -
JSP and MySQL
By Ed in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 07-04-2007, 05:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks