Hi guys well I have two class's 1 class for my methods and another class for my GUI. In my method class I have my update statements for my database and in my GUI class I have obviously my gui and all my buttons that will produce a result when clicked. My problem is that I have set my update statement to work with getText from my GUI class. So how do I go about setting it up in my method class to read the getText from my GUI class?

My code can be seen below:

Method class code:

public static String UPDATE() {
        String output = listHeader();
        try {
            ResultSet res = stmt.executeQuery("UPDATE VIDEOS SET VIDEONAME = '" + v  + "' , DIRECTOR = '" + s + "' , RATING = ' " 
                    + r + "' , PLAYCOUNT = ' " + p + " ' WHERE VIDEOID = '"+ f + "'");
            while (res.next()) { // there is a result
                output += formatListEntry(res);
            }
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        return output;
    }

Gui class code

public void actionPerformed(ActionEvent e)
  {
 
      if (e.getSource() == writeBtn) {
            String f = VIDEOIDS.getText();
            String v = VIDEONAMES.getText();
            String s = DIRECTORS.getText();
             String r = RATINGS.getText();
             String p = PLAYCOUNTS.getText();
             String key = VIDEOIDS.getText();
             String name = VideoData.getName(key);
             
             if (name == null) { // if user input if null the following statement will execute
                showMessageDialog(this, "Please Enter Valid Video ID"); // information field will prduce following text
            }
             else {
            // if any field is blank, signal an error
            
                 
                METHODS.UPDATE();

  }}}

method class to read the getText from my GUI class?

You would need a reference to the GUI class in the method class that you could use to call the GUI class's getText() method.
String someText = refToGUIClass.getText();

My JDBC knowledge maybe a little rusty here,but last time I checked 'executeQuery()' throws an exception if a ResultSet was not generated by your query and an SQL 'Update' does not generate a ResultSet. Suggest you use the executeUpdate() method instead.

Also another thing, when constructing a query the way you are:-

ResultSet res = stmt.executeQuery("UPDATE VIDEOS SET VIDEONAME = '" + v + "' , DIRECTOR = '" + s + "' , RATING = ' " + r + "' , PLAYCOUNT = ' " + p + " ' WHERE VIDEOID = '"+ f + "'");

It is always best to use a paremeterized PreparedStatement. That way if your variables 'v,s,r,p and f' contain apostrophes ('), you will not run into SQLException caused due to invalid SQL syntax.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.