用bytes写进去就可以了.pstmt.setBytes(n, bytes);
给分:)
给分:)
解决方案 »
- 关于正则表达式 菜鸟问题 逐个单词读入的问题
- JAVA是从什么语言衍伸过来的()
- poi输出excel时,页眉和页脚乱码,怎么办?
- 怎样由java程序中导出excel表格
- 这段代码为什么不能显示动画呢?
- sswater(水与争锋)请进,其它朋友能解决也进来,关于正则式
- 关于MQ的问题,急!急!急!请大家帮帮小弟
- SQL server连不上什么问题?
- deliver.pc是什么类型的文件?
- exception in thread "main" java.lang.NoClassDeffoundError:FindMax/class出现这种错误提示,我错在哪呢?
- 如果一段字符串中有太多敏感字符又不能一一进行转义怎么办?
- 两个小程序,麻烦大家给写一下(100分)
/*
1、目的:
将文件写入数据库;
从数据库读文件内容到指定的文件中。
2、样例数据库
a、
数据库类型:Microsoft Access 2000
数据库名称:db1.mdb
表名称: testword
表字段:
字段名称 数据类型
name 文本
content OLE对象
b、
数据库类型:Microsoft SQL Server 2000
数据库名称:citi
表名称: testword
表字段:
字段名称 数据类型
name varchar(10)
content image(16)
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");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//dbUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=d:\\db1.mdb";
dbUrl="jdbc:microsoft:sqlserver://tsc:1433;user=sa;password=mdzly109;DatabaseName=citi";
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:java 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
F:\java>java DBReadWriteFile aaa.jpg
Writing to database... from file:aaa.jpg
Write to database successfully
Reading from database... to file:newaaa.jpg
Read from database successfully