Java implementation inserts Mysql binaries, blob types, problems encountered and solutions _mysql

Source: Internet
Author: User
Tags int size wrapper
The first is the database establishment to be prepared:
We're going to set the drop binary field to a BLOB type, select the appropriate BLOB type based on the size of the file, and the size of the binaries that each BLOB type can hold.
Four types of blob for MySQL
Type size (units: bytes)
Tinyblob Max 255
Blob Max 65K
Mediumblob Max 16M
Longblob Max 4G
Here is the specific operation code:
Copy Code code as follows:

/**
*
* Store binary files (this binary can be a local hard drive path or a network path) into the database
* Create DATE:2009-5-13 Author:administrator
*
* @param file
* Can be a local file or a network file
* @param Conn
*/
public void Savebinary (String file, Connection conn) {
Note the classes used by the binaries when they are written to the database, and the class wrapper conversion process
File f = null;
if (File.tolowercase (). Contains ("http:")
f = downloadwithurl.downloadfile (file);
Else
f = new file (file);
if (f!= null) {
try {
InputStream is = new FileInputStream (f);
PreparedStatement PS = conn
. preparestatement ("INSERT into Bankvoice (Name,text) VALUES (?,?)");
Ps.setstring (1, file);
int i = is.available ();
Ps.setbinarystream (2, is, is.available ());
Ps.executeupdate ();
SYSTEM.OUT.PRINTLN ("Binary file Insert succeeded");
Ps.clearparameters ();
Ps.close ();
Is.close ();
catch (Exception e) {
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("exception occurred while inserting binaries");
}
}
}

Note that the following exception occurs when the operation, then we only need to do the setting: Take my local example: Into the D:\MySql5.0\mysql-5.0.51b-win32 directory, the following files can be seen: My-large.ini, My-small.ini, My-medium.ini, My-huge.ini
We put the config item in the INI file that just put the MySQL service in the current load: Max_allowed_packet to 16M
That is: Max_allowed_packet = 16M The default is 1M we change to 16M, and then restart the MySQL server, so there will not be the following exception.
Copy Code code as follows:

Com.mysql.jdbc.PacketTooBigException:Packet for query is too large (1048587 > 1047552). You can change this value on the server by setting the Max_allowed_packet ' variable.
At Com.mysql.jdbc.MysqlIO.send (mysqlio.java:2632)
At Com.mysql.jdbc.MysqlIO.send (mysqlio.java:2618)
At Com.mysql.jdbc.MysqlIO.sendCommand (mysqlio.java:1551)
At Com.mysql.jdbc.ServerPreparedStatement.storeStream (serverpreparedstatement.java:2180)
At Com.mysql.jdbc.ServerPreparedStatement.serverLongData (serverpreparedstatement.java:1199)
At Com.mysql.jdbc.ServerPreparedStatement.serverExecute (serverpreparedstatement.java:1004)
At Com.mysql.jdbc.ServerPreparedStatement.executeInternal (serverpreparedstatement.java:670)
At Com.mysql.jdbc.PreparedStatement.executeUpdate (preparedstatement.java:1159)
At Com.mysql.jdbc.PreparedStatement.executeUpdate (preparedstatement.java:1076)
At Com.mysql.jdbc.PreparedStatement.executeUpdate (preparedstatement.java:1061)
At SaveBinaryToDB.SaveBinaryToDB.saveBinary (savebinarytodb.java:33)
At SaveBinaryToDB.SaveBinaryToDB.main (savebinarytodb.java:17)
/**
* Read binary files from the database create DATE:2009-5-13 author:administrator
*
* @param file
* @param Conn
*/
public void Getbinary (String file, Connection conn) {
Note the classes that the binaries use when reading from the database, and the wrapper conversion process for the class
try {
PreparedStatement PS = conn
. Preparestatement ("Select text from Bankvoice where name=?");
Ps.setstring (1, file);
Blob blob = null;
ResultSet rs = Ps.executequery ();
if (Rs.next ()) {
Blob = (BLOB) rs.getblob ("text");
}
FileOutputStream fos = new FileOutputStream ("D:\\test1.mp3");
Fos.write (Blob.getbytes (1, (int) blob.length ()));
SYSTEM.OUT.PRINTLN ("binary file succeeded");
Ps.clearparameters ();
Ps.close ();
Fos.close ();
catch (Exception e) {
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("Exception occurred while reading binary files");
}
}

package savebinarytodb;
Copy Code code as follows:

/**
* The function of this program to achieve network download
* Download the file of the specified URL to the local hard drive
*
*/
Import java.io.*;
Import java.net.*;
/**
* @todo to store images, MP3 and other files on the web to local
*
* @version 1.0
*/
public class Downloadwithurl {
public static File DownLoadFile (String fromurl) {
URL url;
File file = null;
try {
url = new
URL ("http://count.koubei.com/showphone/showphone.php?f=jpg&w=96&h=10&bc=255,255,255&fc=0,0,0 &fs=10&fn=arial&phone=nzmwnziynte1%236awcxttnzykxasrj ");
url = new URL (fromurl);
URLConnection UC = Url.openconnection ();
InputStream is = Uc.getinputstream ();
According to the download file type, make the appropriate file name
File = new file ("D:\\forever.mp3");
FileOutputStream out = new FileOutputStream (file);
/*
* This annotation is also a way to write a file, but usually download mp3 or smaller pictures than mp3
* And so these files with this buffered method of writing files is slow, so that small file download usually use the following method to write the file//byte[] B = new
* Byte[102400*3]; int size = 0; while (size = Is.read (b))!=
*-1) {//Out.write (b, 1, size);////}
*/
int i = 0;
while ((i = Is.read ())!=-1) {
Out.write (i);
}
Out.flush ();
Is.close ();
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return file;
}
/**
* Delete the local disk specified path file create date:2009-5-13 Author:administrator
*
* @param file
*/
public static void Delfile (String file) {
File F = new file (file);
if (F.exists ())
F.delete ();
System.out.println (file + "has been deleted");
}
public static void Main (string[] args) {
Delfile ("D:\\forever.mp3");
DownLoadFile ("");
}
}
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.