使用org.springframework.jdbc.datasource.init.ScriptUtils在unix中执行.sql文件

Har*_*thi 3 java unix spring

我的unix服务器中有sql文件.我想使用java程序在unix中运行该文件.我在网上看到很多方法,我尝试过使用org.springframework.jdbc.datasource.init.ScriptUtils

代码如下

try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException ex) {
            System.out.println("Error: unable to 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())); 
Run Code Online (Sandbox Code Playgroud)

问题是此方法无法检测到unix服务器中的文件.我在配置文件中有路径.从那里我正在阅读路径,我的路径是"/home/applvis/JAVA/UAT/config/ABC.sql".当我执行包含此代码的jar时,它会显示在此位置找不到的文件.它删除文件路径中存在的第一个斜杠.如果我放两个斜杠,那么它也无法检测到该文件.我得到的错误是

INFO: Executing SQL script from 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 from class path resource [/home/applvis/JAVA/UAT/config/ALLOT010T_OBJNAMES.sql]; nested exception is java.io.FileNotFoundException: class path resource [/home/applvis/JAVA/UAT/config/ALLOT010T_OBJNAMES.sql] cannot be opened because it does not exist
        at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:442)
        at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:395)
        at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:373)
        at 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 be opened because it does not exist
        at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:143)
        at org.springframework.core.io.support.EncodedResource.getReader(EncodedResource.java:92)
        at org.springframework.jdbc.datasource.init.ScriptUtils.readScript(ScriptUtils.java:279)
        at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:439)
        ... 3 more
Run Code Online (Sandbox Code Playgroud)

我无法理解我做错了什么,或者是否有不同的方法从unix读取文件.请帮忙

Uoo*_*ooo 7

您编写的文件位于文件系统下/home/applvis/JAVA/UAT/config/ABC.sql,但您的代码正在尝试从类路径加载到文件.

而不是

ScriptUtils.executeSqlScript(conn, new ClassPathResource(configBean.getSqlFilePath())); 
Run Code Online (Sandbox Code Playgroud)

你必须使用

ScriptUtils.executeSqlScript(conn, new FileSystemResource(configBean.getSqlFilePath())); 
Run Code Online (Sandbox Code Playgroud)