1

I have the Java object with a method "getUser" which will do a query, for example "select * from user when table.name= $param1"

The method have the parameter "Map<String, String> params" , for example "params.add("param1", "Den")"

I want to store the query in other file, for example GetUser.sql

When running the program I change the query.

How do this?

I won't found a framework

2
  • When running the program, u wanna change the query manually or from inside the program? Commented Aug 15, 2013 at 9:35
  • I want to do late binding, change the file sql I can other program and myself. Commented Aug 15, 2013 at 9:51

2 Answers 2

3

You could use java property file store your query. During program execution, you could load the query from property file and fill the parameters and execute the final query to the database.

query.getuser = select * from user when table.name= $param1

From code load the property file and get your query string:

Properties property = //load property file 
String getUserQuery= propertie.getProperty("query.getuser");

Alternatively, you could use JPA/Hibernate named query where you can keep your query into another external mapping file. See, http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch13.html

Sign up to request clarification or add additional context in comments.

3 Comments

JPA/Hibernate named queryif - If change the query at runtime, it will not pick up
Why you want to change query at runtime. you may have several queries during runtime. you could select appropriate one and execute it. It is not good idea to build the whole query string at runtime as it is vulnerable for SQL injection.
you mention there are bugs in your code. Can you specify what bugs? We could help you to fix them.
1

You can try FileAlterationMonitor from Apache commons-vfs:

FileSystemManager fsManager = VFS.getManager();
FileObject file = fsManager.resolveFile("/path-to-file");
DefaultFileMonitor fm = new DefaultFileMonitor(new FileChangeListener());
fm.addFile(file);
fm.start();

public class FileChangeListener implements FileListener {
  @Override
  public void fileCreated(FileChangeEvent event) throws Exception {
    //do nothing
  }

  @Override
  public void fileDeleted(FileChangeEvent event) throws Exception {
    //do nothing
  }

  @Override
  public void fileChanged(FileChangeEvent event) throws Exception {
    //Reload query from file
  }
}

For query reloading you can use Shamim Ahmmed's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.