Help me optimize this code!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2008
Posts: 129
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Help me optimize this code!

 
0
  #1
Jan 13th, 2009
Hey guys,
I'm creating this as an admin program for another program I've made. Basically all that happens at this point is show a bunch of buttons and the titles of the buttons are the computers that the using accounts from my MySQL database.
This is just the panel, but if I take out all my code it runs extremely fast.
  1. import javax.swing.*;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.*; //Used for JOption Panes
  7. import java.sql.Statement;// Statement class
  8. import java.io.*;
  9. import java.sql.Connection;//MySQL connection Initiate
  10. import java.sql.DriverManager;//Driver manager
  11. import java.sql.SQLException;//SQLexception class
  12. import java.sql.ResultSet;
  13. import java.net.*;
  14. import java.util.ArrayList;
  15. public class ButtonsPanel extends JPanel {
  16.  
  17. //forgot what these were for.
  18. //boolean p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17;
  19.  
  20. private JLabel banner = new JLabel("Steam Administration");
  21. private JLabel copyRight = new JLabel("All Rights Reserved \u00A9 2009 Jon Friesen");
  22. private JButton freeAll= new JButton("Free All");
  23. private JButton refresh= new JButton("Refresh");
  24. private JButton pc1= new JButton("1---"+finder(1));
  25. private JButton pc2= new JButton("2---"+finder(2));
  26. private JButton pc3= new JButton("3---"+finder(3));
  27. private JButton pc4= new JButton("4---"+finder(4));
  28. private JButton pc5= new JButton("5---"+finder(5));
  29. private JButton pc6= new JButton("6---"+finder(6));
  30. private JButton pc7= new JButton("7---"+finder(7));
  31. private JButton pc8= new JButton("8---"+finder(8));
  32. private JButton pc9= new JButton("9---"+finder(9));
  33. private JButton pc10= new JButton("10---"+finder(10));
  34. private JButton pc11= new JButton("11---"+finder(11));
  35. private JButton pc12= new JButton("12---"+finder(12));
  36. private JButton pc13= new JButton("13---"+finder(13));
  37. private JButton pc14= new JButton("14---"+finder(14));
  38. private JButton pc15= new JButton("15---"+finder(15));
  39. private JButton pc16= new JButton("16---"+finder(16));
  40. private JButton pc17= new JButton("17---"+finder(17));
  41.  
  42. public ButtonsPanel() throws SQLException{
  43. setLayout(new GridLayout(20,1));
  44.  
  45. setBorder(BorderFactory.createTitledBorder("Account List"));
  46.  
  47. //listeners
  48. pc1.addActionListener(new pc1Listener());
  49. pc2.addActionListener(new pc2Listener());
  50. pc3.addActionListener(new pc3Listener());
  51. pc4.addActionListener(new pc4Listener());
  52. pc5.addActionListener(new pc5Listener());
  53. pc6.addActionListener(new pc6Listener());
  54. pc7.addActionListener(new pc7Listener());
  55. pc8.addActionListener(new pc8Listener());
  56. pc9.addActionListener(new pc9Listener());
  57. pc10.addActionListener(new pc10Listener());
  58. pc11.addActionListener(new pc11Listener());
  59. pc12.addActionListener(new pc12Listener());
  60. pc13.addActionListener(new pc13Listener());
  61. pc14.addActionListener(new pc14Listener());
  62. pc15.addActionListener(new pc15Listener());
  63. pc16.addActionListener(new pc16Listener());
  64. pc17.addActionListener(new pc17Listener());
  65. refresh.addActionListener(new refreshListener());
  66. freeAll.addActionListener(new freeAllListener());
  67.  
  68. add(banner);
  69. add(pc1);
  70. add(pc2);
  71. add(pc3);
  72. add(pc4);
  73. add(pc5);
  74. add(pc6);
  75. add(pc7);
  76. add(pc8);
  77. add(pc9);
  78. add(pc10);
  79. add(pc11);
  80. add(pc12);
  81. add(pc13);
  82. add(pc14);
  83. add(pc15);
  84. add(pc16);
  85. add(pc17);
  86. add(freeAll);
  87. //add(refresh);
  88. add(copyRight);
  89. }
  90. private class pc1Listener implements ActionListener{
  91. public void actionPerformed(ActionEvent e)
  92. {
  93. System.exit(0);
  94. }
  95. }
  96. private class pc2Listener implements ActionListener{
  97. public void actionPerformed(ActionEvent e)
  98. {
  99. System.exit(0);
  100. }
  101. }
  102. private class pc3Listener implements ActionListener{
  103. public void actionPerformed(ActionEvent e)
  104. {
  105. System.exit(0);
  106. }
  107. }
  108. private class pc4Listener implements ActionListener{
  109. public void actionPerformed(ActionEvent e)
  110. {
  111. System.exit(0);
  112. }
  113. }
  114. private class pc5Listener implements ActionListener{
  115. public void actionPerformed(ActionEvent e)
  116. {
  117. System.exit(0);
  118. }
  119. }
  120. private class pc6Listener implements ActionListener{
  121. public void actionPerformed(ActionEvent e)
  122. {
  123. System.exit(0);
  124. }
  125. }
  126. private class pc7Listener implements ActionListener{
  127. public void actionPerformed(ActionEvent e)
  128. {
  129. System.exit(0);
  130. }
  131. }
  132. private class pc8Listener implements ActionListener{
  133. public void actionPerformed(ActionEvent e)
  134. {
  135. System.exit(0);
  136. }
  137. }
  138. private class pc9Listener implements ActionListener{
  139. public void actionPerformed(ActionEvent e)
  140. {
  141. System.exit(0);
  142. }
  143. }
  144. private class pc10Listener implements ActionListener{
  145. public void actionPerformed(ActionEvent e)
  146. {
  147. System.exit(0);
  148. }
  149. }
  150. private class pc11Listener implements ActionListener{
  151. public void actionPerformed(ActionEvent e)
  152. {
  153. System.exit(0);
  154. }
  155. }
  156. private class pc12Listener implements ActionListener{
  157. public void actionPerformed(ActionEvent e)
  158. {
  159. System.exit(0);
  160. }
  161. }
  162. private class pc13Listener implements ActionListener{
  163. public void actionPerformed(ActionEvent e)
  164. {
  165. System.exit(0);
  166. }
  167. }
  168. private class pc14Listener implements ActionListener{
  169. public void actionPerformed(ActionEvent e)
  170. {
  171. System.exit(0);
  172. }
  173. }
  174. private class pc15Listener implements ActionListener{
  175. public void actionPerformed(ActionEvent e)
  176. {
  177. System.exit(0);
  178. }
  179. }
  180. private class pc16Listener implements ActionListener{
  181. public void actionPerformed(ActionEvent e)
  182. {
  183. System.exit(0);
  184. }
  185. }
  186. private class pc17Listener implements ActionListener{
  187. public void actionPerformed(ActionEvent e)
  188. {
  189. System.exit(0);
  190. }
  191. }
  192. private class freeAllListener implements ActionListener{
  193. public void actionPerformed(ActionEvent e)
  194. {
  195. System.exit(0);
  196. }
  197. }
  198. private class refreshListener implements ActionListener{
  199. public void actionPerformed(ActionEvent e)
  200. {
  201. System.exit(0);
  202. }
  203. }
  204. /**
  205. * The finder grabs the computer that is using a certain account and returns
  206. * it so one can see when an account is in use.
  207. * @param pcNumber this is the number that is passed from the button variables
  208. * @return returns the computer name
  209. * @throws SQLException for accessing variables.
  210. */
  211. public String finder(int pcNumber) throws SQLException{
  212. Statement stmt = null;//This creates a statement variable
  213. Connection con = null; //Connection variable
  214. ResultSet rs;// result set variable
  215.  
  216. //tries to connect to the database.
  217. try {
  218. Class.forName("com.mysql.jdbc.Driver").newInstance();
  219. con = DriverManager.getConnection("jdbc:mysql://IP_ADDRESS:3306/DB", "USERNAME", "PASSWORD");
  220.  
  221. stmt = con.createStatement();
  222.  
  223. } catch(Exception e) {
  224. System.out.println("Exception: " + e.getMessage());
  225. }
  226.  
  227. //Gets everything from accounts table
  228. rs = stmt.executeQuery("SELECT * from accounts ORDER BY autoid");
  229. String computer = "";
  230. while(rs.next())
  231. {
  232. int autoId= rs.getInt("autoid");//gets rows autoid
  233. int accAva= rs.getInt("available");//gets the account availability number (0=Available or 1=Not Available)
  234. int accNum= rs.getInt("number");//gets the account number that is passed
  235. String computerName= rs.getString("computer");//gets computer name
  236.  
  237. if(accNum==pcNumber){
  238. computer = computerName;
  239. }
  240. }
  241. return computer;
  242. }
  243. /**
  244. * Refreshes all the account names.
  245. * @throws SQLException
  246. */
  247. public void Refresher() throws SQLException{
  248. Statement stmt = null;//This creates a statement variable
  249. Connection con = null; //Connection variable
  250. ResultSet rs;// result set variable
  251.  
  252. //tries to connect to the database.
  253. try {
  254. Class.forName("com.mysql.jdbc.Driver").newInstance();
  255. con = DriverManager.getConnection("jdbc:mysql://IP_ADDRESS:3306/DB", "USERNAME", "PASSWORD");
  256.  
  257. stmt = con.createStatement();
  258.  
  259. } catch(Exception e) {
  260. System.out.println("Exception: " + e.getMessage());
  261. }
  262.  
  263. //Gets everything from accounts table
  264. rs = stmt.executeQuery("SELECT * from accounts ORDER BY autoid");
  265.  
  266. while(rs.next())
  267. {
  268. int autoId= rs.getInt("autoid");//gets rows autoid
  269. int accAva= rs.getInt("available");//gets the account availability number (0=Available or 1=Not Available)
  270. int accNum= rs.getInt("number");//gets the account number that is passed
  271. String computerName= rs.getString("computer");//gets computer name
  272. for(int x=1; x<=18; x++){
  273. finder(x);
  274. }
  275.  
  276. }
  277.  
  278. }
  279. }
