📄 blobbean.java~156~
字号:
package com.sztheater.biz.programe;
import java.sql.*;
import javax.sql.*;
import java.io.*;
import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.BLOB;
public class BlobBean
{
private Connection conn ;
/**
*构造方法,创建Connection对象,并且在数据库中添加一个表。
*/
public void FreeConn()
{
try
{
if(conn != null)
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
conn = null;
}
public BlobBean()throws Exception
{
String strDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String strUrl = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=SZTheater";
String strUser = "SZTheater";
String strPass = "SZTheater";
Class.forName(strDriver).newInstance();
DriverManager.setLoginTimeout(15);
conn = DriverManager.getConnection(strUrl, strUser, strPass);
// conn.createStatement().execute("create table blobtable(blobvalue blob)");
}
/**
*写入Blob数据到数据库
*/
public void addBlob(String fileName)throws Exception
{
conn.setAutoCommit(false);
String strSQL = "insert into blobtable values (?)";
PreparedStatement pstmt = conn.prepareStatement(strSQL);
File binaryFile = new File(fileName);
System.out.println(fileName+"'s length = " + binaryFile.length());
FileInputStream instream = new FileInputStream(binaryFile);
FileInputStream inputtextfile=new FileInputStream(fileName);
int len = inputtextfile.available();
byte[] bufferArray = new byte[len];
inputtextfile.read(bufferArray);
pstmt.setBytes(1,bufferArray);
pstmt.execute();
conn.commit();
}
/**
*从数据库读取blob数据,并且保存在文件系统。
*/
public void readBlob(String fileName)throws Exception
{
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("SELECT blobvalue FROM blobtable");
byte buffer[] = null;
while (rset.next()) {
buffer = rset.getBytes("blobvalue");
}
FileOutputStream file_out = new FileOutputStream(new File(fileName));
int temp;
file_out.write(buffer);//读取数据、写入文件系统
file_out.close();
conn.commit();
}
public void test()
{
}
public static void main(String args[])
{
String str = "我";
byte by[] = new byte[2];
by = str.getBytes();
for(int i = 0 ;i <by.length ;i ++)
{
System.out.println(by[i]);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -