您好,登錄后才能下訂單哦!
將開發過程中經常用的一些內容片段做個珍藏,下面資料是關于java大對象存取的簡單實現的內容。
package info.itlanguageexpress.LargeObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MySqlLargeObjectAccess {
public static String DRIVER = "org.gjt.mm.mysql.Driver";
public static String USER = "root";
public static String PASSWORD = "your password";
public static void main(String[] args) throws Exception {
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USER, PASSWORD);
insertWordFileToMySql(conn);
fetchWordFileFromMySql(conn);
} catch (Exception ex) {
ex.printStackTrace(System.out);
} finally {
try {
conn.close();
} catch (Exception ex) {
}
}
}
public static void insertWordFileToMySql(Connection conn)
throws SQLException, IOException {
PreparedStatement pstm = conn
.prepareStatement("insert into wordInputTest values (?,?)");
pstm.setString(1, "docx1");
InputStream in = new FileInputStream("F:" + File.separator
+ "test1.docx");
pstm.setBinaryStream(2, in, in.available());
pstm.executeUpdate();
pstm.close();
}
public static void fetchWordFileFromMySql(Connection conn)
throws SQLException, IOException {
PreparedStatement pstm = conn
pstm.setString(1, "docx1");
ResultSet rs = pstm.executeQuery();
int count = 1;
while(rs.next()) {
InputStream in = rs.getBinaryStream("file");
FileOutputStream out = new FileOutputStream("F:" + File.separator + "testcopy" + count + ".docx");
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
out.flush();
}
out.close();
in.close();
count++;
}
rs.close();
pstm.close();
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。