我工作了半天,探求了一个方法。共享之。//JDBC-数据库中文件的读写.txt//DBReadWriteFile.java
/*
1、目的:
将文件写入数据库;
从数据库读文件内容到指定的文件中。
2、样例数据库
数据库类型:Microsoft Access 2000
数据库名称:db1.mdb
表名称: testword
表字段:
字段名称 数据类型
name 文本
content OLE对象
3、作者:
[email protected]
*/import java.io.*;
import java.sql.*;public class DBReadWriteFile{
String dbUrl;
String sql;
Connection con;
Statement state;
ResultSet rs;
public DBReadWriteFile(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
/*这里的数据库的url一定要写正确,这是关键,其中DBQ可以绝对路径,也可以是相对路径,为了体现数据存储路径的/独立性,你可以将数据库copy到不同的位试一下*/
dbUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=d:\\db1.mdb";
con = DriverManager.getConnection(dbUrl,"","");
state = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
//执行SQL语句
sql = "select * from testword";
rs = state.executeQuery(sql);
}catch(Exception e){e.printStackTrace();}
}
//将一个文件的内容写入数据库,文件可以为任何类型的文件,如.doc、.txt、.jpg等等
public void WriteFile(String strFileName){
try{
System.out.println("Writing to database... from file:"+strFileName);
File myFile=new File(strFileName);
InputStream inword=new FileInputStream(myFile);
rs.moveToInsertRow(); // moves cursor to the insert row
rs.updateString(1,strFileName); //updates the col:1
rs.updateBinaryStream(2,inword,(int)myFile.length());//updates the col:2,input the file's content
rs.insertRow();
rs.close(); //closes the ResultSet
con.close();//closes the Connection
System.out.println("Write to database successfully");
}catch(Exception e){e.printStackTrace();}
}
//将数据库中的文件数据读到指定的文件中
public void ReadFile(String strFileName){
try{
System.out.println("Reading from database... to file:"+strFileName);
rs.last();//for demo,just move the cursor to last
OutputStream outword = new FileOutputStream(strFileName);
InputStream inword = rs.getBinaryStream(2);//reads the file's content
int temp;
while((temp=inword.read())!=-1){
outword.write(temp);
}
outword.close();
System.out.println("Read from database successfully");
}catch(Exception e){e.printStackTrace();}
}
public static void main(String[] args){
try{
if(args.length<1){
System.out.println("Usage:jjava DBReadWriteFile aaa.jpg");
System.exit(0);
}
DBReadWriteFile my=new DBReadWriteFile();
my.WriteFile(args[0]);//测试,写一个文件到数据库中
my=new DBReadWriteFile();
my.ReadFile("new"+args[0]);//测试,从数据库读出文件到指定的文件中
//如果想支持非当前路径文件的话,需要修正"new"+args[0]
}catch(Exception e){e.printStackTrace();}
}
}//End class MobileDatabase
/*
1、目的:
将文件写入数据库;
从数据库读文件内容到指定的文件中。
2、样例数据库
数据库类型:Microsoft Access 2000
数据库名称:db1.mdb
表名称: testword
表字段:
字段名称 数据类型
name 文本
content OLE对象
3、作者:
[email protected]
*/import java.io.*;
import java.sql.*;public class DBReadWriteFile{
String dbUrl;
String sql;
Connection con;
Statement state;
ResultSet rs;
public DBReadWriteFile(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
/*这里的数据库的url一定要写正确,这是关键,其中DBQ可以绝对路径,也可以是相对路径,为了体现数据存储路径的/独立性,你可以将数据库copy到不同的位试一下*/
dbUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=d:\\db1.mdb";
con = DriverManager.getConnection(dbUrl,"","");
state = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
//执行SQL语句
sql = "select * from testword";
rs = state.executeQuery(sql);
}catch(Exception e){e.printStackTrace();}
}
//将一个文件的内容写入数据库,文件可以为任何类型的文件,如.doc、.txt、.jpg等等
public void WriteFile(String strFileName){
try{
System.out.println("Writing to database... from file:"+strFileName);
File myFile=new File(strFileName);
InputStream inword=new FileInputStream(myFile);
rs.moveToInsertRow(); // moves cursor to the insert row
rs.updateString(1,strFileName); //updates the col:1
rs.updateBinaryStream(2,inword,(int)myFile.length());//updates the col:2,input the file's content
rs.insertRow();
rs.close(); //closes the ResultSet
con.close();//closes the Connection
System.out.println("Write to database successfully");
}catch(Exception e){e.printStackTrace();}
}
//将数据库中的文件数据读到指定的文件中
public void ReadFile(String strFileName){
try{
System.out.println("Reading from database... to file:"+strFileName);
rs.last();//for demo,just move the cursor to last
OutputStream outword = new FileOutputStream(strFileName);
InputStream inword = rs.getBinaryStream(2);//reads the file's content
int temp;
while((temp=inword.read())!=-1){
outword.write(temp);
}
outword.close();
System.out.println("Read from database successfully");
}catch(Exception e){e.printStackTrace();}
}
public static void main(String[] args){
try{
if(args.length<1){
System.out.println("Usage:jjava DBReadWriteFile aaa.jpg");
System.exit(0);
}
DBReadWriteFile my=new DBReadWriteFile();
my.WriteFile(args[0]);//测试,写一个文件到数据库中
my=new DBReadWriteFile();
my.ReadFile("new"+args[0]);//测试,从数据库读出文件到指定的文件中
//如果想支持非当前路径文件的话,需要修正"new"+args[0]
}catch(Exception e){e.printStackTrace();}
}
}//End class MobileDatabase
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货