經過參考
《java多線程數據庫操作》-JAVA中文站(www_Java-cn_com)
Java語言SQL接口-JDBC編程技術
文章和書都可以從Java_cn下載
編寫的把文件的內容寫入到數據庫的代碼如下
首先 建立 數據庫表
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();
}
}