当前位置: 首页 > 面试题库 >

使用JDBC在Oracle数据库上创建Java

张高澹
2023-03-14
问题内容

我正在尝试使用JDBC在oracle数据库上创建Java源对象。

我要创建的源如下:

create or replace and resolve java source named "BlobIO" as package dbjava;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;


public class BlobIO {

    /**
     * Stores a blob into a local file (or even UNC-path)
     * @param blob The blob locator that should be stored in a file
     * @param filename The filename to write to
     * @param bufferSize The buffer size for data transfer
     * @return 1 if successful, 0 if failed
     */
    public static int blobToFile(java.sql.Blob blob, String filename, int bufferSize)
    {
        OutputStream os = null;
        InputStream is = null;
        boolean fail = true;
        try {

            is = blob.getBinaryStream();
            os = new FileOutputStream(filename);
            int amountRead = 0;
            byte[] buffer = new byte[bufferSize];
            while ((amountRead = is.read(buffer, 0, buffer.length)) != -1) {
                os.write(buffer, 0, amountRead);
            }
            is.close();
            os.flush();
            os.close();
            fail = false;
        } catch (IOException ex) {
            new File(filename).delete();
            System.err.println("Could not store blob to file.");
            System.err.println("File : " + filename);
            System.err.println("Reason : " + ex.getClass().getName() + " : " + ex.getMessage());
            fail = true;
        } catch (SQLException ex) {
            new File(filename).delete();
            System.err.println("Could not store blob to file.");
            System.err.println("File : " + filename);
            System.err.println("Reason : " + ex.getClass().getName() + " : " + ex.getMessage());
            fail = true;
        } finally {
            try {is.close();} catch (Exception ex) {}
            try {os.flush();} catch (Exception ex) {}
            try {os.close();} catch (Exception ex) {}
        }
        return fail? 0:1;
    }

    /**
     * Stores a blob into a local file (or even UNC-path)
     * @param query The query that should select ONLY the blob field
     * @param filename The filename to write to
     * @param bufferSize The buffer size for data transfer
     * @return 1 if successful, 0 if failed
     */
    public static int blobToFile(String query, String filename, int bufferSize) {
        try {
            Connection conn = DriverManager.getConnection("jdbc:default:connection:");
            Statement stmt = conn.createStatement();
            ResultSet rset = stmt.executeQuery(query);
            InputStream is;


            if (rset.next())
            {
                int ret = blobToFile(rset.getBlob(1), filename, bufferSize);
                if (rset.next())
                {
                    new File(filename).delete();
                    System.err.println("Could not store blob to file.");
                    System.err.println("Blob query : " + query);
                    System.err.println("File : " + filename);
                    System.err.println("Reason : too many rows");
                    rset.close();
                    stmt.close();
                    return 0;
                } else {
                    rset.close();
                    stmt.close();
                    return ret;
                }
            } else {
                System.err.println("Could not store blob to file.");
                System.err.println("Blob query : " + query);
                System.err.println("File : " + filename);
                System.err.println("Reason : no records retrieved by query");
                rset.close();
                stmt.close();
                return 0;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
            return 0;
        }

    }

}
/

我已经尝试过CallableStatement使用execute方法,这给了我。error: "Missing IN/OUT parameters"
当我尝试在普通Statement对象上使用execute方法时,出现错误:“

Non supported SQL92 token at position: 262"

有人知道我在做什么错吗?似乎在Google上也找不到任何内容。

编辑:这是我用来尝试执行脚本的代码(字符串sql包含您可以在上面看到的脚本,变量connConnection对象。

CallableStatement stat = conn.prepareCall(sql);
stat.setEscapeProcessing(false);
stat.execute();

如果我尝试使用Statement就是这样:

Statement stat = conn.createStatement();
stat.execute(sql);

问题答案:

Ok最终找到了问题,必须CallableStatement与解决setEscapeProcessing(false)



 类似资料:
  • 问题内容: 我正在使用http://code.google.com/p/sqlite- jdbc/wiki/Introduction中 的SQLite驱动程序。 上述文档中显示的示例显示了如何连接现有数据库。 在我的应用程序中,我需要创建一个SQLite数据库。怎么做?创建具有扩展名的文件是否足够?还有一个叫做的函数。如果可以,如何使用?我用谷歌搜索,没有人给出明确的答案。 问题答案: 如果文件不

  • 主要内容:所需步骤,示例代码在本教程将演示如何在JDBC应用程序中创建数据库。 在执行以下示例之前,请确保您已经准备好以下操作: 具有数据库管理员权限,以在给定模式中创建数据库。 要执行以下示例,需要用实际用户名和密码替换这里用户名()和密码()。 MySQL或数据库已启动并运行。 所需步骤 使用JDBC应用程序创建新数据库需要以下步骤: 导入包:需要包含包含数据库编程所需的JDBC类的包。 大多数情况下,使用就足够了。 注

  • 我正在尝试一个程序来检查与oracle数据库的连接,如下所示 但在eclipse中运行之后,它显示了以下异常。 JAVAsql。SQLException:Listener拒绝了连接,出现以下错误:ORA-12505,TNS:Listener当前不知道连接描述符中给出的SID 请帮助我,我是oracle数据库的新手。

  • 我有公共IP,在私有IP中安装了Oracle cloud DB。通过公有IP,我们可以建立ssh隧道到私有IP,并转发本地主机端口访问Oracle cloud DB。 我的任务是创建从本地主机oracle数据库到cloud oracle数据库的数据库链接。因此,我可以从本地主机Oracle DB访问Oracle cloud DB架构。 我使用Putty建立SSH和localhost端口转发,并且可

  • 我正在尝试学习如何使用Oracle Container数据库,并只做基本的JDBC连接。我安装了一个文档化版本的Oracle: https://hub.docker.com/_/oracle-database-enterprise-edition 根据数据表,该数据库由CDB数据库ORCLCDB和PDB数据库ORCLPDB1组成。 所以我想我可以这样连接它: jdbc:oracle:thin:@l

  • testcontainers oracleContainer在Dokerfile中定义的entrypoint发生更改之前启动,以完成并创建测试所需的用户。 容器运行后,将执行一些sql脚本创建表并用数据填充表。这些脚本失败,因为脚本中提到的用户尚未创建。 testcontainers没有等待完成安装。 通过使用以下命令检查日志: 我可以看到testcontainers从不等待它完成。 关于这个“b