I have an abstract class called student with an abstract method add().
I also have other classes undergraduate, graduate, parttime that extends student. Requirements are that I have to have an abstract class student and the same abstract method add(). This method needs to write to mySQL DB elements entered (fistname, lastname etc...).

In my add() method under subclass undergraduate I can easily see the values from the textfield (System.out.println(this.firstname));

My question is: how can I easily implement something like sql.execute(this.firstname, this.lastname...); the same as println(this.firstname)?

Another question. Is opening the connection and closing it for a single SQL statement a requirement? I assume that I have to open the SQL connection at the beggining of the program and close it when the program ends outside and independent of the classes.

I don't want to open a connection to the database, write the record and close the connection, since I have to implement that for the other three subclasses and it's a lot of repeating code.

What I want is the same as println(this.firstname) but to write a record to the database.

I can't use an interface because student is an abstract class and as I said it is a requirement.

Any thoughts, examples are welcome... Thanks

Firstly, an abstract class from definition contains atleast one abstract method so this means that not every method in an abstract class should be abstract. So if the add() method is identical for every student you could place it in the Student class instead of the specific sub-classes. This way you would have saved yourself the effort of writing three identical methods in three different classes. Also this is good for maintenance as you will soon learn.

Secondly, as you want a more convenient option of inserting the elements into the db what you could do is to write a method say insertToStorage() in the Db class that takes parameters to be inserted into the db pretty much like a SOP does and then puts them in the database. This will abstract away the database communication from your class and will keep it restricted to the db class itself. This is also good from another point of view which is say tomorrow if you need to change your storage to a flat file instead of a db then you do not need to change all of the sub-classes but you need to simply implement a file-storage specific class that offers a similar method insertToStorage() and your storage would have changed in minutes.

About the opening/closing of database connections, yes you would have to open/close connections for anything that you would want to do involving a database. But through the database specific class your connection handling and other db specific activities would all take place at one place and you wouldn't have to repeat the code everywhere else.

you can open and close connection at one place...

open in one class and close it an another class is not accepted...

you simply write connection opening and closing in your add method it self(student class)...

show when you subclass the student , all subclasses can handle this open and close via student(parent class) while it call addMethod() from it...

> Firstly, an abstract class from definition contains atleast one abstract method

Really? ;-)

abstract class MyClass {}

I don't want to open a connection to the database, write the record and close the connection, since I have to implement that for the other three subclasses and it's a lot of repeating code.

Yes, you are right, that's a lot of repeating code plus creating and closing connections is an overhead. Consider using a connection pool as a Datasource instead of asking connections from DriverManager. To get around the problem of repeating code (getting the connection, closing statements, result sets and finally the connection) create a class similar to SimpleJdbcTemplate which handles all the repetitive boiler plate code.

To have the same interface for both writing to STDOUT and database, create a StudenDao which exposes the insertStudent method and create two implementations for the same. Something like:

public interface StudentDao {
  void insertStudent(Student student);
}

public class JdbcStudentDao extends SimpleJdbcTemplate implements StudentDao {
  public void insertStudent(Student student) {
    someHelperMethodOfTemplate(yourQuery, student);
  }
}

public class LoggingStudentDao implements StudentDao {
  public void insertStudent(Student student) {
    System.out.println("Student name: " + student.getName());    
  }
}

Of course there might be other better ways/patterns of doing the same; these are the easiest ones.

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.