Oracle的绑定CLOB的Jdbc教程示例

问题描述 投票:1回答:1

我试图了解如何使用CLOBJDBC传递到数据库。我正在使用Oracle 11gOracle's JDBC Driver 12.1.0.1

我正在浏览Oracle的Jdbc教程和I found this sample code。它读取文件的内容并写入Clob实例,然后使用INSERT将其写入数据库。为了便于阅读,我只发布代码的相关部分:

        Clob myClob = this.con.createClob();
        Writer clobWriter = myClob.setCharacterStream(1);
        String str = this.readFile(fileName, clobWriter);
        System.out.println("Wrote the following: " +
            clobWriter.toString());

        if (this.settings.dbms.equals("mysql")) 
        {
            System.out.println("MySQL, setting String in Clob " + "object with setString method");
            myClob.setString(1, str);
        }

        System.out.println("Length of Clob: " + myClob.length());

        String sql = "INSERT INTO COFFEE_DESCRIPTIONS " + "VALUES(?,?)";

        pstmt = this.con.prepareStatement(sql);
        pstmt.setString(1, coffeeName);
        pstmt.setClob(2, myClob);
        pstmt.executeUpdate();

这里是readFile方法的代码:

    private String readFile(String fileName, Writer writerArg) throws FileNotFoundException, IOException {

    BufferedReader br = new BufferedReader(new FileReader(fileName));
    String nextLine = "";
    StringBuffer sb = new StringBuffer();
    while ((nextLine = br.readLine()) != null) 
    {
        System.out.println("Writing: " + nextLine);
        writerArg.write(nextLine);
        sb.append(nextLine);
    }

    // Convert the content into to a string
    String clobData = sb.toString();

    // Return the data.
    return clobData;
}

我修改了上面的示例代码,以将CLOB传递给PL / SQL过程。

 public String handleEmployeeReview(int empId , String fileName) throws IOException
 {
     Connection conn = null;
     CallableStatement callStmt = null;
     String reviewContent = null;

     try 
     {
         // Register the Oracle Jdbc driver 
         // Class.forName(JDBC_DRIVER_ORACLE);

         // Create a database connection
         conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PWD);

         // Create a query string
         String callProc = " { call HR.EMP_PKG.handle_employee_review ( ? , ? , ? ) } ";

         // Create a Callable Statement
         callStmt = conn.prepareCall(callProc);

         // Bind values to the first IN parameter
         callStmt.setInt(1, empId);


         // Create CLOB and use ClobWriter
         Clob reviewFileClob = conn.createClob();
         Writer clobWriter = reviewFileClob.setCharacterStream(1);
         String  reviewStr = this.readFile(fileName , clobWriter);
         callStmt.setClob(2, reviewFileClob);


         // Register OUT parameter
         callStmt.registerOutParameter(3, java.sql.Types.VARCHAR);

         // Execute the callable statement
         callStmt.execute();

         // Retreive the OUT parameters
         reviewContent = callStmt.getString(3);
         System.out.println("Employee with id : "+ empId + " has annual review : " + reviewContent);
     } 

这是PL / SQL过程:

  -- PURPOSE: Takes Clob and returns its contents
  -- Example of: PROCEDURE that takes a Clob as IN parameter
  PROCEDURE handle_employee_review(empId IN VARCHAR2 , empReviewDoc IN CLOB , reviewContent OUT VARCHAR2) IS
  BEGIN
    reviewContent := empReviewDoc || ' THIS IS COMING FROM THE DATABASE';
  EXCEPTION
    WHEN others THEN
    dbms_output.put_line('Error!');
  END handle_employee_review;

在运行修改后的代码时,我看不到任何东西发送到数据库。我如何使此代码起作用?似乎writerArg.write(nextLine);方法中的这一行readFile无法按预期工作?这是我的代码的输出:

Employee with id : 101 has annual review :  THIS IS COMING FROM THE DATABASE

这里是调试器的快照。我确实看到数据正在写入CLOB的Writer

“在此处输入图像描述”

如果您在上面的o / p中注意到,PL / SQL代码在CLOB中什么也找不到。

另一方面,如果我以以下方式将内容传递给CLOB(本教程的示例也使用该内容,但仅适用于MYSQL数据库)。 我还使用setString方法将文件内容传递给CLOB,而没有使用Clob的Writer实例。效果很好。

         Clob reviewFileClob = conn.createClob();
         String  reviewStr = this.readFile(fileName , clobWriter);
         reviewFileClob.setString(1, reviewStr);
         callStmt.setClob(2, reviewFileClob);

这给了我以下输出:

Employee with id : 101 has annual review : Employee Exceeds Expectations THIS IS COMING FROM THE DATABASE

NOTE:我已经剥离了原始代码(删除了try catch块,数据库/文件资源清理),仅发布了相关代码。帖子太长了。

java jdbc database-connection instantiation
1个回答
0
投票

我有同样的问题。看来Oracle教程中的示例代码仅适用于非常大的文件。当我尝试使用一个小文件时,它也仅在执行setString(...)方法时才起作用。但是对于大文件,它无需setString就可以工作。截至目前尚无解决方案...

© www.soinside.com 2019 - 2024. All rights reserved.