http://www.easywayserver.com/blog/java-upload-image-in-database/
http://www.java2s.com/Code/Java/Database-SQL-JDBC/InsertpicturetoMySQL.htm
/*
Defining the Table: Oracle and MySql
create table MyPictures (
id INT PRIMARY KEY,
name VARCHAR(0),
photo BLOB
);
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertPictureToMySql {
public static void main(String[] args) throws Exception, IOException, SQLException {
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root");
String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (?, ?, ?)";
FileInputStream fis = null;
PreparedStatement ps = null;
try {
conn.setAutoCommit(false);
File file = new File("myPhoto.png");
fis = new FileInputStream(file);
ps = conn.prepareStatement(INSERT_PICTURE);
ps.setString(1, "001");
ps.setString(2, "name");
ps.setBinaryStream(3, fis, (int) file.length());
//ps.setBinaryStream(3,fis,fis.available());
ps.executeUpdate();
conn.commit();
} finally {
ps.close();
fis.close();
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Main {
static String url = "jdbc:oracle:thin:@localhost:1521:javaDemo";
static String username = "username";
static String password = "welcome";
public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT name, description, image FROM pictures ";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) {
String name = resultSet.getString(1);
String description = resultSet.getString(2);
File image = new File("D:\\java.gif");
FileOutputStream fos = new FileOutputStream(image);
byte[] buffer = new byte[1];
InputStream is = resultSet.getBinaryStream(3);
while (is.read(buffer) > 0) {
fos.write(buffer);
}
fos.close();
}
conn.close();
}
}
=======================================================
For several users, this was solved by getting the latest jdbc driver.
See these links:
http://forums.oracle.com/forums/thread.jspa?messageID=1808509
http://forum.java.sun.com/thread.jspa?threadID=5164500&tstart=255
Regards, Jan File file = new File("com/cisco/wipro/dca/GAJANANBABA.jpg");
fis = new FileInputStream(file);
ps = conn.prepareStatement(INSERT_PICTURE);
ps.setString(1, "001");
ps.setString(2, "psm");
ps.setBinaryStream(3, fis, (int) file.length());
//ps.setBinaryStream(3,fis,fis.available());
Blob blob = resultSerOfSelectAbove.getBlob(2);
OutputStream blobOs = blob.setBinaryStream(0);
byte[] buffer = <the XML file content>;
if ( (blobOs != null) && (buffer != null) ) {
blobOs.write(buffer);
blobOs.close();
} I see a patch for 11.2.0.2
Shared cursor graduated bind array can become inconsistent (ORA-1460) |
=============================================================