Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
package routines;
import java.io.*;
import java.sql.*;
public class GetBlobFromMysql {
public static void getBlob(int id) {
FileOutputStream image;
Connection con = null;
PreparedStatement pstmt = null;
Statement stmt = null;
ResultSet res = null;
StringBuffer query = null;
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String dbName = "register";
String userName = "root";
String password = "root";
try {
Class.forName(driverName);
con = DriverManager.getConnection(url + dbName, userName, password);
stmt = con.createStatement();
ResultSet rs = stmt
.executeQuery("select * from picture where image_id="+id);
if (rs.next()) {
Blob test = rs.getBlob("image");
InputStream x = test.getBinaryStream();
int size = x.available();
OutputStream out = new FileOutputStream("C:\\myfolder\\"+id+".jpg");
byte b[] = new byte;
x.read(b);
out.write(b);
}
} catch (Exception e) {
System.out.println("Exception :" + e);
} finally {
try {
stmt.close();
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
}
select cast(Data as char(10000) ) cdb_data from Accounts where DataKey = '13023512558'