JAVA notes: Java Database Programming (4): Processing Big Data Objects, java Database Programming

Source: Internet
Author: User

JAVA notes: Java Database Programming (4): Processing Big Data Objects, java Database Programming

Big Data Objects refer to two types of fields: CLOB and BLOB. CLOB can store massive volumes of text, such as a novel. Binary files, such as movies and images, can be stored in BLOB. To process large data objects in a program, you must use PreparedStatement, all content must be saved and read from the large field text through the IO stream.



The main methods for writing data objects are as follows:


The main methods for reading big data objects are as follows:



Processing CLOB data

CLOB indicates Character Large Object. In MySQL, The LONGTEXT field is provided to indicate big text data. The maximum storage capacity of this field is 4 GB.

The file content can be read through the IO stream:

Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. io. file; import java. io. fileInputStream; import java. io. inputStream; import java. util. logging; public class ClobDemo02 {// defines the MySQL database driver public static final String DBDRIVER = "org. gjt. mm. mysql. driver "; // define the MySQL database connection address public static fina L String DBURL = "jdbc: mysql: // localhost: 3306/root"; // MySQL database connection username public static final String DBUSER = "root "; // MySQL database Connection password public static final String DBPASS = "mysqladmin"; public static void main (String args []) throws Exception {// All exceptions throw Connection conn = null; // database connection PreparedStatement pstmt = null; ResultSet rs = null; int id = 1; // read number String SQL = "SELECT name, note FROM userclob WHERE Id =? "; Class. forName (DBDRIVER); // load the driver conn = DriverManager. getConnection (DBURL, DBUSER, DBPASS); pstmt = conn. prepareStatement (SQL); // create the PreapredStatement object pstmt. setInt (1, id); rs = pstmt.exe cuteQuery (); if (rs. next () {String name = rs. getString (1); StringBuffer note = new StringBuffer (); System. out. println ("name:" + name); InputStream input = rs. getAsciiStream (2); external scan = new external (input); // use the external class to read the scan content. useDelimiter ("\ r \ n"); // use the line feed of the file as the delimiter while (scan. hasNext () {note. append (scan. next ()). append ("\ n");} System. out. println ("content:" + note); input. close ();} rs. close (); pstmt. close (); conn. close (); // close the database }};


CLOB class:

You can also use the getClob () method in the ResultSet to convert all the content into the content of the Clob object, and directly use Clob to conveniently obtain the big data text, you can also perform simple operations on text data.

Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import java. SQL. preparedStatement; import java. SQL. clob; import java. SQL. resultSet; public class ClobDemo03 {// define the MySQL database driver 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/root "; // MySQL database connection username public static f Inal String DBUSER = "root"; // MySQL database connection password public static final String DBPASS = "mysqladmin"; public static void main (String args []) throws Exception {// All exceptions throw Connection conn = null; // database Connection PreparedStatement pstmt = null; ResultSet rs = null; int id = 1; // read id String SQL = "SELECT name, note FROM userclob WHERE id =? "; Class. forName (DBDRIVER); // load the driver conn = DriverManager. getConnection (DBURL, DBUSER, DBPASS); pstmt = conn. prepareStatement (SQL); // create the PreapredStatement object pstmt. setInt (1, id); rs = pstmt.exe cuteQuery (); if (rs. next () {String name = rs. getString (1); System. out. println ("name:" + name); Clob c = rs. getClob (2); String note = c. getSubString (1, (int) c. length (); System. out. println ("content:" + note); c. truncate (100); // only 100 contents can be read: System. out. println ("partially read content:" + c. getSubString (1, (int) c. length ();} rs. close (); pstmt. close (); conn. close (); // close the database }};

Processing BLOB Data BLOB data is similar to CLOB data. It is used to store binary data. It is declared in MySQL using LONGBLOB to store up to 4 GB of content.

Read IO streams:

Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import java. SQL. preparedStatement; import java. io. file; import java. io. fileOutputStream; import java. SQL. resultSet; import java. io. inputStream; import java. io. inputStream; import java. io. outputStream; public class BlobDemo02 {// defines the MySQL database driver public static final String DBDRIVER = "org. gjt. mm. mysql. driver "; // set The connection address of the MySQL database is public static final String DBURL = "jdbc: mysql: // localhost: 3306/mldn "; // MySQL database connection username public static final String DBUSER = "root"; // MySQL database connection password public static final String DBPASS = "mysqladmin "; public static void main (String args []) throws Exception {// All exceptions throw Connection conn = null; // database Connection PreparedStatement pstmt = null; ResultSet rs = null; int id = 1; String SQL = "SELECT name, Photo FROM userblob WHERE id =? "; Class. forName (DBDRIVER); // load the driver conn = DriverManager. getConnection (DBURL, DBUSER, DBPASS); pstmt = conn. prepareStatement (SQL); pstmt. setInt (1, id); rs = pstmt.exe cuteQuery (); // execute the query if (rs. next () {String name = rs. getString (1); System. out. println ("name:" + name); InputStream input = rs. getBinaryStream (2); File f = new File ("d:" + File. separator + "loadmldn.gif"); // picture file OutputStream out = null; out = New FileOutputStream (f); int temp = 0; while (temp = input. read ())! =-1) {// read and write out. write (temp);} input. close (); out. close ();} pstmt. close (); conn. close (); // close the database }};

You can also perform operations through the BLOB class:

Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import java. SQL. preparedStatement; import java. SQL. blob; import java. SQL. resultSet; import java. io. file; import java. io. fileOutputStream; import java. io. inputStream; import java. io. inputStream; import java. io. outputStream; public class BlobDemo03 {// defines the MySQL database driver 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/mldn "; // MySQL database connection username public static final String DBUSER = "root"; // MySQL database connection password public static final String DBPASS = "mysqladmin "; public static void main (String args []) throws Exception {// All exceptions throw Connection conn = null; // database Connection PreparedStatement pstmt = null; ResultSet rs = null; int id = 1; Stri Ng SQL = "SELECT name, photo FROM userblob WHERE id =? "; Class. forName (DBDRIVER); // load the driver conn = DriverManager. getConnection (DBURL, DBUSER, DBPASS); pstmt = conn. prepareStatement (SQL); pstmt. setInt (1, id); rs = pstmt.exe cuteQuery (); // execute the query if (rs. next () {String name = rs. getString (1); System. out. println ("name:" + name); Blob B = rs. getBlob (2); File f = new File ("d:" + File. separator + "loadmldn.gif"); // picture file OutputStream out = null; out = new FileOutputStream (f); out. write (B. getBytes (1, (int) B. length (); out. close ();} pstmt. close (); conn. close (); // close the database }};


This article mainly summarizes the operations on Big Data Objects in the Java database. However, from the perspective of actual use and development, it is unwise to put too many files in the database, when the data size is too large, the ing path is usually used for writing.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.