繁体   English   中英

JDBC:调用外部SQL脚本,这些脚本声明一个PL / SQL包

[英]JDBC: Call external SQL Scripts, which declare a PL/SQL Package

我有一个名为setup的SQL脚本,该脚本建立一些设置,然后声明一个名为salespack的PL / SQL包

setup.sql:

set verify off
set feedback off
set serveroutput on;
set linesize 200;
@Package/pack.sql
@Package/packbody.sql

我有一个名为main.java的文件,该文件尝试运行上述SQL脚本,然后调用该程序包,但这似乎不起作用。 如何使用jdbc调用SQL脚本,声明PL / SQL包,然后在包中使用一个函数。 我得到的错误是calltmt.execute(); 是无效的SQL语句。

main.java:

import java.sql.*;
import java.io.*;

class main
{
  public static void main (String args [])
       throws SQLException, IOException
  {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    String connect = "jdbc:oracle:thin:@vmaddress:1521:xe";
    Connection conn = DriverManager.getConnection (connect, "user", "pass");

    //Perform setup
    //Load packages, allow output, establish sale database
    //Make sure to run the main script from dbSetup to have fresh data
    CallableStatement callstmt = conn.prepareCall ("@setup.sql");
    callstmt.execute();
    callstmt.close();

    CallableStatement callstmt2 = conn.prepareCall ("{ ? = call salepack.getspname(?) }");
    callstmt2.registerOutParameter (1, Types.VARCHAR);
    int id = 23;    // the id is hard-coded here for simplicity
    callstmt2.setInt(2, id);

    callstmt2.execute();
    String name = callstmt2.getString(1);
    System.out.println ("The salesperson with id of " + id + " is " + name);
    callstmt2.close();
    conn.close();
  }
}

我认为您可以使用以下代码运行setup.sql

private static String script_location = "";
private static String file_extension = ".sql";
private static ProcessBuilder processBuilder =null;

public static void main(String[] args) {
try {
    File file = new File("C:/sql_folder");
    File [] list_files= file.listFiles(new FileFilter() {

        public boolean accept(File f) {
            if (f.getName().toLowerCase().endsWith(file_extension))
                return true;
            return false;
        }
    });
    for (int i = 0; i<list_files.length;i++){
        script_location = "@" + list_files[i].getAbsolutePath();//ORACLE
        processBuilder = new ProcessBuilder("sqlplus",        "UserName/Password@database_name", script_location); //ORACLE
        //script_location = "-i" + list_files[i].getAbsolutePath();
        //  processBuilder = new ProcessBuilder("sqlplus", "-Udeep-Pdumbhead-Spc-de-deep\\sqlexpress-de_com",script_location);
        processBuilder.redirectErrorStream(true);
        Process process = processBuilder.start();
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String currentLine = null;
        while ((currentLine = in.readLine()) != null) {
            System.out.println(" "  + currentLine);
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}catch(Exception ex){
    ex.printStackTrace();
}
}

也许下面的链接会给你一些其他的想法;

如何在Java中显示sqlplus结果?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM