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();
B. java如何把文件存入Microsoft sql server資料庫
要將xml文件的數據寫進資料庫,原來其實很簡單,就是把xml文件里的數據取出然後連接到資料庫使用insert就ok了,當然這前提是你要了解怎麼解析xml文件了,目前解析xml文件主要有sax,dom,pull等方法。具體用法就不一一例舉啦,本例使用的是dom。下面就看代碼吧
C. java文件路徑怎麼存入資料庫
直接讀寫文件,再把讀出來的文件內容格式化成json,再用jdbc、mybatis或者其他框架將json數據存入資料庫。
D. 在java中如何將數組里的數據存入資料庫呢
保存位元組數組到資料庫分兩步:
第一、利用FileInputStream.read(byte[])方法把內容讀取到byte[]數組中,比如圖片是由二進制數組成的,就可以定義為一個位元組數組。
第二、在資料庫中對應記錄欄位應該設置為blob類型,這樣就能夠順利保存了
事例代碼如下:
PreparedStatement stmt = connection.generatePreparedStatement("INSERT INTO ... ");
stmt.setBytes(1, yourByteArray);
其中,yourByteArray是你讀出來的字元數組。
E. 用java將word文件上傳到伺服器,把word裡面的內容保存到資料庫
使用java中的io進行讀取
BufferedReader bufferedReader = null;
File file = new File("文檔地址+文檔名.docx");
if(!file.exists()){
System.out.println("文件不存在");
} else {
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "讀取的字元格式(UTF-8或GBK)"));
String lineText = null;
while((lineText = bufferedReader.readLine()) != null){
if (linText != null && !lineText.eq("")){
System.out.println("一次讀取一行,一行內容為:" + lineText);
}
}
}
F. 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();
}
}
}
G. 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();
}
H. java怎麼解析指定的文件,並保存到資料庫
如果txt,xls,按行讀,用特定的字元作分隔符來拆分,讀一行處理一行,直到結束,導入都是這樣的.
給個txt的案例給你看看:
File logFile = new java.io.File("d://PartInputLog.txt"); //
ins = form.getFile().getInputStream(); //讀取數據流
workBook = Workbook.getWorkbook(ins); //打開工作簿
sheet = workBook.getSheet(0); //打開SHEET
int rowSize = sheet.getRows(); //獲取總行數
for(int i=0;i<rowSize;i++){
if(sheet.getCell(0, i).getContents().trim().equals(""))break;
String gysNo = sheet.getCell(0, i).getContents().trim();//獲取第i行第1列的具體數據
String bpNo = sheet.getCell(1, i).getContents().trim(); //獲取第i行第2列的具體數據
String numStr = sheet.getCell(2, i).getContents().trim(); //獲取第i行第3列的具體數據
.................................................
}
//數據都可以讀取到剩下的就在循環中插入了
I. java如何里將文件存到資料庫中
java要實現將文件存到資料庫中的話,你可以在資料庫中使用blob類型,然後使用IO操作保存為位元組類型,這樣就可以進行傳輸和下載
J. 怎樣用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);
}
}