mercredi 1 août 2018

Trying to use an Accessor method from a button within a jframe in netbeans

I'm trying to code a program which will retrieve different "Events" from a database, by using an accessor method, and compare them to different results using if-statements from within a button. However, I am having trouble accessing the accessor method from within the button, because it gives me the "connection not found exception".

This is my code:

public static String getEvent1()
 {
String Event1 ="";
try {
        Statement s = conn.createStatement();

     String query = "SELECT *"
                 + "FROM EventDets "  
                 + "WHERE Username = '"+username+"'"; 

     ResultSet rs = s.executeQuery(query);
    if (rs.next()){
       Event1 =rs.getString("Event1");
    }
    s.close();
    conn.close();
    }
 catch (SQLException ex) {
     System.out.println("got 2 exception " + ex.getMessage() );
 }

return Event1;

}

     private void getprogActionPerformed(java.awt.event.ActionEvent evt)      {             
      if (Event1.equals("100m"))
      {
      try 
   {   
     Statement s = conn.createStatement();

     String query = "SELECT* " +
                    "FROM tbl " +
                    "WHERE SGroup = 'IntenseBoy'";


     ResultSet rs = s.executeQuery(query); 

     while (rs.next()) 
     {
     JOptionPane.showMessageDialog(null,"Follow this program for six weeks:  \n Exercise 1: " + rs.getString ("Exercise1") + "\n Exercise 2: " + rs.getString ("Exercise2") +
            "\n Exercise 3: " + rs.getString ("Exercise3") + "\n Exercise 4: " + rs.getString ("Exercise4") + "\n Exercise 5: " + rs.getString ("Exercise5"), 
            "Your Training Program", JOptionPane.INFORMATION_MESSAGE, 
            new ImageIcon("running.jpg"));

     }
        s.close(); 
        conn.close();
    }
    catch (SQLException ex) 
    {
    System.out.println("got exception " + ex.getMessage() );  

    }
  }
}      

I am relatively new to java and I'm only doing it on school level so any help will be appreciated.

Aucun commentaire:

Enregistrer un commentaire