Timo, I have a application that generate sql dynamically.
My application is like this:
public Class Table {
private String nameTable;
private List<FieldTable> listField;
public Table(String nameTable,List<FieldTable> listField) {
this.nameTable = nameTable;
this.listField = listField;
}
//Getter and Setter of nameTable and listField
..........
}
public Class ServiceInsert {
// Implementation of a Singleton
......
public void insertTable(Table table,List<Object> values){
StringBuffer st = new StringBuffer("INSERT INTO " + Table.getNameTable());
StringBuffer fieldSt = new StringBuffer();
StringBuffer valueSt = new StringBuffer();
for (Field field : Table.getListField){
fieldSt.append(field.getNameField + ",");
valueSt.append("?,");
}
st.append("(" + fieldSt.toString() + ") VALUES " + valueSt.toString);
//Connection JDBC for a Singletton
Connection con = ServiceConnection.getInstance().getConnection();
PreparedStatement ps = con.preparedStatement(st.toString);
//
assign values
....
ps.executeUpdate();
con.commit();
}
}
@WebServlet("/send")
public class SendServlet extends HttpServlet {
private static final long serialVersionUID = 7593256373572976563L;
private static final Logger _logger = Logger.getLogger(EnvioServlet.class.getName());
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
_
logger.info("start the sending ...");
List<Field> fields = createField(request.getParameterMap());
List<Object> Values=createValues(request.getParameterMap());
Table newTable = new Table(request.getParameter("Table"),fields);
ServiceInsert.getInstance().insertTable(newTable,values);
_
logger.info("End sending ...");
} catch (Throwable e) {
_logger.log(Level.SEVERE, e.getMessage());
response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
}
}
}
My application received the name of table , fields and the values.
I do not know how to create the QClass of something dinamic?. is it possible or
make Insert sql dinamic, only Knowing the name table, the name fields and the values.
How to do it?.
The requirements are well.
I already made changes to use with dynamic SQL queries QUeryDSL now have to do the same but with Insert so substitute the part of my code with preparedStatement using QueryDSL. This is my big question, as I do with QueryDSL.