During the development of a software, I need to execute a statement which is stored as a string
. Say for example:
String test = " int a; ";
In the above example, I want to declare a variable a
.
Another example is the execution of a mysql statement from a string
:
String sqlquery ="SELECT * FROM FULLDETAILS WHERE Classname = 'test' ";
con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/executable","root","mysql");
st = (Statement) con.createStatement();
rs = st.executeQuery(sqlquery);
^
I want to achieve this for Java statements too. Is there a way?
EDIT :
The above given is an example, the real situation is much different (but the thing I basically need is the same).
The situation is that, I have a number of jpanels
and I want to add some components to it, say a label
, the content of the label is obtained from a database, and its only during runtime I can know, to which panel I should add the component. In this case, I cannot designate the panel
by name, say if I have 10 jpanels
, I thought I could use a string like:
String test = " jpanel " + i + " .add(jlabel1); " ;
will result in the value of test will be:
jpanel1.add(jlabel1); // provided i is a string with value of i is 1
now I want to execute this. Is there a way?