The problem is as follows. Suppose there are two different data models Table1 and Table2 I get the data from the JSP page to the servlet and fill these models into the records array which is of the type of one of these models. After that I want to get an array in the add method to the model.insert (records) database

The question is how to use the insert method to access the appropriate model getter? If I do not understand what has come to the method Table1 or Table2 ?? I want a universal method. Help please, preferably with an example.

method

insert Table1 или Table1 element; for (int i = 0; i < records.size(); i++) { element = (Table1 или Table2) records.get(i); String name = element.getName(); int vid = element.getId(); } case "Tab1": Table1 table1 = null; records = new ArrayList<Table1>(); table1 = new Table1( Integer.parseInt(request.getParameter("id")), formatter.parse(request.getParameter("datan")),// тип date request.getParameter("name").trim(), records.add(table1); case "Tab2": Table2 table2 = null; records = new ArrayList<Table2>(); table2 = new Table2( Integer.parseInt(request.getParameter("id")), request.getParameter("pole").trim(), request.getParameter("name").trim(), records.add(table2); model.insert(records); public class Table1 { private int id; private Date Data1; private String Name; public Table1() { } public Table1(int id,Date Data1, String Name) { this.id = id; this.Data1 = Data1; this.Name = Name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getData1() { return Data1; } public void setData1(Date Data1) { this.Data1 = Data1; } public String getName() { return Name; } public void setName(String Name) { this.Name = Name; } public class Table2 { private int id; private int Vid; private Date Data1; private String Name; public Table2() { } public Table1(int id, String Pole, String Name) { this.id = id; this.Pole = Pole; this.Name = Name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPole() { return Pole; } public void setPole(String Pole) { this.Data1 = Data1; } public String getName() { return Name; } public void setName(String Name) { this.Name = Name; } public String insert(String shema, String table, List records) throws Exception { String query = ""; String result = ""; PreparedStatement stmt = null; Registry.openConnection(); Connection conn = Registry.getConnection(); conn.setAutoCommit(false); try { query ="INSERT into aa (а1..аn) vlues(?,?,?)"; stmt = conn.prepareStatement(query); Вот тут и надр сделать setObject из records stmt.setObject(1, Integer.parseInt(vid)); stmt.setObject(2, genSQLDatePlus(date, 0)); stmt.setObject(3, name); int j = stmt.executeUpdate(); stmt.close(); conn.commit(); } catch (SQLException e) { conn.rollback(); throw new Exception(e); } finally { if (stmt != null) { stmt.close(); } Registry.closeConnection(); } return result; } 

    1 answer 1

    Of course you can. Discover the interfaces, without them in the world of OOP is really bad. And look at what a Service Layer is ... And pay attention to the naming of variables. Permen are always named with camel notation. Constants are called in capital letters through the underscore. It seems a trifle, but hands are torn off for it. And now the solution. Create an interface that will be implemented by both of your classes.

      public interface TableInterface { Date getData(); int getId(); String getName(); void setData(Date data); void setId(int id); void setName(String name); } import java.util.Date; public class Table1 implements TableInterface { private int id; private Date data; private String name; public Table1() {} public Table1(int id, Date data, String name) { this.id = id; this.data = data; this.name = name; } @Override public int getId() { return id; } @Override public void setId(int id) { this.id = id; } @Override public Date getData() { return data; } @Override public void setData(Date data) { this.data = data; } @Override public String getName() { return name; } @Override public void setName(String name) { this.name = name; } } import java.util.Date; public class Table2 implements TableInterface{ private int id; private int vid; private Date data; private String name; public Table2() {} public Table2(int id, int vid, Date data, String name) { this.id = id; this.vid = vid; this.data = data; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getVid() { return vid; } public void setVid(int vid) { this.vid = vid; } public Date getData() { return data; } public void setData(Date data) { this.data = data; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 

    Fine! Now the collection for writing class instances will look like this

     List <TableInterface> records = new ArrayList<>(); 

    Since the collection is typed by your interface, and not by a specific class, it can store objects of all classes that implement this interface. This means that you easily add Table1 and Table2 classes to this collection.

    After that we create a universal method (let it be a service).

     import java.util.List; public class Service<T extends TableInterface> { public void ibsert(final List<T> tables) { for (int i = 0; i < 10; i++) { T t = tables.get(i); String name = t.getName(); int vid = t.getId(); } } } 

    In the insert method of this service, you transfer your collection with the objects of your two classes. That's all. Since the class is typed by generic T, which is inherited from your interface, a variable of type T in the method can use all methods of the interface. At the same time, it does not matter what kind of object it is - a class of Table1 or Table2. This is the classic polymorphism. Moreover, the services usually build a tree with a different level of abstractions at each level, which allows, lowering down the tree, to specify the type of objects, using their more and more specific methods (Service Layer).

    Excellent and very correct question! Good luck to you.

    • Comments are not intended for extended discussion; conversation moved to chat . - YurySPb