List of usage examples for java.sql ResultSet getBlob
Blob getBlob(String columnLabel) throws SQLException;
ResultSet
object as a Blob
object in the Java programming language. From source file:EmployeeShow.java
public static void main(String[] args) throws Exception { ImageIcon image;/*from w ww . ja va 2 s . c o m*/ Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/c:\\employee"); Statement s = con.createStatement(); ResultSet rs = s.executeQuery("select photo from employee where name = 'Duke'"); if (rs.next()) { Blob photo = rs.getBlob(1); ObjectInputStream ois = null; ois = new ObjectInputStream(photo.getBinaryStream()); image = (ImageIcon) ois.readObject(); } s.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);/* ww w . ja v a 2 s . com*/ String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT col_blob FROM mysql_all_table"); if (rs.next()) { Blob blob = rs.getBlob("col_blob"); long blobLength = blob.length(); int pos = 1; // position is 1-based int len = 10; byte[] bytes = blob.getBytes(pos, len); InputStream is = blob.getBinaryStream(); int b = is.read(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd"); Statement stmt = conn.createStatement(); createBlobClobTables(stmt);//www . ja va2 s.c om PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)"); File file = new File("blob.txt"); FileInputStream fis = new FileInputStream(file); pstmt.setBinaryStream(1, fis, (int) file.length()); file = new File("clob.txt"); fis = new FileInputStream(file); pstmt.setAsciiStream(2, fis, (int) file.length()); fis.close(); pstmt.execute(); ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40"); rs.next(); java.sql.Blob blob = rs.getBlob(2); java.sql.Clob clob = rs.getClob(3); byte blobVal[] = new byte[(int) blob.length()]; InputStream blobIs = blob.getBinaryStream(); blobIs.read(blobVal); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(blobVal); blobIs.close(); char clobVal[] = new char[(int) clob.length()]; Reader r = clob.getCharacterStream(); r.read(clobVal); StringWriter sw = new StringWriter(); sw.write(clobVal); r.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd"); Statement stmt = conn.createStatement(); createBlobClobTables(stmt);//from ww w . j a va 2s. com PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)"); File file = new File("blob.txt"); FileInputStream fis = new FileInputStream(file); pstmt.setBinaryStream(1, fis, (int) file.length()); file = new File("clob.txt"); fis = new FileInputStream(file); pstmt.setAsciiStream(2, fis, (int) file.length()); fis.close(); pstmt.execute(); ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40"); rs.next(); java.sql.Blob blob = rs.getBlob(2); java.sql.Clob clob = rs.getClob("myClobColumn"); byte blobVal[] = new byte[(int) blob.length()]; InputStream blobIs = blob.getBinaryStream(); blobIs.read(blobVal); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(blobVal); blobIs.close(); char clobVal[] = new char[(int) clob.length()]; Reader r = clob.getCharacterStream(); r.read(clobVal); StringWriter sw = new StringWriter(); sw.write(clobVal); r.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String WRITE_OBJECT_SQL = "BEGIN " + " INSERT INTO java_objects(object_id, object_name, object_value) " + " VALUES (?, ?, empty_blob()) " + " RETURN object_value INTO ?; " + "END;"; String READ_OBJECT_SQL = "SELECT object_value FROM java_objects WHERE object_id = ?"; Connection conn = getOracleConnection(); conn.setAutoCommit(false);// w w w . j a va 2 s. c om List<Object> list = new ArrayList<Object>(); list.add("This is a short string."); list.add(new Integer(1234)); list.add(new java.util.Date()); // write object to Oracle long id = 0001; String className = list.getClass().getName(); CallableStatement cstmt = conn.prepareCall(WRITE_OBJECT_SQL); cstmt.setLong(1, id); cstmt.setString(2, className); cstmt.registerOutParameter(3, java.sql.Types.BLOB); cstmt.executeUpdate(); BLOB blob = (BLOB) cstmt.getBlob(3); OutputStream os = blob.getBinaryOutputStream(); ObjectOutputStream oop = new ObjectOutputStream(os); oop.writeObject(list); oop.flush(); oop.close(); os.close(); // Read object from oracle PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL); pstmt.setLong(1, id); ResultSet rs = pstmt.executeQuery(); rs.next(); InputStream is = rs.getBlob(1).getBinaryStream(); ObjectInputStream oip = new ObjectInputStream(is); Object object = oip.readObject(); className = object.getClass().getName(); oip.close(); is.close(); rs.close(); pstmt.close(); conn.commit(); // de-serialize list a java object from a given objectID List listFromDatabase = (List) object; System.out.println("[After De-Serialization] list=" + listFromDatabase); conn.close(); }
From source file:DemoPreparedStatementSetBlob.java
public static void main(String[] args) throws Exception { Connection conn = null;/* ww w . j av a 2s .c o m*/ PreparedStatement pstmt = null; ResultSet rs = null; java.sql.Blob blob = null; try { conn = getConnection(); // prepare blob object from an existing binary column pstmt = conn.prepareStatement("select photo from my_pictures where id = ?"); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); rs.next(); blob = rs.getBlob(1); // prepare SQL query for inserting a new row using setBlob() String query = "insert into blob_table(id, blob_column) values(?, ?)"; // begin transaction conn.setAutoCommit(false); pstmt = conn.prepareStatement(query); pstmt.setString(1, "0002"); pstmt.setBlob(2, blob); int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); // end transaction conn.commit(); } finally { rs.close(); pstmt.close(); conn.close(); } }
From source file:DemoDisplayBlobFromDatabase.java
public static void main(String args[]) throws Exception { Connection conn = null;/*from w w w . j a v a 2 s.c om*/ ResultSet rs = null; PreparedStatement pstmt = null; String query = "SELECT blob_column FROM blob_table WHERE id = ?"; try { conn = getConnection(); pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); rs.next(); // materialize binary data onto client java.sql.Blob blob = rs.getBlob(1); } finally { rs.close(); pstmt.close(); conn.close(); } }
From source file:Blobs.java
public static void main(String args[]) { if (args.length != 1) { System.err.println("Syntax: <java Blobs [driver] [url] " + "[uid] [pass] [file]"); return;/*from ww w .jav a 2s . c om*/ } try { Class.forName(args[0]).newInstance(); Connection con = DriverManager.getConnection(args[1], args[2], args[3]); File f = new File(args[4]); PreparedStatement stmt; if (!f.exists()) { // if the file does not exist // retrieve it from the database and write it to the named file ResultSet rs; stmt = con.prepareStatement("SELECT blobData " + "FROM BlobTest " + "WHERE fileName = ?"); stmt.setString(1, args[0]); rs = stmt.executeQuery(); if (!rs.next()) { System.out.println("No such file stored."); } else { Blob b = rs.getBlob(1); BufferedOutputStream os; os = new BufferedOutputStream(new FileOutputStream(f)); os.write(b.getBytes(0, (int) b.length()), 0, (int) b.length()); os.flush(); os.close(); } } else { // otherwise read it and save it to the database FileInputStream fis = new FileInputStream(f); byte[] tmp = new byte[1024]; byte[] data = null; int sz, len = 0; while ((sz = fis.read(tmp)) != -1) { if (data == null) { len = sz; data = tmp; } else { byte[] narr; int nlen; nlen = len + sz; narr = new byte[nlen]; System.arraycopy(data, 0, narr, 0, len); System.arraycopy(tmp, 0, narr, len, sz); data = narr; len = nlen; } } if (len != data.length) { byte[] narr = new byte[len]; System.arraycopy(data, 0, narr, 0, len); data = narr; } stmt = con.prepareStatement("INSERT INTO BlobTest(fileName, " + "blobData) VALUES(?, ?)"); stmt.setString(1, args[0]); stmt.setObject(2, data); stmt.executeUpdate(); f.delete(); } con.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.plexobject.testplayer.dao.jdbc.GenericDaoJdbc.java
protected static Object getBlobObject(ResultSet rs, int n) throws SQLException { Blob blob = rs.getBlob(n); return toObject(blob); }
From source file:org.etudes.jforum.dao.oracle.OracleUtils.java
/** * The query should look like:/*from ww w . j av a 2 s .c o m*/ * * SELECT blob_field from any_table WHERE id = ? FOR UPDATE * * BUT KEEP IN MIND: * * When you insert record in previous step, it should go with empty_blob() like: * * INSERT INTO jforum_posts_text ( post_text ) VALUES (EMPTY_BLOB()) * * @param query * @param idForQuery * @param value * @throws IOException * @throws SQLException */ public static void writeBlobUTF16BinaryStream(String query, int idForQuery, String value) throws IOException, SQLException { PreparedStatement p = JForum.getConnection().prepareStatement(query); p.setInt(1, idForQuery); ResultSet rs = p.executeQuery(); rs.next(); Blob postText = rs.getBlob(1); //wipe out the Blob contents postText.truncate(0); if (logger.isDebugEnabled()) logger.debug("post test is a " + postText.getClass().getName()); // OutputStream blobWriter = ((oracle.sql.BLOB)postText).setBinaryStream(0L); OutputStream blobWriter = postText.setBinaryStream(0L); blobWriter.write(value.getBytes("UTF-16")); blobWriter.flush(); blobWriter.close(); rs.close(); p.close(); }