當前位置:首頁 » 操作系統 » java文件保存資料庫中

java文件保存資料庫中

發布時間: 2023-05-17 04:57:06

java中存文件到ORACLE資料庫里怎麼做

參考代碼如下:
public class InsertBlobData {
Connection con = null;

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InsertBlobData data = new InsertBlobData();
data.insertBlogInfo("002jpg", "sdsdfdf", "2007-02-12", "002.jpg");
}
public void insertBlogInfo(String jmzh, String xm, String smsj,
String fileName) throws Exception {
// try {
con = ConnectionPoliceFactory.getFactory().getConnection();
// } catch (ClassNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// 處理事務
boolean defaultCommit = con.getAutoCommit();
con.setAutoCommit(false);
Statement st = con.createStatement();
// 插入一個空對象
st.executeUpdate("insert into ksren_txxx(jmzh,xm,smsj,txsj) values('"
+ jmzh + "','" + xm + "',to_date('" + smsj
+ "','yyyy-mm-dd'),empty_blob())");
// 用for update方式鎖定數據行
ResultSet rs = st
.executeQuery("select txsj from ksren_txxx where jmzh='"
+ jmzh + "' and xm='" + xm + "' for update");
if (rs.next()) {
// 得到java.sql.Blob對象,然後Cast為oracle.sql.BLOB
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
// 到資料庫的輸出流
OutputStream outStream = blob.getBinaryOutputStream();
// 這里用一個文件模擬輸入流
InputStream fin = new FileInputStream(new File(fileName));
// 將輸入流寫到輸出流
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ((len = fin.read(b)) != -1) {
outStream.write(b, 0, len);
// blob.putBytes(1,b);
}
// 依次關閉(注意順序)

fin.close();
outStream.flush();
outStream.close();
con.commit();
/* 恢復原提交狀態 */
con.setAutoCommit(defaultCommit);
con.close();

}
}
}

❷ 怎樣用Java實現從文本文檔中讀取數據並存入資料庫

不知道你要什麼樣的文本,文本中的內容是否是有格式的:

這里提供下思路,供參考:
1.文本文件,基本上式字元格式的了,可以用Readerio流
2.如果是格式化的文本,可以按數據的長度讀取,readIntreadByte...
3.保存到資料庫當然用JDBC了,如果你讀取出來封裝成POJO了,也可以選擇OM框架



importjava.io.BufferedReader;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;

/**
*文件讀取和寫入資料庫
*@author樊雲升
*
*/
publicclassFilesReader{

publicFilesReader(){

}

/**
*讀取文件內容
*@paramFILE
*@return
*/
publicStringre_content(StringFILE){
Stringcontent="";
try{
BufferedReaderbufRead=newBufferedReader(newInputStreamReader(newFileInputStream(FILE)));
Stringstr;
while((str=bufRead.readLine())!=null){
content+=str+" ";
}
}catch(IOExceptionioe){
ioe.printStackTrace();
}
returncontent;
}

/**
*將特定字元寫入資料庫中(原來我寫的是重寫文件,你這里這里將content寫入資料庫就OK)
*@parampath
*@return
*/
publicbooleanwriteFile(Stringcontent){
try{
//資料庫寫入代碼
}catch(Exceptione){
out.close();
returnfalse;
}
returntrue;
}

publicstaticvoidmain(String[]args){
Stringcontent=newFilesReader().re_content("D:\AJAX.htm");
newFilesReader().writeFile(content);
}

}

❸ java如何將圖片保存在資料庫中(java保存圖片到本地)

一般都是這樣的,就是在你伺服器有一個專門放置圖片的文件夾,然後資料庫保存的是你伺服器圖片的路徑。需要用的時候就去資料庫裡面取路徑。得到路徑以後你蔽哪想怎麼處理圖片是你的事情了。

至於如何去資料庫取路徑這個就是簡單的db操作。

載入驅動類:

Class.forName(DBDriver);

獲取連接:

Connectionconn=.(url,username,password);

創建操作對象:

stmt=con.(sql);

執行操作:

ResultSetrs=stmt.();

遍歷賣畢結果:

Listlist=newArrayList();

while(rs.next()){

//具體操作,通常用rs.getString(name)取值

Imageimg=newImage();//圖片類對應你資料庫中圖片表格

img.setSrc(rs.getString("src"));//假設你資料庫中image表中圖片地址欄位是src

list.add(img);

}

記得關閉資源:

rs.close();

stmt.close();

con.close();

看你的意思是已經中並芹取出來了不知道怎麼顯示:

你取出來之後可以把圖片放在一個list裡面然後去頁面上遍歷這個list

大致應該是這樣

❹ java如何里將文件存到資料庫中

java要實現將文件存到資料庫中的話,你可以在資料庫中使用blob類型,然後使用IO操作保存為位元組類型,這樣就可以進行傳輸和下載

❺ java中 怎樣吧 文件路徑存儲到mysql資料庫中。圖片如下(回答的好,追加分)謝謝了。

存到塌畢虛資料庫時將所有\團燃替換成其他字串,如"|",然後在使用的時候在替換回來就可以了。。加上驗數大證,這樣可以避免一些BUG

訪問Java編程中的數據如何保存到資料庫中的數據

保存到數毀握據庫的話相對簡單,只要與資料庫建立連接,創建自己的數據棗余伏庫,實現數據添加方法凳攜,需要插入時調用該方法就行了。

❼ java往資料庫存儲大文件

你好。請問什麼資料庫。oracle如下

資料庫中提供了兩種欄位類型 Blob 和 Clob 用於存儲大型字元串或二進制數據(如圖片)。察殲

