新聞中心
隨著計(jì)算機(jī)技術(shù)的不斷發(fā)展,文件存儲(chǔ)空間的需求也在不斷增加。而傳統(tǒng)的文件存儲(chǔ)方式面臨著多種瓶頸,如存儲(chǔ)器限制、數(shù)據(jù)丟失、數(shù)據(jù)安全等問(wèn)題。為了解決這些問(wèn)題,將文件存儲(chǔ)到數(shù)據(jù)庫(kù)中已成為一種越來(lái)越普遍的方式。

專業(yè)領(lǐng)域包括成都做網(wǎng)站、網(wǎng)站建設(shè)、商城網(wǎng)站制作、微信營(yíng)銷、系統(tǒng)平臺(tái)開(kāi)發(fā), 與其他網(wǎng)站設(shè)計(jì)及系統(tǒng)開(kāi)發(fā)公司不同,創(chuàng)新互聯(lián)公司的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營(yíng)銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。
在Java應(yīng)用程序中實(shí)現(xiàn)將文件存儲(chǔ)到數(shù)據(jù)庫(kù)的功能,可以通過(guò)以下步驟完成:
1. 設(shè)計(jì)數(shù)據(jù)庫(kù)表
我們需要設(shè)計(jì)數(shù)據(jù)庫(kù)表來(lái)存儲(chǔ)文件。一般而言,文件表至少包含文件名、文件類型、文件大小、文件二進(jìn)制數(shù)據(jù)和創(chuàng)建時(shí)間等字段。此外,根據(jù)業(yè)務(wù)需求,我們還可以在表中添加其他字段。
CREATE TABLE t_file (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) DEFAULT NULL,
type varchar(255) DEFAULT NULL,
size bigint(20) DEFAULT NULL,
content longblob,
created_date timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
2. 設(shè)計(jì)Java對(duì)象
接下來(lái),我們需要定義一個(gè)Java對(duì)象來(lái)映射文件表的數(shù)據(jù)結(jié)構(gòu)。Java對(duì)象中的字段需要與數(shù)據(jù)庫(kù)表的字段對(duì)應(yīng)。
public class File {
private int id;
private String name;
private String type;
private long size;
private byte[] content;
private Date createdDate;
// getters and setters
}
3. 讀取文件
在將文件存儲(chǔ)到數(shù)據(jù)庫(kù)之前,我們需要將文件讀取到內(nèi)存中。Java中可以通過(guò)FileInputStream、ByteArrayOutputStream等類來(lái)實(shí)現(xiàn)文件讀取的功能。下面是一段將文件讀取為byte數(shù)組的代碼:
public static byte[] readFileToByteArray(File file) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
try (InputStream input = new FileInputStream(file)) {
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
}
return output.toByteArray();
}
4. 存儲(chǔ)文件到數(shù)據(jù)庫(kù)
有了文件的二進(jìn)制數(shù)據(jù)后,我們就可以將文件存儲(chǔ)到數(shù)據(jù)庫(kù)中了。在Java中,可以通過(guò)JDBC來(lái)連接數(shù)據(jù)庫(kù)和執(zhí)行SQL語(yǔ)句。下面是一段將文件存儲(chǔ)到數(shù)據(jù)庫(kù)中的代碼:
public static int saveFile(Connection connection, File file) throws SQLException, IOException {
final String sql = “INSERT INTO t_file(name, type, size, content) VALUES (?, ?, ?, ?)”;
try (PreparedStatement statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
statement.setString(1, file.getName());
statement.setString(2, file.getType());
statement.setLong(3, file.getSize());
statement.setBytes(4, file.getContent());
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new SQLException(“Creating file fled, no rows affected.”);
}
try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
if (generatedKeys.next()) {
file.setId(generatedKeys.getInt(1));
} else {
throw new SQLException(“Creating file fled, no ID obtned.”);
}
}
}
return file.getId();
}
5. 從數(shù)據(jù)庫(kù)中讀取文件
當(dāng)需要讀取數(shù)據(jù)庫(kù)中的文件時(shí),也可以借助JDBC來(lái)實(shí)現(xiàn)。以下是通過(guò)ID讀取文件的代碼示例:
public static File readFile(Connection connection, int id) throws SQLException, IOException {
final String sql = “SELECT id, name, type, size, content, created_date FROM t_file WHERE id = ?”;
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setInt(1, id);
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
File file = new File();
file.setId(resultSet.getInt(“id”));
file.setName(resultSet.getString(“name”));
file.setType(resultSet.getString(“type”));
file.setSize(resultSet.getLong(“size”));
file.setContent(resultSet.getBytes(“content”));
file.setCreatedDate(resultSet.getDate(“created_date”));
return file;
}
}
}
return null;
}
將文件存儲(chǔ)到數(shù)據(jù)庫(kù)中,可以使文件存儲(chǔ)更加方便、可靠和安全。在Java應(yīng)用中實(shí)現(xiàn)該功能,需要經(jīng)過(guò)設(shè)計(jì)數(shù)據(jù)庫(kù)表、定義Java對(duì)象、讀取文件、存儲(chǔ)文件和讀取文件等步驟。通過(guò)JDBC可以方便的對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作,從而實(shí)現(xiàn)將文件存儲(chǔ)到數(shù)據(jù)庫(kù)中的功能。
相關(guān)問(wèn)題拓展閱讀:
- 用java代碼把txt文檔中資料導(dǎo)入到數(shù)據(jù)庫(kù)
- 怎樣用java代碼把數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中
用java代碼把txt文檔中資料導(dǎo)入到數(shù)據(jù)庫(kù)
搜索的關(guān)鍵字是 java讀取txt ,你把文件讀取掘培保存在一個(gè)StringBuffer里悉族面,插入數(shù)據(jù)庫(kù)判陸唯即可
BufferedReader input;
try {
String s = new String();
input = new BufferedReader(new FileReader(“f:\\123.txt”));
while ((s = input.readLine()) != null) { // 判斷是否讀到了最后一行
String info = s.split(“培激 “敗喚);
System.out.println( info + ” ” + info + ” ” + info );
}
input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
把info + ” ” + info + ” ” + info 這配枯襪三個(gè)值放在insert語(yǔ)句里就行了 經(jīng)過(guò)測(cè)試
1、在數(shù)據(jù)庫(kù)中建燃搏敗立一個(gè)表,創(chuàng)建兩個(gè)字段,1個(gè)id,1個(gè)content(根據(jù)你估計(jì)銀橡的文本內(nèi)容大小,選定類型 varchar,text,blob等)
2、寫一個(gè)讀取txt文本的皮顫類A。
3、用java 建立好數(shù)據(jù)庫(kù)連接,通過(guò)類A把文本讀出來(lái),寫到數(shù)據(jù)庫(kù)中。
怎樣用java代碼把數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中
Java可以使用JDBC對(duì)數(shù)據(jù)庫(kù)進(jìn)行讀寫。JDBC訪問(wèn)一般分為如下流程:
1、加載JDBC驅(qū)動(dòng)程序:
在連接數(shù)據(jù)庫(kù)之前,首先要加載想要連接的數(shù)據(jù)庫(kù)的驅(qū)動(dòng)虛做到JVM(Java虛擬機(jī)),
這通過(guò)java.lang.Class類的靜態(tài)方法forName(String className)實(shí)現(xiàn)。
例如:
try{
//加載MySql的驅(qū)動(dòng)類
Class.forName(“com.mysql.jdbc.Driver”) ;
}catch(ClassNotFoundException e){
System.out.println(“找不到驅(qū)動(dòng)程序類 ,加載驅(qū)動(dòng)失??!”);
e.printStackTrace() ;
}
成功加載后,會(huì)將Driver類的實(shí)例注冊(cè)到DriverManager類中。
2、提供JDBC連接的URL
?連接URL定義了連接數(shù)據(jù)庫(kù)時(shí)的協(xié)議、子協(xié)議、數(shù)據(jù)源標(biāo)識(shí)。
?書寫形式:協(xié)議:子協(xié)議:數(shù)據(jù)源標(biāo)識(shí)
協(xié)議:在JDBC中總是以jdbc開(kāi)始
子協(xié)議:是橋連接的驅(qū)動(dòng)程序或是數(shù)據(jù)庫(kù)管理系統(tǒng)名稱。
數(shù)據(jù)源標(biāo)識(shí):標(biāo)記找到數(shù)據(jù)庫(kù)來(lái)源的地址與連接端口。
例如:(MySql的連接URL)
jdbc: ;
useUnicode=true:表示使用Unicode字符集。如果characterEncoding設(shè)置為
gb2312或GBK,本參數(shù)必須設(shè)置為true 。characterEncoding=gbk:字符編碼方式。
3、創(chuàng)建數(shù)據(jù)庫(kù)的連接
?要連接數(shù)據(jù)庫(kù),需要向java.sql.DriverManager請(qǐng)求并獲得Connection對(duì)象,該對(duì)象就代表一個(gè)數(shù)據(jù)庫(kù)的連接。
?使用DriverManager的getConnectin(String url,String username,String password )方法傳入指定的欲連接的數(shù)據(jù)庫(kù)的路徑、數(shù)據(jù)庫(kù)的用戶名和密碼來(lái)獲得。
例如:
//連接MySql數(shù)據(jù)庫(kù),用戶名和密碼都是root
String url = “jdbc: ;
String username = “root” ;
String password = “root” ;
try{
Connection con =
DriverManager.getConnection(url , username , password ) ;
}catch(SQLException se){
System.out.println(“數(shù)據(jù)庫(kù)連接失?。 ?;
se.printStackTrace() ;
}
4、創(chuàng)建一個(gè)Statement
?要執(zhí)行SQL語(yǔ)句,必須獲得java.sql.Statement實(shí)例,Statement實(shí)例分為以下3種類型:
1、執(zhí)行靜態(tài)SQL語(yǔ)句。通常通過(guò)Statement實(shí)例實(shí)現(xiàn)。
2、執(zhí)行動(dòng)差純衡態(tài)SQL語(yǔ)句。通常通過(guò)PreparedStatement實(shí)例實(shí)現(xiàn)。
3、執(zhí)行數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程。通常通過(guò)CallableStatement實(shí)例實(shí)現(xiàn)。
具體的實(shí)現(xiàn)方式:
Statement stmt = con.createStatement() ;
PreparedStatement pstmt = con.prepareStatement(sql) ;
CallableStatement cstmt = con.prepareCall(“{CALL demoSp(? , ?)}”) ;
5、執(zhí)行SQL語(yǔ)句
Statement接口提供了三種執(zhí)行褲歷SQL語(yǔ)句的方法:executeQuery 、executeUpdate和execute
1、ResultSet executeQuery(String sqlString):執(zhí)行查詢數(shù)據(jù)庫(kù)的SQL語(yǔ)句,返回一個(gè)結(jié)果集(ResultSet)對(duì)象。
2、int executeUpdate(String sqlString):用于執(zhí)行INSERT、UPDATE或DELETE語(yǔ)句以及SQL DDL語(yǔ)句,如:CREATE TABLE和DROP TABLE等
3、execute(sqlString):用于執(zhí)行返回多個(gè)結(jié)果集、多個(gè)更新計(jì)數(shù)或二者組合的語(yǔ)句。
具體實(shí)現(xiàn)的代碼:
ResultSet rs = stmt.executeQuery(“SELECT * FROM …”) ;
int rows = stmt.executeUpdate(“INSERT INTO …”) ;
boolean flag = stmt.execute(String sql) ;
6、處理結(jié)果
兩種情況:
1、執(zhí)行更新返回的是本次操作影響到的記錄數(shù)。
2、執(zhí)行查詢返回的結(jié)果是一個(gè)ResultSet對(duì)象。
ResultSet包含符合SQL語(yǔ)句中條件的所有行,并且它通過(guò)一套get方法提供了對(duì)這些行中數(shù)據(jù)的訪問(wèn)。
使用結(jié)果集(ResultSet)對(duì)象的訪問(wèn)方法獲取數(shù)據(jù):
while(rs.next()){
String name = rs.getString(“name”) ;
String pass = rs.getString(1); // 此方法比較高效(列是從左到右編號(hào)的,并且從列1開(kāi)始)
}
7、關(guān)閉JDBC對(duì)象
操作完成以后要把所有使用的JDBC對(duì)象全都關(guān)閉,以釋放JDBC資源,關(guān)閉順序和聲明順序相反:
1、關(guān)閉記錄集
2、關(guān)閉聲明
3、關(guān)閉連接對(duì)象
if(rs != null){ // 關(guān)閉記錄集
try{
rs.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(stmt != null){ // 關(guān)閉聲明
try{
stmt.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(conn != null){ // 關(guān)閉連接對(duì)象
try{
conn.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
java的文件保存到數(shù)據(jù)庫(kù)中的介紹就聊到這里吧,感謝你花時(shí)間閱讀本站內(nèi)容,更多關(guān)于java的文件保存到數(shù)據(jù)庫(kù)中,Java實(shí)現(xiàn)將文件存儲(chǔ)到數(shù)據(jù)庫(kù)中的方法,用java代碼把txt文檔中資料導(dǎo)入到數(shù)據(jù)庫(kù),怎樣用java代碼把數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中的信息別忘了在本站進(jìn)行查找喔。
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開(kāi)通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過(guò)10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開(kāi)發(fā)經(jīng)驗(yàn)。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊(cè)、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
分享標(biāo)題:Java實(shí)現(xiàn)將文件存儲(chǔ)到數(shù)據(jù)庫(kù)中的方法(java的文件保存到數(shù)據(jù)庫(kù)中)
文章網(wǎng)址:http://m.fisionsoft.com.cn/article/cdhshcd.html


咨詢
建站咨詢