The reason I am posting is because this code takes almost 45seconds to run! Why is it going so slow?

Thanks PO
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,653
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Help me optimize this code!

 
1
  #2
Jan 14th, 2009
Though I don't do standalone application development using Swing, a few obvious comments.

> Class.forName("com.mysql.jdbc.Driver").newInstance(); .

You don't need to do this for every invocation. As soon as the Driver class is loaded, it registers itself with the DriverManager and that's it; no more Class.forName() required. Also, you don't need to create a new instance, just doing Class.forName() is sufficient.

> String computer = ""; .

Try to avoid this. String computer = null; is good to go.

> getConnection("jdbc:mysql://IP_ADDRESS:3306/DB", "USERNAME", "PASSWORD"); .

Don't use magic numbers/literals in your program. Either load these configurable parameters via an external resource [properties file] or at least move them to a separate class/interface as constants and refer them throughout your application.

> public void Refresher() throws SQLException .

Follow Java coding conventions; don't use uppercase characters for method names. It is a bad programming practice to expose checked exceptions to your client[given that the method is public]. Since a SQLException is almost always unrecoverable, catch it and either rethrow it wrapped in your own application specific Exception class or just log the exception for debugging/troubleshooting/analysis purposes.

You never release your database resources! You need to close the ResultSet , Statement and Connection object. Read the JDBC tutorials for more information.
  1. public void some() {
  2. Connection conn = null;
  3. PreparedStatement pStmt = null;
  4. ResultSet rs = null;
  5. try {
  6. try {
  7. //grab connection
  8. // use statement and resultset
  9. } finally {
  10. if(rs != null) rs.close();
  11. if(pStmt != null) pStmt.close();
  12. if(conn != null) conn.close();
  13. }
  14. } catch(Exception e) {
  15. e.printStackTrace();
  16. }
  17. }
