【问题标题】:byte[] to be stored in Oraclebyte[] 存储在 Oracle 中
【发布时间】:2013-11-22 09:04:23
【问题描述】:

我有一个字节[],它实际上是一个图像。

我想将它存储在 Oracle 11g 中。我在表中创建了一个 BLOB 列。并通过以下我尝试插入它。

String imageStr = "xyz...."
byte[] data = imageStr.getBytes(); 
String sQuery = "insert into Table (LOCATION , BLOB_DATA) Values ('Lahore', data) ";

抛出异常“java.sql.SQLException: ORA-01465: invalid hex number

我搜了一下,发现这种查询应该是通过PreparedStaement来完成的。

所以我做了以下事情

PreparedStatement prepStmt = dbConnection.prepareStatement("insert into Table (LOCATION, BLOB_DATA) values(?,?);
prepStmt.setString(1, 'Lahore');
prepStmt.setBytes(2, bytes);

我开始在 dbConnection.prepareStatement(String) 上遇到错误,因为 DBConnection 类不是 Java Native 类。

这是早期开发者为数据库连接制作的自定义类,它没有prepareStatement(String)功能。

那现在该怎么办?

1.我应该在 DBConnection 类中创建一个方法 prepareStatement(String) 吗?

2。我应该采用第一种方法吗?

【问题讨论】:

    标签: java sql oracle


    【解决方案1】:

    你可以看我的例子来将图像存储在数据库中

       Statement s;
             Connection c;      
             FileInputStream fis;
             PreparedStatement ps;
             File file; 
             try
            {
                   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//your driver
                   c=DriverManager.getConnection("Jdbc:Odbc:image","scott","tiger");//password and name changes according to your db
                   s=c.createStatement();   
                   st.execute("Create table ImageStoring(Image_No number(5),Photo blob)");
             }
             catch(Exception e1) 
            {
                   e1.printStackTrace();
             }  
    
             try
            {       
                   file=new File"D:/ARU/Aruphotos/4.jpg");
                   fis=new FileInputStream(file);
    
                   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                   c=DriverManager.getConnection("Jdbc:Odbc:image","scott","tiger");
                   s=c.createStatement();
    
                   ps=c.prepareStatement("insert into ImageStoring values(?,?)");
                   ps.setInt(1,2);
                   ps.setBinaryStream(2,fis,(int)file.length());
                   System.out.println("success");
                   ps.execute();
                   ps.close();
                   c.close();
             }
             catch(Exception e)
             {
                   e.printStackTrace();
             }
         }
    

    【讨论】:

    • 请注意代码中的“Connection c”,即 java.sql.Connection;所以你得到了 con.prepareStatement() 的本机方法;但在我的情况下,Connection 是 DBConnection con 的一个实例; DBConnection 是由以前的开发人员制作的自定义类。哪个没有 con.prepareStatement();方法
    • 希望我能......这是一个很大的类......大约 6000 行。并包含一些客户信息,这就是为什么。你能给我你的最终 SQL 查询吗?所以我可以通过在 Oracle 上运行来检查它。所以我可以确保数据库部分工作正常
    猜你喜欢
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 2019-06-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多