Use JDBC storage and call large objects
Peng Li
Summary: This section describes how to store and call in a database in a database with Java language, declare the image, sound and other large object files as BLOB type data in the data sheet. Connecting to the database via the JDBC driver, using the method in the class preparedStatement and ResultSet included in the JDBC API, and invokes BLOB type data.
Keywords: big object (blob), database, JDBC, Java API
With the development of Internet network applications, the application of multimedia data is increasingly wide. The management of multimedia materials has gradually become an urgent need to solve problems, and establish a multimedia database system that makes multimedia materials convenient storage and lookup. Traditional file system management is only suitable for a small amount, a single multimedia data browsing and query. As the current mainstream technology, the database management system based on the relationship model is stored by introducing new data types such as binary larming objects such as the Binary Large Object, Sound, etc..
Below to implement an image file in a GIF format in the mysql database, the programming language uses Java, the original full program code is compiled in the JBuilder5 environment.
1 First create a database MyImage and table bin_data that store binary data in the MySQL database.
After starting mySQL, enter the following command line:
Mysql> Create Database MyImage;
mysql> Use myimage;
MySQL> Create Table Bin_Data (ID Integer NTE NULL AUTO_INCREMENT Primary Key, Name VARCHAR (30), DATA Longblob);
This is built in the database and table.
2 Set up a database connection, load the JDBC driver:
The Java program and database connection is through JDBC (Java Database Connectivity), the basic structure of JDBC is a JDBC API to match the JDBC driver. In the beginning of the program, the Java.sql Suite IMPORT to the JDBC API will be included in the program, so that the powerful Java database program can be written through the rich classes and interfaces provided by JDBC. The JDBC driver can be subdivided into four types. Different types of JDBC drivers have different features and methods of use, and the same type of different versions of JDBC has a lot of support for data types of different types and different versions of databases. Differences are not described herein.
The driver in this program uses JDBC-ODBC Bridage.
Class.Forname ("Sun.jdbc.odbc.jdbcodbcdriver");
// Load driver
Connection conn = drivermanager.getConnection
"JDBC: ODBC: BIN_DATA", // Database Connection Path
"root", // username
"123"); // User password
3 The image file named "Dancegirl.gif" is stored, using the setBinaryStream () method of the PREPAREDSTATEMENT class using the java.sql package:
File Files = New File ("Dancegirl.gif");
FileInputStream Fis = New FileInputStream (Files);
PreparedState ps = conn.preparestatement ("INSERT INTO BINARY_DATA (Name, DATA)
"VALUES (?,?)"); // Pre-compiling SQL statement
Ps.setstring (1, files.getname ());
Ps.setBinaryStream (2, Fis, (int) files.length ());
ps.executeUpdate ();
fis.close ();
ps.close ();
4 Toned files that have been stored in the database "Dancegirl.gif", read the number of bytes and display it as an ImageCon class object:
ImageICON II;
Jlabel Jlabel1 = new jlabel ();
Jlabel Jlabel1 = new jlabel ();
INT BYTESUM = 0;
INT BYTESREAD = 0;
Byte [] buffer = new byte [8 * 1924];
FileOutputStream Fis = New FileoutputStream
(String) (JCOMBOBOX1.GetSelectedItem ()));
PreparedState ps = conn.preparestatement
"SELECT DATA from BINARY_DATA where name =?");
ps.setstring (1,
(String) (JCOMBOBOX1.GetSelectedItem ()));
ResultSet RS = ps.executeQuery ();
IF (rs! = null)
{
While (rs.next ())
{
InputStream IS = rs.getbinaryStream (1);
While ((bytesRead = is.read (buffer))! = -1)
{
Bytesum = bytesread;
Fis.write (buffer, 0, bytesread);
}
II = new imageicon (buffer);
Jlabel1.Seticon (II);
Is.close ();
}
Rs.close ();
}
ps.close ();
fis.close ();
Jlabel3.Settext ("Bytes Written:" bytesum);
references:
1. [Beauty] Denieli.joshi Rodney Rundfson Ramesh Chandak, Wang Cuiying, etc. "Java's latest technology - JDBC connecting SQL"
Beijing: Electronic Industry Press, 1999.
2, [US] Bruce Eckel, Jingjing Studio Translation "Java Programming Thoughts"
Beijing: Machinery Industry Press, 1999.