database + methods help required
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();
}}}
TIM_M_91
Junior Poster in Training
83 posts since Feb 2012
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0
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();
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
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.
stephen84s
Nearly a Posting Virtuoso
1,444 posts since Jul 2007
Reputation Points: 668
Solved Threads: 156
Skill Endorsements: 10