java - Execute an .sql file in unix using org.springframework.jdbc.datasource.init.ScriptUtils -


i have sql file present in unix server. want run file in unix using java program. saw many ways online, tried using org.springframework.jdbc.datasource.init.scriptutils

the code follows

try {             class.forname("oracle.jdbc.driver.oracledriver");         } catch (classnotfoundexception ex) {             system.out.println("error: unable load driver class!");             system.exit(1);         }          string url = configbean.geturl();         string user = configbean.getuser();         string pass = configbean.getpassword();         system.out.println(url + " ,"+user+" ,"+pass);         connection conn = drivermanager.getconnection(url,user,pass);         system.out.println(configbean.getsqlfilepath());         scriptutils.executesqlscript(conn, new classpathresource(configbean.getsqlfilepath()));  

the problem method unable detect file in unix server. have path present in config file. there i'm reading path, path "/home/applvis/java/uat/config/abc.sql". when execute jar containing code, shows file not found in location. removes first slash present in file path. if put 2 slashes unable detect file. error is

info: executing sql script class path resource [/home/applvis/java/uat/config/allot010t_objnames.sql] exception in thread "main thread" org.springframework.jdbc.datasource.init.cannotreadscriptexception: cannot read sql script class path resource [/home/applvis/java/uat/config/allot010t_objnames.sql]; nested exception java.io.filenotfoundexception: class path resource [/home/applvis/java/uat/config/allot010t_objnames.sql] cannot opened because not exist         @ org.springframework.jdbc.datasource.init.scriptutils.executesqlscript(scriptutils.java:442)         @ org.springframework.jdbc.datasource.init.scriptutils.executesqlscript(scriptutils.java:395)         @ org.springframework.jdbc.datasource.init.scriptutils.executesqlscript(scriptutils.java:373)         @ com.acc.directory.scanner.sdscanner.main(sdscanner.java:77) caused by: java.io.filenotfoundexception: class path resource [/home/applvis/java/uat/config/allot010t_objnames.sql] cannot opened because not exist         @ org.springframework.core.io.classpathresource.getinputstream(classpathresource.java:143)         @ org.springframework.core.io.support.encodedresource.getreader(encodedresource.java:92)         @ org.springframework.jdbc.datasource.init.scriptutils.readscript(scriptutils.java:279)         @ org.springframework.jdbc.datasource.init.scriptutils.executesqlscript(scriptutils.java:439)         ... 3 more 

i'm unable understand doing wrong or there different way read files unix. please help

you wrote file lies in filesystem under /home/applvis/java/uat/config/abc.sql, code trying load file classpath.

so instead of

scriptutils.executesqlscript(conn, new classpathresource(configbean.getsqlfilepath()));  

you have use

scriptutils.executesqlscript(conn, new filesystemresource(configbean.getsqlfilepath()));  

Comments

Popular posts from this blog

python - Mongodb How to add addtional information when aggregating? -

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

java - Incorrect order of records in M-M relationship in hibernate -