我知道用数据流的方式把图片弄成二进制形式存入到数据库
  但是我不知道该怎么去写这个东西
   希望各位能帮帮我
    谢谢
    最好给个例子 
  可以研究研究 
 

解决方案 »

  1.   

    给你贴一个吧,刚做的
    public static void insertBlob(Connection conn, byte[] data,
    String tableName, String keyName, String blobName, String colKey) {
    Statement stmt = null;
    ResultSet result = null;
    boolean defaultCommit = false;
    OutputStream outStream = null;
    /* 设置为不能自动提交 */
    try {
    defaultCommit = conn.getAutoCommit();
    conn.setAutoCommit(false); StringBuffer QuerySQL = new StringBuffer();
    StringBuffer EmptyQuerySQL = new StringBuffer();
    StringBuffer chekexistSQL = new StringBuffer();
    StringBuffer UptdateSQL = new StringBuffer();
    stmt = conn.createStatement(); chekexistSQL.append("SELECT * FROM ").append(tableName).append(
    " WHERE ").append(colKey).append("=").append("'").append(
    keyName).append("'"); EmptyQuerySQL.append("INSERT INTO ").append(tableName).append("(")
    .append(colKey).append(",").append(blobName).append(
    ") VALUES (").append("'").append(keyName).append(
    "',empty_blob()").append(")"); UptdateSQL.append("UPDATE ").append(tableName).append(" SET ")
    .append(blobName).append("=empty_blob() where ").append(colKey).append("='").append(keyName).append("'"); QuerySQL.append("SELECT  ").append(blobName).append(" FROM ")
    .append(tableName).append(" ").append(" WHERE ").append(
    colKey).append("=").append("'").append(keyName)
    .append("'").append(" FOR UPDATE"); result = stmt.executeQuery(chekexistSQL.toString());
    if (!result.next()) {
    /* 插入空的附件 sql 中包含empty_blob() */
    stmt.executeUpdate(UptdateSQL.toString());

    } else {
    stmt.executeUpdate(EmptyQuerySQL.toString());
    } /* 查询此BLOB对象并锁定 */
    result = stmt.executeQuery(QuerySQL.toString());
    if (result.next()) {

    oracle.sql.BLOB blob = (oracle.sql.BLOB) result
    .getBlob(blobName);
    outStream = blob.getBinaryOutputStream();
    // data是传入的byte数组,定义:byte[] data
    outStream.write(data, 0, data.length);
    outStream.flush();
    } else {
    // log.info("没有找到数据result=" + result.toString());
    }
    /* 正式提交 */
    conn.commit(); } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    } finally {
    try {
    if (outStream != null)
    outStream.close();
    } catch (IOException e1) {
    // TODO 自动生成 catch 块
    }
    try {
    if (result != null)
    result.close();
    } catch (SQLException e) {
    // log.debug("close result Exception");
    }
    try {
    if (stmt != null)
    stmt.close();
    } catch (SQLException e) {
    // log.debug("close stmt Exception");
    }
    try {
    if (conn != null)
    conn.setAutoCommit(defaultCommit);
    } catch (SQLException e) {
    // log.debug("close conn Exception");
    }
    // log.debug("finally!end");
    }
    }
      

  2.   

    其中data为你读取的前台图片的数据
      

  3.   

    很完整的代码
    http://blog.csdn.net/caoyinghui1986/archive/2008/04/05/2252772.aspx
    包括存储和读取。