BLOB and CLOB
If you want to record the database, you can use the BLOB or Clob type field on the field, BLOB (Binary Large Object) is used to store a large amount of binary data, and Clob (Character Large Object) is used to store a large number of text data.
BLOB and Clob are also provided in JDBC, representing BLOB and Clob type data, and JDBC do not provide an API (such as setblob (), setClob (), etc.), but you can use PrepareStatement's setBinaryStream (), setObject (), setASciistReam (), setUnicodestream (), etc., for example, you can use the method to get a file and store it in the database:
File file = new file (".// Logo.JPG");
INT length = (int) file.length ();
InputStream Fin = New FileInputStream (file);
PreparedStatement PSTMT = Conn.PreparedStatement ("INSERT INTO Files VALUES (?,?)");
PSTMT.SetString (1, "logo");
PSTMT.SetBinaryStream (2, FIN, Length); // Written the file stream
PSTMT.ExecuteUpdate ();
Pstmt.clearparameters ();
PSTMT.Close ();
Fin.close ();
If you want to get BLOB and CLOB type data from the database, you can use the following methods:
BLOB BLOB = Result.getblob (2);
Clob Clob = Result.getClob (2);
BLOB has a method of getBinaryStream (), getBytes (), etc., can get binary streams or byte, etc., similarly, Clob can obtain data stream or sub-string, etc. by getCharacterStream (), getSubstring () and other methods. View API files in detail)
Below, an illustration of the operational blob, Clob data
Import java.io. *;
Import java.sql. *;
Public class blobclobdemo {
Public static void main (String [] args) {
String driver = "com.mysql.jdbc.driver";
String Url = "JDBC: MYSQL: / / LOCALHOST: 3306 / UPLOAD?"
String username = "myname";
String Password = "123456";
Try {
Connection conn = conn.getConnection (URL, UserName, Password);
File file = new file (".// Logo.JPG");
INT length = (int) file.length ();
InputStream Fin = New FileInputStream (file);
PreparedStatement PSTMT = New PreparedStrate ("INSERT INTO FILES VALUSE (?,?)");
PSTMT.SetString (1, "logo");
PSTMT.SetBinaryStream (2, FIN, Length);
PSTMT.ExecuteUpdate ();
Pstmt.clearparameters ();
PSTMT.Close ();
Fin.close ();
// Remove the information and archive statement stmt = conn.createstatement ();
ResultSet Result = Stmt.executeQuery ("SELECT * from Files");
Result.next ();
String description = result.getstring (1);
BLOB BLOB = Result.getblob (2);
System.out.println ("Document Description:" Description;
FileOutputStream Fout = New FileOutputStream (".// logo2.jpg");
Fout.write (blob.getbytes (1, (int) blob.length ()));
Fout.flush ();
Fout.close ();
Stmt.close ();
CONN.CLOSE ();
}
Catch (classnotfoundexception e) {
System.out.println ("Can't find driver");
E.PrintStackTrace ();
}
Catch (SQLException E) {
E.PrintStackTrace ();
}
Catch (IOException E) {
E.PrintStackTrace ();
}
}
}