java sql server2000 存入⼀读取 图片

2024-12-16 02:53:52
推荐回答(1个)
回答1:

package mshtang.large;

import java.sql.*;
import java.io.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;

import mshtang.html.DatabaseForHtml;

public class LargeObjectAction
{
public void orablob()
{
}

public static void insertblob()
{
try
{
//首先是将文件输入到数据库。
Class.forName("oracle.jdbc.driver.OracleDriver");//注册数据库引擎。
String dbURL = "jdbc:oracle:thin:@192.168.0.74:1521:orcl";
String user = "pm";
String passWord = "pm";
Connection conn = DriverManager.getConnection(dbURL, user, passWord);
conn.setAutoCommit(false);//关闭自动提交,以提高性能。
Statement stmt = conn.createStatement();//建立会话
DatabaseForHtml sqlBean = new DatabaseForHtml();
String sqlStr = "";
String fileName = "";
String displayMessage = "";
try
{
sqlStr = "select table_name from user_tables where table_name='JPGTEST'";
if(!sqlBean.hasResult(conn, sqlStr))
{
displayMessage = (stmt.executeUpdate("create table JPGTEST (fname varchar2(600), bx blob)") == 0) ? "table created successfuly" : "table created failure";
System.out.println(displayMessage);
}
File file = new File("C:\\25.jpg");
fileName = file.getName();
FileInputStream inStream = new FileInputStream(file);//创建输入流,将外部文件输入到InputStream 中。
byte[] buffer = new byte[inStream.available()];
sqlStr = "INSERT INTO JPGTEST VALUES ('" + fileName + "', empty_blob())";
stmt.execute(sqlStr);
ResultSet rs = stmt.executeQuery("select bx from JPGTEST where fname='" + fileName + "' for update");
if(rs.next())
{
BLOB blob = ((OracleResultSet)rs).getBLOB("bx");
OutputStream outStream = blob.getBinaryOutputStream();
inStream.read(buffer);
outStream.write(buffer);
outStream.flush();
stmt.execute("commit");
outStream.close();
System.out.println("文件成功写入数据库");
}
inStream.close();
rs.close();
}
catch(SQLException e)
{
e.printStackTrace();
System.out.println("数据库异常:" + e.getMessage());
}
//以下是从库中读取文件。
sqlStr = "SELECT * from JPGTEST where fname='" + fileName + "'";
System.out.println(sqlStr);
ResultSet rset = stmt.executeQuery(sqlStr);
if(rset.next())
{
BLOB blob = ((OracleResultSet)rset).getBLOB("bx");//获取文件字段。
InputStream inStream = blob.getBinaryStream();//建立输入流,并将字段bx的值以流的形式,放入inStream变量。
File binaryFile = new File("D:\\test\\" + fileName);
FileOutputStream fileOutStream = new FileOutputStream(binaryFile);//创建文件输出流。
int by = inStream.read();
while(by != -1)
{
fileOutStream.write(by);
by = inStream.read();
}
fileOutStream.flush();
System.out.println("文件从数据库中成功读出");
inStream.close();//关闭流
fileOutStream.close();
}
rset.close();
stmt.close();
conn.close();
}
catch(Exception ee)
{
System.out.println(ee.getMessage());
}
}

public static void insertLongRow()
{
try
{
//首先是将文件输入到数据库。
Class.forName("oracle.jdbc.driver.OracleDriver");//注册数据库引擎。
String dbURL = "jdbc:oracle:thin:@192.168.0.74:1521:orcl";
String user = "pm";
String passWord = "pm";
Connection conn = DriverManager.getConnection(dbURL, user, passWord);
conn.setAutoCommit(false);//关闭自动提交,以提高性能。
Statement stmt = conn.createStatement();//建立会话
DatabaseForHtml sqlBean = new DatabaseForHtml();
String sqlStr = "";
String fileName = "";
String displayMessage = "";
try
{
sqlStr = "select table_name from user_tables where table_name='rowTestTable'";
if(!sqlBean.hasResult(conn, sqlStr))
{
// displayMessage = (stmt.executeUpdate("create table rowTestTable(fileName varchar2(50), detail long raw)") == 0) ? "table created successfuly" : "table created failure";
// System.out.println(displayMessage);
}
File file = new File("C:\\工程信息管理系统-内参.doc");
fileName = file.getName();
FileInputStream inStream = new FileInputStream(file);//创建输入流,将外部文件输入到InputStream 中。
// byte[] buffer = new byte[inStream.available()];
PreparedStatement pstmt = conn.prepareStatement("insert into rowTestTable values('" + fileName + "', ?)");
pstmt.setBinaryStream (1, inStream, (int)file.length());
if(pstmt.executeUpdate() == 1)
{
System.out.println("ok");
}
else
{
System.out.println("bad");
}
inStream.close();
}
catch(SQLException e)
{
e.printStackTrace();
System.out.println("数据库异常:" + e.getMessage());
}
//以下是从库中读取文件。
sqlStr = "SELECT * from rowTestTable where fileName='" + fileName + "'";
System.out.println(sqlStr);
ResultSet rset = stmt.executeQuery(sqlStr);
if(rset.next())
{
// BLOB blob = ((OracleResultSet)rset).getBLOB("bx");//获取文件字段。
InputStream inStream = rset.getBinaryStream("detail");//建立输入流,并将字段bx的值以流的形式,放入inStream变量。
File binaryFile = new File("D:\\test\\" + fileName);
FileOutputStream fileOutStream = new FileOutputStream(binaryFile);//创建文件输出流。
int by = inStream.read();
while(by != -1)
{
fileOutStream.write(by);
by = inStream.read();
}
fileOutStream.flush();
System.out.println("文件从数据库中成功读出");
inStream.close();//关闭流
fileOutStream.close();
}
rset.close();
stmt.close();
conn.close();
}
catch(Exception ee)
{
System.out.println(ee.getMessage());
}
}

public static void main(String args[])
{
// insertblob();
insertLongRow();
}
}