JDBC supports the processing of text (CLOB) and binary (BLOB) files, such as accessing articles or images into a database. This is all solved by the idea of flow.
Two demos to see how JDBC operates text and binary files.
Clob:
Package Com.wxisme.jdbcclob;import Java.io.file;import Java.io.filenotfoundexception;import java.io.FileReader; Import Java.io.ioexception;import java.io.reader;import Java.sql.clob;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException; Import Java.sql.timestamp;import Java.text.dateformat;import Java.text.parseexception;import java.text.simpledateformat;/** * operation of the text object in the database. * @author Wxisme * */public class Clobtest {/** * Standard Time object converted to long * @param date * @return long */public static long Datetolo Ng (String date) {DateFormat format = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); long time = 0;try {time = Format.parse (da TE). GetTime ();} catch (ParseException e) {e.printstacktrace ();} return time;} /** * Insert a record with Clob text Object field into the database */public void Test1 () {Connection conn = null; PreparedStatement PS = null;try {class.forname ("Com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) { E.printstacktrace ();} Stringc = "Jdbc:mysql://localhost:3306/student?useunicode=true&characterencoding=utf8"; try {conn = Drivermanager.getconnection (c, "root", "1234"); String sql = "INSERT into student (NAME,PSWD,TIME,STIME,MYIMG) VALUES (?,?,?,?,?)"; PS = conn.preparestatement (sql); Java.sql.Date Date = new Java.sql.Date (Datetolong ("2015-5-14 9:25:32")); Timestamp stime = new Timestamp (Datetolong ("2015-5-14 9:25:32"));p s.setstring (1, "Zhang San");p s.setstring (2, "123456"); Ps.setdate (3, date);p S.settimestamp (4, stime);p S.setclob (5, New FileReader ("E:" +file.separator + "A.txt")); Ps.executeupdate ();} catch (SQLException e) {e.printstacktrace ();} catch (FileNotFoundException e) {e.printstacktrace ()}} public static void Main (string[] args) {//reads the Clob text object from the database and outputs connection conn = null; PreparedStatement PS = null; ResultSet rs = null; Reader r = null;try {class.forname ("Com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {e.printstacktrace ();} String C = "Jdbc:mysql://localhost:3306/student?useunicode=true&characterenCoding=utf8 "; try {conn = Drivermanager.getconnection (c," root "," 1234 "); String sql = "Select myimg from student where id=?"; PS = conn.preparestatement (sql);p S.setint (1, 1), rs = Ps.executequery (), while (Rs.next ()) {Clob Clob = Rs.getclob ("myimg" ); r = Clob.getcharacterstream (); int t = 0;while ((T=r.read ())! =-1) {System.out.println ((char) t);}}} catch (SQLException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Finally {try {rs.close ();} catch (SQLException e) {e.printstacktrace ();} try {ps.close ();} catch (SQLException e) {e.printstacktrace ();} try {r.close ();} catch (IOException e) {e.printstacktrace ();} try {conn.close ();} catch (SQLException e) {e.printstacktrace ();}}}}
Blob:
Package Com.wxisme.jdbcblob;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import Java.sql.blob;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException;/ * * Database operation binary file blob class use * @author wxisme * */public class BLOBTest {/** * insert binary */public void Test1 () {Connection Co in database) nn = null; PreparedStatement PS = null;try {class.forname ("Com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) { E.printstacktrace ();} String c = "Jdbc:mysql://localhost:3306/student?useunicode=true&characterencoding=utf8"; try {conn = Drivermanager.getconnection (c, "root", "1234"); String sql = "INSERT INTO Student (IMG) VALUES (?)"; PS = conn.preparestatement (sql);p S.setblob (1, New FileInputStream ("E:" + file.separator + "b.jpg"));p s.executeupdate () ;} catch (Sqlexception e) {e.printstacktrace ();} catch (FileNotFoundException e) {e.printstacktrace ();} Finally {try {ps.close ();} catch (SQLException e) {e.printstacktrace ();} try {conn.close ();} catch (SQLException e) {e.printstacktrace ();}}} public static void Main (string[] args) {//Read binary from database and restore connection conn = null; PreparedStatement PS = null; ResultSet rs = null;inputstream r = Null;outputstream os = null;try {class.forname ("Com.mysql.jdbc.Driver");} catch (Class Notfoundexception e) {e.printstacktrace ();} String c = "Jdbc:mysql://localhost:3306/student?useunicode=true&characterencoding=utf8"; try {conn = Drivermanager.getconnection (c, "root", "1234"); String sql = "Select img from student where id=?"; PS = conn.preparestatement (sql);p S.setint (1, 3); rs = Ps.executequery (); OS = new FileOutputStream (New File ("E:/e.jpg")), while (Rs.next ()) {blob blob = RS.GETBLOB ("img"); r = Blob.getbinarystream (); int t = 0;while ((T=r.read ())! =-1) {os.write (t);}}} catch (SQLException e) {e.printstacktrace ();} catch (FileNotfoundexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Finally {try {rs.close ();} catch (SQLException e) {e.printstacktrace ();} try {ps.close ();} catch (SQLException e) {e.printstacktrace ();} try {r.close ();} catch (IOException e) {e.printstacktrace ();} try {conn.close ();} catch (SQLException e) {e.printstacktrace ();} try {os.close ();} catch (IOException e) {e.printstacktrace ();}}}}
JDBC handles text and binary files