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; }