Java SQL Server Text Type field Read Method _java

Source: Internet
Author: User
Tags base64
There is a need to dump documents originally stored in the database into the file system, so a simple program is written to complete this function, as follows:
Java code
Copy Code code as follows:

Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import org.dbunit.util.Base64;
public class Readblob {
/**
* @param args
*/
public static void Main (string[] args) throws Exception {
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = Drivermanager.getconnection (
"Jdbc:sqlserver://localhost:1433;databasename=test1", "sa",
"123456");
PreparedStatement PS = conn.preparestatement ("SELECT * from AA");
ResultSet rs = Ps.executequery ();
while (Rs.next ()) {
String fileName = rs.getstring ("filename");
String content = rs.getstring ("content");
byte[] byte_content = base64.decode (content);
Generatefile (byte_content, "D:\\doc", fileName);
}
Conn.close ();
}
/**
* Generate file according to byte array
*/
public static void Generatefile (byte[] bfile, String filepath,string fileName) {
Bufferedoutputstream BOS = NULL;
FileOutputStream fos = null;
File file = null;
try {
File dir = new file (FilePath);
if (!dir.exists () &&dir.isdirectory ()) {
Dir.mkdirs ();
}
File = new file (filepath+ "\" +filename);
FOS = new FileOutputStream (file);
BOS = new Bufferedoutputstream (FOS);
Bos.write (bfile);
catch (Exception e) {
E.printstacktrace ();
finally {
if (Bos!= null) {
try {
Bos.close ();
catch (IOException E1) {
E1.printstacktrace ();
}
}
if (FOS!= null) {
try {
Fos.close ();
catch (IOException E1) {
E1.printstacktrace ();
}
}
}
}
}
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.