Blob 採用單位元組存儲,適合保存二進制數據,如圖片文件。
Clob 採用多位元組存儲,適合保存大型文本數據。

首先創建一個空 Blob/Clob 欄位,再從這個圓敏空 Blob/Clob欄位獲取游標,例如下面的代碼:

PreparedStatement ps = conn.prepareStatement( " insert into PICTURE(image,resume) values(?,?) " );
// 通過oralce.sql.BLOB/CLOB.empty_lob()構造空Blob/Clob對象
ps.setBlob( 1 ,oracle.sql.BLOB.empty_lob());
ps.setClob( 2 ,oracle.sql.CLOB.empty_lob());

ps.excuteUpdate();
ps.close();

// 再次對讀出Blob/Clob句柄
ps = conn.prepareStatement( " select image,resume from PICTURE where id=? for update " );
ps.setInt( 1 , 100 );

ResultSet rs = ps.executeQuery();
rs.next();

oracle.sql.BLOB imgBlob = (oracle.sql.BLOB)rs.getBlob( 1 );
oracle.sql.CLOB resClob = (oracle.sql.CLOB)rs.getClob( 2 );

// 將二橘沒枝進制數據寫入Blob
FileInputStream inStream = new FileInputStream( " c://image.jpg " );
OutputStream outStream = imgBlob.getBinaryOutputStream();

byte [] buf = new byte [ 10240 ];
int len;
while (len = inStream.read(buf) > 0 ) {
outStream.write(buf, 0 ,len);
}
inStream.close();
outStream.cloese();

// 將字元串寫入Clob
resClob.putString( 1 , " this is a clob " );

// 再將Blob/Clob欄位更新到資料庫
ps = conn.prepareStatement( " update PICTURE set image=? and resume=? where id=? " );
ps.setBlob( 1 ,imgBlob);
ps.setClob( 2 ,resClob);
ps.setInt( 3 , 100 );

ps.executeUpdate();
ps.close();

❽ JAVA語言寫文件存取,存到ORACLE資料庫里怎麼寫

package com.jspdev.ch13;
import com.jspdev.util.*;
import java.sql.*;
import javax.sql.*;
import java.io.*;
import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.BLOB;
public class BlobBean
{

Connection conn ;
/**
*構造方法,創建Connection對象,並且在資料庫中添加一個表。
*/
public BlobBean()throws Exception
{

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thin:[email protected]:1521:hellking", "system", "manager");
// conn.createStatement().execute("create table blobtable(blobvalue blob)");
}

/**
*寫入Blob數據到資料庫
*/
public void addBlob(String fileName)throws Exception
{

conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
stmt.execute("insert into blobtable values (empty_blob())");
ResultSet rset = stmt.executeQuery("SELECT blobvalue FROM blobtable FOR UPDATE");
BLOB blob = null;
while (rset.next()) {
blob = ((OracleResultSet) rset).getBLOB(1);
System.out.println(blob.length());
}
File binaryFile = new File(fileName);
System.out.println(fileName+"'s length = " + binaryFile.length());
FileInputStream instream = new FileInputStream(binaryFile);
OutputStream outstream = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
System.out.println("chunk size = " + chunk);
byte[] buffer = new byte[chunk];
int length = -1;
while ((length = instream.read(buffer)) != -1)
outstream.write(buffer, 0, length);
instream.close();
outstream.close();
conn.commit();

}

❾ 用java怎樣把數據存到資料庫中

只能寫個大概的,要寫數據到資料庫中,先得在資料庫中建庫,庫里建凳槐表,表裡建欄位,然棗基友後java里建立資料庫連接,用SQL語言寫數據到表中的欄位
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=資料庫名"; //7.0、2000
String url="jdbc:sqlserver://localhost:1433;DatabaseName=資料庫名"; //2005
Connection conn=null;
conn= DriverManager.getConnection(url,用戶名,密碼);
PreparedStatement pst=null;
pst=conn.prepareStatement("Insert Into grade(表名) Values (?)");
pst.setInt(1,你要寫的鋒蔽整弄數據);
//pst.setString(2,你要寫的字元串數據);
pst.addBatch();
pst.executeBatch();

❿ java上傳文件保存至達夢資料庫

1、創建需要的表格,包括文件名、文件類型、文件內容和上傳時間等欄位。
2、在Java項目中引入相關的資料庫驅動程序,以實現與達夢資料庫的連接早旅。
3、創建一個包含文件上傳表單猜碧的HTML頁面,並且在陸兆凳後端使用Java代碼接收上傳的文件。
4、將上傳的文件轉換為二進制流,並將其插入到資料庫中。

熱點內容
編譯好的內核如何升級另一台主機 發布:2025-05-15 02:00:06 瀏覽:757
彈反腳本 發布:2025-05-15 01:58:24 瀏覽:585
安卓按鍵大師怎麼用 發布:2025-05-15 01:54:12 瀏覽:686
手機ea伺服器連不上怎麼辦 發布:2025-05-15 01:35:03 瀏覽:450
資料庫數據插入語句 發布:2025-05-15 01:30:01 瀏覽:871
js是無需編譯直接運行嗎 發布:2025-05-15 01:28:30 瀏覽:476
android文件夾重命名 發布:2025-05-15 01:13:50 瀏覽:481
cns腳本 發布:2025-05-15 01:13:38 瀏覽:722
數據結構與演算法筆試題 發布:2025-05-15 01:04:20 瀏覽:417
搜狗輸入法如何直接編輯配置文件 發布:2025-05-15 00:51:47 瀏覽:668