D
D
DDwrt1002019-10-08 12:25:18
Java
DDwrt100, 2019-10-08 12:25:18

How to connect to generated tables at runtime?

Good afternoon, can you please tell me if this will work?
The situation, there is a system which works with . The peculiarity of this system is that it periodically creates a new table, something like:

tableImportantResults_02022012
tableImportantResults_03022012

I need to connect to generated tables and take certain data.
So far I have come up with such a scheme, but I'm not sure if it will work.
I create a model class
@Entity 
@Table(name = TABLE)
public class Model{
public static string = Table;
@id
@Column(name = "id")
int id
}

And actually when I start working with the table, changing the static variable, I change the name of the table to which I am connected.
Will this option work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bestie, 2019-10-10
@bestie

Not strong in Heber, but according to Google, there is an option to use your own interceptor, which will dynamically change the table name https://javaaltaf.blogspot.com/2019/01/change-tabl...

public class CustomInterceptor extends EmptyInterceptor {
 @Override
 public String onPrepareStatement(String sql) {
  System.err.println("Before Modifying SQL =" + sql);
  sql = sql.replace("ATTENDANCE_1_2019 ", "ATTENDANCE_2_2019 ");
  System.err.println("After Modifying SQL =" + sql);
  return sql;
 }
}

Or as an option - just switch to PlainSQL ("SELECT * FROM :table_name") + NamedParameterJdbcTemplate -> this will allow you to push the table name into the query itself without any problems, but you will have to process the result manually (RowMapper'om).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question