List of usage examples for java.sql Blob length
long length() throws SQLException;
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);/* w ww . j a v a2 s.co m*/ 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);//from w w w . j a va 2s . co m 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 www. j a va 2s. c o m*/ 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:Blobs.java
public static void main(String args[]) { if (args.length != 1) { System.err.println("Syntax: <java Blobs [driver] [url] " + "[uid] [pass] [file]"); return;// www . j a v a2 s.co m } 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:Main.java
public static byte[] getBLOB(int id, Connection conn) throws Exception { ResultSet rs = null;/*w w w .j a v a 2 s . com*/ PreparedStatement pstmt = null; String query = "SELECT photo FROM MyPictures WHERE id = ?"; try { pstmt = conn.prepareStatement(query); pstmt.setInt(1, id); rs = pstmt.executeQuery(); rs.next(); Blob blob = rs.getBlob(3); // materialize BLOB onto client return blob.getBytes(1, (int) blob.length()); } finally { rs.close(); pstmt.close(); conn.close(); } }
From source file:Main.java
public static byte[] getBLOB(int id, Connection conn) throws Exception { ResultSet rs = null;/*from w ww . ja va2 s. c om*/ PreparedStatement pstmt = null; String query = "SELECT photo FROM MyPictures WHERE id = ?"; try { pstmt = conn.prepareStatement(query); pstmt.setInt(1, id); rs = pstmt.executeQuery(); rs.next(); Blob blob = rs.getBlob("photo"); // materialize BLOB onto client return blob.getBytes(1, (int) blob.length()); } finally { rs.close(); pstmt.close(); conn.close(); } }
From source file:com.espertech.esperio.db.core.DBUtil.java
private static byte[] getBlobValue(Blob blob) throws SQLException { if (blob == null) { return null; }//w ww . j a v a 2 s. c o m if (blob.length() > Integer.MAX_VALUE) { log.warn("Blob truncated: value larger then Integer.MAX_VALUE bytes:" + blob.length()); return null; } int len = (int) blob.length(); return blob.getBytes(1, len); }
From source file:com.webbfontaine.valuewebb.irms.impl.data.AttachedDocDataSource.java
private static InputStream stream(Blob data) throws SQLException { if (data != null) { LOGGER.debug("Attached doc LOB data length: {}", data.length()); return data.getBinaryStream(); }//from w ww .java2 s . c o m return null; }
From source file:procuradoria.pdf.util.DocumentsPdf.java
public static void byteArrayToFile(Uzatdocs doc) { try {// w w w . ja v a 2 s . c o m Blob blob = doc.getUzatdocsArchivo(); int blobLength = (int) blob.length(); byte[] bArray = blob.getBytes(1, blobLength); blob.free(); OutputStream out = new FileOutputStream("out.pdf"); out.write(bArray); out.close(); } catch (Exception ex) { log.level.info(">> Exception " + ex.getMessage()); } }
From source file:procuradoria.pdf.util.DocumentsPdf.java
public static Boolean CreateFilePDF(Uzatdocs document, String filePath) { Boolean exito = false;/*from w w w. j a va 2 s. com*/ try { String fileName = document.getId().getUzatdocsId() + ".pdf"; Blob blob = document.getUzatdocsArchivo(); int blobLength = (int) blob.length(); byte[] bArray = blob.getBytes(1, blobLength); OutputStream out = new FileOutputStream(filePath + fileName); out.write(bArray); out.close(); exito = true; } catch (Exception ex) { log.level.info(">> Exception " + ex.getMessage()); } return exito; }