How to run sql scripts in order to update a Derby schema from java code?

佐手、 提交于 2019-12-02 05:29:46

You might find the ij.runScript method helpful: http://db.apache.org/derby/javadoc/publishedapi/jdbc4/org/apache/derby/tools/ij.html

One thing to be aware of is that it's not easy to automate the error handling inside the script, as there are no control flow (if/then/else) constructs in the ij scripting. But if you just need to perform a series of DDL operations in a clean manner, ij.runScript is a pretty good technique.

You could store the SQL in a text file:

From your java code you read in the SQL for updating/inserting data into the various tables:

Your java code could also perform checks on which 'SQL update file to run'.

something along these lines (in very pseudo code)

Installed = getCurrentInstalledVersion();//info stored in an admin table for current version

if (Installed < V4.1){//This would probably work better as a switch
String sql = updateWith(URL file:\\PathTov4.1\sql.txt);
}
else{
     if(Installed = OtherVersions){
      String sql = UpdateWith(URL file:\\PathToOtherVersion\sql.txt);
      }
}

Connect.sendSQL(sql);//send the sql to the DB

I leave you to figure out the fuller details. If you already found a solution, why not post it here.

If you use a switch you could run updates to a newer version via other versions if required by placing the versions in the correct order, and inserting code before any break;

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!