gpt4 book ai didi

java - byte[] 存储在 Oracle 中

转载 作者:行者123 更新时间:2023-12-01 13:48:39 25 4
gpt4 key购买 nike

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

我想将其存储在 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:无效的十六进制数

我搜索了一下,发现这种类型的查询应该通过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。我应该采用第一种方法吗?

最佳答案

您可以查看我的示例,将图像存储在数据库中

   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();
}
}

关于java - byte[] 存储在 Oracle 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20140829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com