How to store strings in Database blob fields

Source: Internet
Author: User

How to store strings in Database blob fields
 
How do I store strings in Database blob fields?

I am storing strings in blob fields with garbled characters. How can I solve this problem? Do you need to convert the string to binary and then store it?


How do I store strings in Database blob fields?
Can String. getBytes () be used?
We used to create a fileinputstream and set it in.


How do I store strings in Database blob fields?
Garbled characters are written with String. getByte!


How do I store strings in Database blob fields?
Finally, we can convert the text string to 8859-1 encoding!


How do I store strings in Database blob fields?
How to access graphics?


How do I store strings in Database blob fields?


This is a very simple class for accessing blob fields in mysql. Just change it as needed.

[Java]
/**
* Title: BlobPros. java
* Project: test
* Description: Save the image to the blob field in mysql and retrieve it.
* Call Module: tmp table in the mtools Database
* File: C: \ downloads \ luozsh.jpg
* Create Date: 2002.12.5
* @ Version 1.0
*
* Note: Set the Blob field in the database to longblob.
*
*/
 
// Package com. uniware;
 
Import java. io .*;
Import java. util .*;
Import java. SQL .*;
 
Public class BlobPros
{
Private static final String URL = "jdbc: mysql: // 10.144.123.63: 3306/mtools?
 
User = wind & password = 123 & useUnicode = true ";
Private Connection conn = null;
Private PreparedStatement pstmt = null;
Private ResultSet rs = null;
Private File file = null;

Public BlobPros ()
{
}
 
/**
* Insert a new BLOB Object (image) into the database)
*
* @ Param infile-data file to be input
* @ Throws java. lang. Exception
*
*/
Public void blobInsert (String infile) throws Exception
{
FileInputStream FCM = null;
Try
{
Class. forName ("org. gjt. mm. mysql. Driver"). newInstance ();
Conn = DriverManager. getConnection (URL );

File = new File (infile );
FS = new FileInputStream (file );
// InputStream FCM = new FileInputStream (infile );
Pstmt = conn. prepareStatement ("insert into tmp (descs, pic) values (?,?) ");
Pstmt. setString (1, file. getName (); // set the first parameter to the file name.
// Pstmt. setBinaryStream (2, FCM, (int) file. length (); // In principle, data is lost because
 
File. length () returns the long type.
Pstmt. setBinaryStream (2, fiis, fiis. available (); // The second parameter is the file content.
Pstmt.exe cuteUpdate ();
}
Catch (Exception ex)
{
System. out. println ("[blobInsert error:]" + ex. toString ());
}
Finally
{
// Close the opened object //
Pstmt. close ();
FCM. close ();
Conn. close ();
}
}

 
/**
* Reading BLOB objects from the database
*
* @ Param outfile-output data file
* @ Param picID-ID of the image to be retrieved in the database
* @ Throws java. lang. Exception
*
*/
 
Public void blobRead (String outfile, int picID) throws Exception
{
FileOutputStream fos = null;
InputStream is = null;
Byte [] Buffer = new byte [4096];
 
Try
{
Class. forName ("org. gjt. mm. mysql. Driver"). newInstance ();
Conn = DriverManager. getConnection (URL );
Pstmt = conn. prepareStatement ("select pic from tmp where id =? ");
Pstmt. setInt (1, picID); // input the ID of the image to be retrieved
Rs = pstmt.exe cuteQuery ();
Rs. next ();

File = new File (outfile );
If (! File. exists ())
{
File. createNewFile (); // if the file does not exist, create
}
Fos = new FileOutputStream (file );
Is = rs. getBinaryStream ("pic ");
Int size = 0;
/* While (size! =-1)
{
Size = is. read (Buffer); // read data from a segment of the database
// System. out. println (size );
If (size! =-1) //-1 indicates that the object has been read to the end.
Fos. write (Buffer, 0, size );
}*/
While (size = is. read (Buffer ))! =-1)
{
// System. out. println (size );
Fos. write (Buffer, 0, size );
}

}
Catch (Exception e)
{
System. out. println ("[OutPutFile error:]" + e. getMessage ());
}
Finally
{
// Close the resources used
Fos. close ();
Rs. close ();
Pstmt. close ();
Conn. close ();
}
}

Public static void main (String [] args)
{
Try
{

BlobPros blob = new BlobPros ();
// Blob. blobInsert ("C: \ Downloads \ luozsh1.jpg ");
Blob. blobRead ("c:/downloads/luozishang.jpg", 47 );
}
Catch (Exception e)
{
System. out. println ("[Main func error:]" + e. getMessage ());
}
}
}

How do I store strings in Database blob fields?
Input stream for accessing graphics

How do I store strings in Database blob fields?
How do I store strings in Database blob fields?
[Java]
// Function for accessing the binary file to the BLOB Field
Import oracle. SQL. BLOB;
Import oracle. jdbc. driver. OracleResultSet;
 
Public void updateBlob (Connection conn, String strFile, long newId, String table_name ){
Try {
Conn. setAutoCommit (false );
Statement stmt = conn. createStatement ();
// Stmt.exe cute ("insert into A (A, B) values ('" + strId + "', empty_blob ())");
ResultSet rset = stmt.exe cuteQuery ("select t. content from" + table_name + "t where id =" + newId +
"For update"); www.2cto.com
BLOB blob = null;
While (rset. next ()){
Blob = (OracleResultSet) rset). getBLOB (1 );
}
File binaryFile = new File (strFile );
FileInputStream instream = new FileInputStream (binaryFile); // read binary files
OutputStream outstream = blob. getBinaryOutputStream ();
Byte [] buffer = new byte [32];
 
Int length =-1;
While (length = instream. read (buffer ))! =-1)
Outstream. write (buffer, 0, length );
Instream. close ();
Outstream. close ();
 
Conn. commit ();
Conn. setAutoCommit (true );
Stmt. close ();
Conn. close ();
}
Catch (Exception e ){
E. printStackTrace ();
}
}


 

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.