程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java對數據庫的大文件的操作(SQLServer2000)

java對數據庫的大文件的操作(SQLServer2000)

編輯:關於JAVA

編寫的把文件的內容寫入到數據庫的代碼如下

首先 建立 數據庫表

id char
data image(注意此處不要用text類型 text類型與binary不兼容)
代碼如下:
import java.io.*;
import java.sql.*;
public class Db {
public static void main(String[] args) {
Db ac = new Db();
String blobname = "D:\\test1.txt"; //blob文件名
String in = "insert into ";
String in1 = "(id,data) values(´0004´,?)";
String tablename = "Ss";
String sqlstr = ""; // sql 語句
sqlstr = in + tablename + in1;
ThreadUseExtends thread = new ThreadUseExtends(blobname,sqlstr);
thread.insert();
}
}
class ThreadUseExtends {
String filename1;//blob filename
String str;
ReadFiles r1 = new ReadFiles();
//構造函數要有(blob文件名,clob文件名,sql語句)
public ThreadUseExtends(String name1,String sqlstr)
{
filename1 = name1;
str = sqlstr;
System.out.println("I carry out this");
}
public void insert()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:LW";
//String login = "system"; // use your login here
//String password = "ti2003"; // use your password here
Connection con = DriverManager.getConnection(url);
String testLong = r1.ReadFile(filename1);
//String testLong1 = r1.ReadFile(filename2);
byte[] ba = testLong.getBytes();
System.out.println("str=" + str);
//String strSql = str; //"insert into
// orders(order_id,ric_code,siz,price,trade_datetime,status,testblob,testclob)
// values(8,′asdfjkl′,21,123.34567,sysdate,′nill逆耳′,?,?)";
PreparedStatement stm = con.prepareStatement(str);
stm.setBytes(1, ba);
//StringReader test = new StringReader(testLong1);
//stm.setCharacterStream(2, test, testLong.length());
stm.execute();
stm.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}//ThreadUseExtends class
// ReadFiles class for read text!!
class ReadFiles {
public ReadFiles() {
}
//ReadFile method,read file
public String ReadFile(String FileName) {
String Name = FileName;
String File = "";
try {
FileReader ReadF = new FileReader(Name);//讀文件
BufferedReader HuanChong = new BufferedReader(ReadF);//文件讀緩沖.
try {
File = HuanChong.readLine();
} catch (IOException e1) {
// TODO 自動生成 catch 塊
e1.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
//System.out.println("文件:"+File);
return File;
}
}//ReadFiles class

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved