| | |
Help me optimize this code!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
The reason I am posting is because this code takes almost 45seconds to run! Why is it going so slow?
Thanks PO
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.
java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; //Used for JOption Panes import java.sql.Statement;// Statement class import java.io.*; import java.sql.Connection;//MySQL connection Initiate import java.sql.DriverManager;//Driver manager import java.sql.SQLException;//SQLexception class import java.sql.ResultSet; import java.net.*; import java.util.ArrayList; public class ButtonsPanel extends JPanel { //forgot what these were for. //boolean p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17; private JLabel banner = new JLabel("Steam Administration"); private JLabel copyRight = new JLabel("All Rights Reserved \u00A9 2009 Jon Friesen"); private JButton freeAll= new JButton("Free All"); private JButton refresh= new JButton("Refresh"); private JButton pc1= new JButton("1---"+finder(1)); private JButton pc2= new JButton("2---"+finder(2)); private JButton pc3= new JButton("3---"+finder(3)); private JButton pc4= new JButton("4---"+finder(4)); private JButton pc5= new JButton("5---"+finder(5)); private JButton pc6= new JButton("6---"+finder(6)); private JButton pc7= new JButton("7---"+finder(7)); private JButton pc8= new JButton("8---"+finder(8)); private JButton pc9= new JButton("9---"+finder(9)); private JButton pc10= new JButton("10---"+finder(10)); private JButton pc11= new JButton("11---"+finder(11)); private JButton pc12= new JButton("12---"+finder(12)); private JButton pc13= new JButton("13---"+finder(13)); private JButton pc14= new JButton("14---"+finder(14)); private JButton pc15= new JButton("15---"+finder(15)); private JButton pc16= new JButton("16---"+finder(16)); private JButton pc17= new JButton("17---"+finder(17)); public ButtonsPanel() throws SQLException{ setLayout(new GridLayout(20,1)); setBorder(BorderFactory.createTitledBorder("Account List")); //listeners pc1.addActionListener(new pc1Listener()); pc2.addActionListener(new pc2Listener()); pc3.addActionListener(new pc3Listener()); pc4.addActionListener(new pc4Listener()); pc5.addActionListener(new pc5Listener()); pc6.addActionListener(new pc6Listener()); pc7.addActionListener(new pc7Listener()); pc8.addActionListener(new pc8Listener()); pc9.addActionListener(new pc9Listener()); pc10.addActionListener(new pc10Listener()); pc11.addActionListener(new pc11Listener()); pc12.addActionListener(new pc12Listener()); pc13.addActionListener(new pc13Listener()); pc14.addActionListener(new pc14Listener()); pc15.addActionListener(new pc15Listener()); pc16.addActionListener(new pc16Listener()); pc17.addActionListener(new pc17Listener()); refresh.addActionListener(new refreshListener()); freeAll.addActionListener(new freeAllListener()); add(banner); add(pc1); add(pc2); add(pc3); add(pc4); add(pc5); add(pc6); add(pc7); add(pc8); add(pc9); add(pc10); add(pc11); add(pc12); add(pc13); add(pc14); add(pc15); add(pc16); add(pc17); add(freeAll); //add(refresh); add(copyRight); } private class pc1Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc2Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc3Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc4Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc5Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc6Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc7Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc8Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc9Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc10Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc11Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc12Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc13Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc14Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc15Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc16Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class pc17Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class freeAllListener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } private class refreshListener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } /** * The finder grabs the computer that is using a certain account and returns * it so one can see when an account is in use. * @param pcNumber this is the number that is passed from the button variables * @return returns the computer name * @throws SQLException for accessing variables. */ public String finder(int pcNumber) throws SQLException{ Statement stmt = null;//This creates a statement variable Connection con = null; //Connection variable ResultSet rs;// result set variable //tries to connect to the database. try { Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection("jdbc:mysql://IP_ADDRESS:3306/DB", "USERNAME", "PASSWORD"); stmt = con.createStatement(); } catch(Exception e) { System.out.println("Exception: " + e.getMessage()); } //Gets everything from accounts table rs = stmt.executeQuery("SELECT * from accounts ORDER BY autoid"); String computer = ""; while(rs.next()) { int autoId= rs.getInt("autoid");//gets rows autoid int accAva= rs.getInt("available");//gets the account availability number (0=Available or 1=Not Available) int accNum= rs.getInt("number");//gets the account number that is passed String computerName= rs.getString("computer");//gets computer name if(accNum==pcNumber){ computer = computerName; } } return computer; } /** * Refreshes all the account names. * @throws SQLException */ public void Refresher() throws SQLException{ Statement stmt = null;//This creates a statement variable Connection con = null; //Connection variable ResultSet rs;// result set variable //tries to connect to the database. try { Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection("jdbc:mysql://IP_ADDRESS:3306/DB", "USERNAME", "PASSWORD"); stmt = con.createStatement(); } catch(Exception e) { System.out.println("Exception: " + e.getMessage()); } //Gets everything from accounts table rs = stmt.executeQuery("SELECT * from accounts ORDER BY autoid"); while(rs.next()) { int autoId= rs.getInt("autoid");//gets rows autoid int accAva= rs.getInt("available");//gets the account availability number (0=Available or 1=Not Available) int accNum= rs.getInt("number");//gets the account number that is passed String computerName= rs.getString("computer");//gets computer name for(int x=1; x<=18; x++){ finder(x); } } } }
Thanks PO
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
---------------------------------- Sir Winston Churchill
Though I don't do standalone application development using Swing, a few obvious comments.
>
You don't need to do this for every invocation. As soon as the Driver class is loaded, it registers itself with the
>
Try to avoid this.
>
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.
>
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
You never release your database resources! You need to close the 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. 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... ;-)
>
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. Java Syntax (Toggle Plain Text)
public void some() { Connection conn = null; PreparedStatement pStmt = null; ResultSet rs = null; try { try { //grab connection // use statement and resultset } finally { if(rs != null) rs.close(); if(pStmt != null) pStmt.close(); if(conn != null) conn.close(); } } catch(Exception e) { e.printStackTrace(); } }
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.
Java Syntax (Toggle Plain Text)
ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }; for(int len = pcs.length - 1; len >= 0; --len) { pcs[len].addActionListener(listener); }
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.
@~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
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
---------------------------------- Sir Winston Churchill
![]() |
Similar Threads
- How to optimize this code further for prime numbers (C++)
- Opinions on cleaning code or organization? C++ (C++)
- code optimization ... (C++)
- optimize code (C++)
- code optimization (C)
- Optimize this code? (ASP)
Other Threads in the Java Forum
- Previous Thread: software tools needed to develop java project
- Next Thread: Help in classes
Views: 373 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api apple applet application arguments array arrays automation binary blackberry block bluetooth chat class classes client code component database detection developmenthelp draw eclipse encode error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie number object online oracle os page print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing template test textfields threads time title transfer tree tutorial-sample update windows working






