/**
*说明:
*通过jdbc-odbc往Access数据库中插入图片、或者乱七八糟的文件都行
*注意,用DataInputStream会出错
*
/
import java.sql.*;
import java.io.FileInputStream;
public class sss
{
public static void main(String args[])
{
try
{
//连接ACCESS数据库,这里用了连接串哦
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
java.sql.Connection conctn = DriverManager.getConnection(
"jdbc:odbc:;"+
"DRIVER=Driver do Microsoft Access (*.mdb);"+
"UID=admin;"+
"UserCommitSync=Yes;"+
"Threads=3;"+
"SafeTransactions=0;"+
"PageTimeout=5;"+
"MaxScanRows=8;"+
"MaxBufferSize=2048;"+
"FIL=MS Access;"+
"DriverId=25;"+
//"DefaultDir=C:\\Documents and Settings\\Administrator\\My Documents;"+
"DBQ=D:\\gtk.mdb"
);
int length = 0;
//
// 连接对象设置数据库为
//
conctn.setAutoCommit(false);
//
// 预编译指令
//
PreparedStatement pstmt =conctn.prepareStatement( "insert into testsaveimg ( title, image ) values ( ? , ? )" );
//
//得到一个文件输入流
//
FileInputStream in = new FileInputStream( "D:\\test.jpg" );
length = in.available();
System.out.println( in.toString() +" has "+ length +" bytes" );
//
// 设置插入的值
//
pstmt.setString( 1, "插入图片的标题" );
pstmt.setBinaryStream( 2, in , in.available() );
//这里in是文件输入流,如果用DataInputStream就会出错,in.available()是in的长度
//
// 执行插入操作
//
System.out.println( "插入了 "+ pstmt.executeUpdate ()+ " 行数据" );
conctn.commit();
pstmt.close();
conctn.close();
}catch(Exception e){
e.printStackTrace();
System.out.println("出错了!");
}
}
}
……