通過(guò)jdbc-odbc往Access數(shù)據(jù)庫(kù)中OLE分類(lèi)的表中插入數(shù)據(jù)的例子
發(fā)表時(shí)間:2023-08-13 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]/***說(shuō)明:*通過(guò)jdbc-odbc往Access數(shù)據(jù)庫(kù)中插入圖片、或者亂七八糟的文件都行*注意,用DataInputStream會(huì)出錯(cuò)*/import java.sql.*;import jav...
/**
*說(shuō)明:
*通過(guò)jdbc-odbc往Access數(shù)據(jù)庫(kù)中插入圖片、或者亂七八糟的文件都行
*注意,用DataInputStream會(huì)出錯(cuò)
*
/
import java.sql.*;
import java.io.FileInputStream;
public class sss
{
public static void main(String args[])
{
try
{
//連接ACCESS數(shù)據(jù)庫(kù),這里用了連接串哦
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;
//
// 連接對(duì)象設(shè)置數(shù)據(jù)庫(kù)為
//
conctn.setAutoCommit(false);
//
// 預(yù)編譯指令
//
PreparedStatement pstmt =conctn.prepareStatement( "insert into testsaveimg ( title, image ) values ( ? , ? )" );
//
//得到一個(gè)文件輸入流
//
FileInputStream in = new FileInputStream( "D:\\test.jpg" );
length = in.available();
System.out.println( in.toString() +" has "+ length +" bytes" );
//
// 設(shè)置插入的值
//
pstmt.setString( 1, "插入圖片的標(biāo)題" );
pstmt.setBinaryStream( 2, in , in.available() );
//這里in是文件輸入流,如果用DataInputStream就會(huì)出錯(cuò),in.available()是in的長(zhǎng)度
//
// 執(zhí)行插入操作
//
System.out.println( "插入了 "+ pstmt.executeUpdate ()+ " 行數(shù)據(jù)" );
conctn.commit();
pstmt.close();
conctn.close();
}catch(Exception e){
e.printStackTrace();
System.out.println("出錯(cuò)了!");
}
}
}