Java NOTES: Java database Programming (IV): Working with big Data Objects

Source: Internet
Author: User

Big Data Objects mainly refer to Clob and blob two types of fields, in the CLOB can store a large amount of text, such as storing a novel. In a blob, you can store binary files, such as movies, pictures, and so on, if you want to work with large data objects in your program, you must use PreparedStatement to complete all of the content to be saved and read in the bulk of the text through the IO stream.



Main methods for writing Big Data objects:


Main ways to read large data objects:



Processing CLOB Data

CLOB represents large text data (Character Large Object), which provides a longtext field in MySQL that represents large text data, and the maximum storage capacity for this field is 4G.

The contents of the file can be read by 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.Scanner;p ublic class clobdemo02{//defines the database driver for MySQL 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 ";//connection user name of MySQL database public static final String DBUSER =" root ";//MySQL database connection password public static final string dbpass =" M Ysqladmin ";p ublic static void Main (String args[]) throws exception{//all exceptions thrown connection conn = null;/database connection Preparedstate ment pstmt = null; ResultSet rs = null; int id = 1;//read number String sql = "Select Name,note from Userclob WHERE id=?"; Class.forName (dbdriver);//LOAD Driver conn = drivermanager.getconnection (dburl,dbuser,dbpass);p stmt = Conn.preparestatement (SQL);//Create Preapredstatement Object Pstmt.setint (1,id); rs = Pstmt.executeqUery (); if (Rs.next ()) {String name = rs.getstring (1); StringBuffer Note = new StringBuffer (); System.out.println ("Name:" + name); InputStream input = Rs.getasciistream (2); Scanner scan = new Scanner (input),//Use Scanner class to read the content scan.usedelimiter ("\ r \ n");//wrap the file as a separator while (Scan.hasnext ()) { Note.append (Scan.next ()). Append ("\ n");} System.out.println ("Content:" + note); Input.close ();} Rs.close ();p stmt.close (); Conn.close ();//Database Close}};


Clob class:

You can also use the Getclob () method in the ResultSet to change the contents of the Clob object, directly using CLOB can easily get big data text, or the text data can be easily manipulated.

Import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import Java.sql.PreparedStatement, import java.sql.Clob, import java.sql.ResultSet;p ublic class clobdemo03{// Define MySQL database driver public static final String dbdriver = "org.gjt.mm.mysql.Driver";//define the connection address of the MySQL database public static final STR ing dburl = "jdbc:mysql://localhost:3306/root";//MySQL database connection user name public static final String DBUSER = "root";//MySQL database connected Connect password public static final String Dbpass = "Mysqladmin";p ublic static void Main (String args[]) throws exception{//all exceptions thrown conn Ection 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 Driver conn = drivermanager.getconnection (dburl,dbuser,dbpass);p stmt = Conn.preparestatement (SQL);//Create Preapredstatement Object Pstmt.setint (1,id); rs = Pstmt.executequery (); 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);//Read only 100 content System.out.println ("partially read content:" + c.getsubstring (1, (int) C.length ()));} Rs.close ();p stmt.close (); Conn.close ();//Database Close}};

Processing BLOB data BLOB data is similar to CLOB data, designed to hold binary data, declared in MySQL using Longblob, and can hold up to 4G of content.

Io stream reads:

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;p ublic class blobdemo02{// Define MySQL database driver public static final String dbdriver = "org.gjt.mm.mysql.Driver";//define the connection address of the MySQL database public static final STR ing dburl = "jdbc:mysql://localhost:3306/mldn";//MySQL database connection user name public static final String DBUSER = "root";//MySQL database connected Connect password public static final String Dbpass = "Mysqladmin";p ublic static void Main (String args[]) throws exception{//all exceptions thrown conn Ection 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 Driver conn = drivermanager.getconnection (dburl,dbuser,dbpass);p stmt = Conn.preparestatement (SQL);p stmt.setint (1,id); rs = Pstmt.executeqUery ();//Execute 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) {//side read Write Out.write (temp);} Input.close (); Out.close ();} Pstmt.close (); Conn.close ();//Database Close}};

You can also work with 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;p ublic Class blobdemo03{//defines the database driver for MySQL 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 user name public static Final string DBUSER = "root";//connection password for MySQL database public static final String dbpass = "Mysqladmin";p ublic static void Main (St Ring args[]) throws exception{//all exceptions thrown 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 Driver conn = drivermanager.getconnection (dburl,dbuser,dbpass);p stmt = Conn.preparestatement (SQL);p Stmt.setint (1,id); rs = Pstmt.executequery ();//Execute 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 ();//Database Close}};


This article mainly summarizes the operation of large data objects in the Java database, but from the actual use and development of the process, it is an unwise choice to put too large files in the database, the data is too large is often used in the way of mapping path written.





Java NOTES: Java database Programming (IV): Working with big Data Objects

Related Article

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.