我目前正在开发一个需要图像的应用程序,因此我尝试使用 blob 和 clob 变量在 Oracle 中通过 SQL 查询插入图像,但我无法通过此操作,而且我正在使用 JAVA 技术进行此操作。
问问题
504 次
1 回答
0
这是您的问题的简单解决方案:
FileInputStream fis;
try {
// Load JDBC driver "com.mysql.jdbc.Driver"
Class.forName("oracle.jdbc.driver.OracleDriver");
/* Create a connection by using getConnection() method that takes
parameters of string type connection url, user name and password to
connect to database. */
String connectionUrl = "your connection url";
String userName = "connecting database username";
String password = "corresponding password";
connection = DriverManager.getConnection(connectionUrl ,userName , password);
// create a file object for image by specifying full path of image as parameter.
File image = new File("C:/image.jpg");
/* prepareStatement() is used for create statement object that is
used for sending sql statements to the specified database. */
psmnt = connection.prepareStatement("insert into save_image(name, city, image) values(?,?,?)");
psmnt.setString(1,"name");
psmnt.setString(2,"city");
fis = new FileInputStream(image);
psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));
/* executeUpdate() method execute specified sql query. Here this query
insert data and image from specified address. */
int s = psmnt.executeUpdate();
if(s>0) {
System.out.println("Uploaded successfully !");
} else {
System.out.println("unsucessfull to upload image.");
}
} catch (Exception e){
//log useful information
}
finally{
if (fis != null) {
fis.close();
}
if(psmnt != null){
psmnt.close();
}
if (connection!= null && !connection.isClosed()) {
connection.close();
}
}
于 2012-11-23T15:01:56.880 回答