It would also help if you abstracted out the common functionality of grabbing and releasing the Connection object.

The program is screaming out for the want of arrays or a collection which can hold your UI elements rather than having numbered references like pc1...pc17. Rule of thumb: if you ever find yourself doing something like a0...an, you are doing it *wrong*.

Also you don't need a dozen listener classes. Since the listeners are anyways stateless and a method invocation allocates a separate stack frame, you can create an instance of a anonymous listener class which can be used for all the UI elements.
  1. ActionListener listener = new ActionListener() {
  2. public void actionPerformed(ActionEvent e) {
  3. System.exit(0);
  4. }
  5. };
  6. for(int len = pcs.length - 1; len >= 0; --len) {
  7. pcs[len].addActionListener(listener);
  8. }
Though there might be a better/easier/shorter way of attaching listeners to UI components in the latest releases of Java, it certainly won't be something you previously had. Use the above snippet unless someone suggests something more appropriate.

You need to incorporate separation of concerns for your application; consider using the MVC architecture for UI based applications.

If you feel your application still needs a bit of tuning, consider using a connection pool.

Phew... ;-)
Last edited by ~s.o.s~; Jan 14th, 2009 at 5:13 am.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 129
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Re: Help me optimize this code!

 
0
  #3
Jan 15th, 2009
@~s.o.s~
Thanks for the help! I see you spent a lot of time. I really appreciate it, unfortunately my boss canceled the project this morning for some unknown reason, but either way I intend on finishing it on my own and making it as efficient as possible.

I know I have a lot to learn, but I'm enrolled in school and doing my best!

Thanks!
PO
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 373 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC