Implement save text large text and blob binary data in MySQL database

Source: Internet
Author: User

The project code is as follows:

Config configuration file:

Classname=com.mysql.jdbc.driverurl=jdbc:mysql://localhost:3306/lobuser=rootpassword=root

Com.itheima.util bag under Dbutil.java

Package Com.itheima.util;import Java.io.filenotfoundexception;import Java.io.filereader;import java.io.IOException ; Import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;import Java.util.properties;public class DBUtil {private static Properties Properties = null;static {properties = new properties (); try {properties.load (new FileReader ( DBUtil.class.getClassLoader (). GetResource ("Config.properties"). GetPath ());} catch (FileNotFoundException e) {e.printstacktrace (); throw new RuntimeException (e);} catch (IOException e) { E.printstacktrace (); throw new RuntimeException (e);}} public static Connection Getconn () {String className = Properties.getproperty ("ClassName"); String url = properties.getproperty ("url"); String user = Properties.getproperty ("user"); String Password = properties.getproperty ("password"); Connection conn;try {class.forname (className); conn = drivermanager.getconnection (URL, user, password);} catch (SQLException e) {e.printstacktrace (); throw new RuntimeException (e);} catch (ClassNotFoundException e) { E.printstacktrace (); throw new RuntimeException (e);} Return conn;} public static void Close (Connection conn, PreparedStatement PS, ResultSet rs) {if (rs! = null) {try {rs.close ();} catch (S Qlexception e) {rs = null;}} if (PS! = null) {try {ps.close ();} catch (SQLException e) {PS = null;}} IF (conn! = null) {try {conn.close ();} catch (SQLException e) {conn = null;}}}

Com.itheima.lob packet Textdemo.java (for storing large text data)

Package Com.itheima.lob;import Java.io.file;import Java.io.filenotfoundexception;import java.io.FileReader;import Java.io.filewriter;import Java.io.ioexception;import Java.io.reader;import Java.io.writer;import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException; Import Org.junit.test;import Com.itheima.util.dbutil;public class Textdemo {/* CREATE TABLE Textdemo (ID int primary key  Auto_increment, name varchar (a), content Mediumtext); */@Testpublic void GetText () {Connection conn = null; PreparedStatement PS = null; ResultSet rs = null; String sql = "SELECT * from Textdemo"; try {conn = Dbutil.getconn ();p s = conn.preparestatement (sql); rs = Ps.executequery (); while (Rs.next ()) {String name = rs.getstring (2); Reader reader = Rs.getcharacterstream (3); Writer writer = new FileWriter (name), int len = 0;char []data = new Char[1024];while (len = reader.read (data))! =-1) {writ Er.write (data, 0, Len);}}} catch (SQLException e) {E.printstacktrace(); throw new RuntimeException (e);} catch (IOException e) {e.printstacktrace (); throw new RuntimeException (e);} finally {dbutil.close (conn, PS, RS);}} @Testpublic void AddText () {Connection conn = null; PreparedStatement PS = null; ResultSet rs = null; String sql = "INSERT into Textdemo values (null,?,?)"; try {conn = Dbutil.getconn ();p s = conn.preparestatement (sql);p s.setstring (1), "How does the steel be smelted into. txt"); File File = new file ("1.txt");p S.setcharacterstream (2, new FileReader (file), (int) file.length ());p s.executeupdate (); catch (SQLException e) {e.printstacktrace (); throw new RuntimeException (e);} catch (FileNotFoundException e) { E.printstacktrace (); throw new RuntimeException (e);} finally {dbutil.close (conn, PS, RS);}}}

Com.itheima.lob Blobdemo.java (implements storage of sophomore data)

Package Com.itheima.lob;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.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;import org.junit.test;import com.itheima.util.dbutil;/* CREATE TABLE Blobdemo (ID int primary key Auto_increment, name varchar (), content blob); */public class Blobdemo {@Testpublic void GetBlob () {Connection conn = null; PreparedStatement PS = null; ResultSet rs = null; String sql = "SELECT * from Blobdemo"; try {conn = Dbutil.getconn ();p s = conn.preparestatement (sql); rs = Ps.executequery (); while (Rs.next ()) {String name = rs.getstring (2), InputStream is = Rs.getblob (3). Getbinarystream (); OutputStream os = new Fi Leoutputstream (name); int len = 0;byte[] data = new Byte[1024];while (len = is.read (data))! =-1) {os.write (data, 0, Len);} }} catch (Sqlexception e) {e.printstacktrace (); throw new RuntimeException (e);} catch (IOException e) {e.printstacktrace (); throw new RuntimeException (e);} finally {dbutil.close (conn, PS, RS);}} @Testpublic void Addblob () {Connection conn = null; PreparedStatement PS = null; ResultSet rs = null; String sql = "INSERT into Blobdemo values (null,?,?)"; try {conn = Dbutil.getconn ();p s = conn.preparestatement (sql);p s.setstring (1, "Roy MP3"); File File = new file ("1.mp3");p S.setbinarystream (2, new FileInputStream (file), (int) file.length ());p s.executeupdate () ;} catch (SQLException e) {e.printstacktrace (); throw new RuntimeException (e);} catch (FileNotFoundException e) { E.printstacktrace (); throw new RuntimeException (e);} finally {dbutil.close (conn, PS, RS);}}}



It is important to note that:

Ps.setcharacterstream (2, new FileReader (file), (int) file.length ()); There are three kinds of overloaded forms

Ps.setbinarystream (2, new FileInputStream (file), (int) file.length ()); There are three kinds of overloaded forms

These two methods are only the second overloaded form of the MySQL jar package implemented, the other two are not implemented, it is an abstract method, so if you use these two methods will prompt the abstract method error

There is also if the upload, prompt java.lang.OutOfMemoryError, then the virtual machine memory is exceeded, which can be clicked

-xms64m set virtual machine minimum memory to 64M

-xmx256m set virtual machine maximum memory to 256M

Note: jdk5.0 previous virtual machine default maximum memory is 64M



If Com.mysql.jdbc.PacketTooBigException is prompted, the packet passed between the program and the database exceeds the size of the default packet, which can be resolved by modifying the configuration file My.ini the MySQL directory.

Just add a row of Max_allowed_packet property values under the "Mysqld" tab.

[mysqld]max_allowed_packet=64m




Implement save text large text and blob binary data in MySQL database

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.