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.
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);
}
}
}
}
The reason I am posting is because this code takes almost 45seconds to run! Why is it going so slow?
Thanks PO