Working with big Data Objects
CLOB can store huge amounts of text
Large amounts of binary data can be stored in BLOBs
If you want to handle such large object operations in your program, you must do so with PreparedStatement , and all of the content is saved and read from the large text field in the form of IO streams.
Write large text data
The encoding of Chinese characters should be changed into GBK
=================================================//File name:clob_demo//------------------------------------- -----------------------------------------//Author:commonimport Java.io.file;import Java.io.fileinputstream;impo RT Java.io.inputstream;import Java.sql.*;import java.text.simpledateformat;//Main class//function:clob_demopublic class C Lob_demo {//database driver for MySQL definition public static final String dbdriver = "org.gjt.mm.mysql.Driver";//define the connection address of the MySQL database public Static final String dburl = "Jdbc:mysql://localhost:3306/mysql_demo"; connection user name for//mysql database public static final String DBUSER = "Root"; The connection password for the//mysql database public static final String Dbpass = "123456";p ublic static void Main (string[] args) throws Exception {//TODO auto-generated method stub Connection conn = null;//database connection PreparedStatement pstmt = null;//database operation string name = "Zhang San"; String sql = "INSERT into Userclob (name,note) VALUES (?,?)"; Class.forName (dbdriver);//Load driver//connection to MySQL database, write the username and password of the connection conn = Drivermanager.getconnection (dburl,dbuser,dbpass );p stmt = conn.preparestatement (SQL),//instantiate preparedstatement//declares a file object that is used to locate the large text file that you want to manipulate, "F = new" ("/home/common/ Software/database/zhangsan.txt "); InputStream input = null;//reads the contents through the input stream input = new FileInputStream (f);// Read file pstmt.setstring (1, name) via input stream; Set the first "? The content of Pstmt.setasciistream (2,input, (int) f.length ()); Set input stream pstmt.executeupdate ();//Perform database update operation Pstmt.close ();//Operation Close Conn.close ();//Database Close}}
Read large text fields
=================================================//File name:clob_demo//------------------------------------- -----------------------------------------//Author:commonimport Java.io.file;import Java.io.fileinputstream;impo RT Java.io.inputstream;import Java.sql.*;import Java.text.simpledateformat;import java.util.scanner;//main class//function : Clob_demopublic class Clob_demo {//database driver to define MySQL public static final String dbdriver = "org.gjt.mm.mysql.Driver";//Definition The connection address of the MySQL database is public static final String Dburl = "Jdbc:mysql://localhost:3306/mysql_demo"; connection user name for//mysql database public Static final String DBUSER = "root"; connection password for//mysql database public static final String dbpass = "123456";p ublic static void Main (St Ring[] args) throws exception{//TODO auto-generated method stub Connection conn = null;//database connection PreparedStatement pstmt = null;//Database operation result Set rs = null;//save result set int id = 2; String sql = "Select Name,note from Userclob WHERE id=?"; Class.forName (dbdriver);//Load driver//connection to MySQL database, write the username and password of the connection conn = DrivermaNager.getconnection (dburl,dbuser,dbpass);p stmt = conn.preparestatement (sql);//instancing Preparedstatementpstmt.setint (1 , id); Set the query's IDRs = Pstmt.executequery ();//Query while (Rs.next ()) {String name = rs.getstring (1); StringBuffer Note = new StringBuffer (); System.out.println ("Name:" +name); InputStream input = Rs.getasciistream (2);//Receive All text data Scanner scan = new Scanner (input) ;//Receive Data Scan.usedelimiter ("\ r \ n");//wrap file as delimiter while (Scan.hasnext ()) {Note.append (Scan.next ()). Append ("\ n");// Continuously read content}system.out.println ("content:" +note); Input.close ();} Pstmt.close ();//Operation Close Conn.close ();//Database Close}}
=================================================//File name:clob_demo//------------------------------------- -----------------------------------------//Author:commonimport Java.io.file;import Java.io.fileinputstream;impo RT Java.io.inputstream;import Java.sql.*;import Java.text.simpledateformat;import java.util.scanner;//main class//function : Clob_demopublic class Clob_demo {//database driver to define MySQL public static final String dbdriver = "org.gjt.mm.mysql.Driver";//Definition The connection address of the MySQL database is public static final String Dburl = "Jdbc:mysql://localhost:3306/mysql_demo"; connection user name for//mysql database public Static final String DBUSER = "root"; connection password for//mysql database public static final String dbpass = "123456";p ublic static void Main (St Ring[] args) throws exception{//TODO auto-generated method stub//connection conn = null;//database connection//preparedstatement pstmt = null;//database operation// String name = "Zhang San";//string sql = "INSERT into Userclob (name,note) VALUES (?,?)"; /class.forname (dbdriver);//Load driver////when connecting to MySQL database, write the username and password of the connection//conn = DrivermAnager.getconnection (dburl,dbuser,dbpass);//pstmt = conn.preparestatement (sql);//Instantiation preparedstatement//// Declares a file object that is used to locate the large text file to be manipulated//file f = new file ("/home/common/software/database/Untitled Document");//inputstream input = null;// Reads the content through the input stream//input = new FileInputStream (f);//reads the file through the input stream//pstmt.setstring (1, name); Set the first "? The content of//pstmt.setasciistream (2,input, (int) f.length ()); Set input stream//pstmt.executeupdate ();//Perform database update operation//pstmt.close ();//Operation Close//conn.close ();//Database off//connection conn = null;// Database connection//preparedstatement pstmt = null;//database operation//resultset rs = null;//save result set////int id = 2;//string sql = "Select Name,note From Userclob WHERE id=? "; /class.forname (Dbdriver)///load driver////when connecting to the MySQL database, write the username and password of the connection//conn = Drivermanager.getconnection (Dburl,dbuser, Dbpass);//pstmt = conn.preparestatement (sql);//Instantiate Preparedstatement//pstmt.setint (1, id); Set the query's Id//rs = Pstmt.executequery ();//Query////while (Rs.next ()) {//string name = rs.getstring (1);//stringbuffer Note = New StringBuffer ();//system.out.println ("Name:" +name);//iNputstream input = Rs.getasciistream (2);//Receive All text data//scanner scan = new Scanner (input)//Receive Data//scan.usedelimiter ("\r\ n ");//wrap the file as a delimiter//while (Scan.hasnext ()) {//note.append (Scan.next ()). Append (" \ n ");//constantly reading content//}// System.out.println ("content:" +note);//input.close ();//}////pstmt.close ();//Operation Close//conn.close ();//Database OFF connection conn = null;//database connection PreparedStatement pstmt = null;//database operation ResultSet rs = null;//save result set int id = 2; String sql = "Select Name,note from Userclob WHERE id=?"; Class.forName (dbdriver);//Load driver//connection to MySQL database, write the username and password of the connection conn = Drivermanager.getconnection (dburl,dbuser,dbpass );p stmt = conn.preparestatement (sql);//Instantiate Preparedstatementpstmt.setint (1, id); Set the query's IDRs = Pstmt.executequery (),//Query while (Rs.next ()) {String name = rs.getstring (1);//Remove the contents of the name column Clob c = Rs.getclob ( 2)///Remove large text data string note = c.getsubstring (1, (int) c.length ());//clob start position is 1system.out.println ("Name:" +name); System.out.println ("content:" +note); c.truncate (100); SYSTEM.OUT.PRINTLN ("Read content in section:" +c.getsubstring (1, (int) c.length ()));} Pstmt.close ();//Operation Close Conn.close ();//Database Close}}
Working with BLOB data
CREATE TABLE Userblob (ID INT auto_increment PRIMARY key,name VARCHAR () not Null,photo Longblob);
=================================================//File name:blob_demo//------------------------------------- -----------------------------------------//Author:commonimport Java.io.file;import Java.io.fileinputstream;impo RT Java.io.fileoutputstream;import Java.io.inputstream;import Java.sql.*;import Java.text.simpledateformat;import java.util.scanner;//Main class//function:blob_demopublic class Blob_demo {//database driver for definition MySQL public static final String DBD river = "Org.gjt.mm.mysql.Driver";//define the connection address of the MySQL database public static final String Dburl = "jdbc:mysql://localhost:3306/ Mysql_demo ";//mysql database connection user name public static final String DBUSER =" root ";//mysql database connection Password public static final String dbpass = "123456";p ublic static void Main (string[] args) throws exception{//TODO auto-generated method stub//connection conn = null;//database connection//prepa Redstatement pstmt = null;//database operation////string name = "Zhao Liu";//string sql = "INSERT into Userblob (Name,photo) VALUES (?,?)"; /class.forname (dbdriver);//load driver////connect MySQLDatabase, the user name and password to be written on the connection//conn = Drivermanager.getconnection (dburl,dbuser,dbpass);//pstmt = conn.preparestatement (sql);// Instantiation preparedstatement////declares a file object that is used to locate the large text files to be manipulated//file f = new file ("/home/common/software/database/photo.jpg");// InputStream input = null;//reads the contents through the input stream//input = new FileInputStream (f);//reads the file from the input stream//pstmt.setstring (1, name); Set the first "? The content of//pstmt.setbinarystream (2,input, (int) f.length ()); Set input stream//pstmt.executeupdate ();//Perform database update operation//pstmt.close ();//Operation Close//conn.close ();//Database off Connection conn = null;// Database connection PreparedStatement pstmt = null;//database operation ResultSet rs = null;//save result set int id = 1; String sql = "Select Name,photo from Userblob WHERE id=?"; Class.forName (dbdriver);//Load driver//connection to MySQL database, write the username and password of the connection conn = Drivermanager.getconnection (dburl,dbuser,dbpass );p stmt = conn.preparestatement (sql);//Instantiate Preparedstatementpstmt.setint (1, id);//Set Query IDRs = Pstmt.executequery (); /Query while (Rs.next ()) {String name = rs.getstring (1); StringBuffer Note = new StringBuffer (); System.out.println ("Name:" +name); INPUtstream input = Rs.getbinarystream (2);//Receive all large text data fileoutputstream out = Null;out = new FileOutputStream ("/ Home/common/software/database/photo_copy.jpg ")); int temp = 0;while ((temp = Input.read ())! =-1) {//side read Write Out.write (temp) ;} Input.close (); Out.close ();} Pstmt.close ();//Operation Close Conn.close ();//Database Close}}
Java database--processing Big Data Objects