Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5379410
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: Mysql/postgreSQL

2008-11-26 14:47:50

package com.kevin.mysql;
/*
* mysql 插入blog的例子
*
*/

import java.io.*;
import java.sql.*;
                                                    
public class DBTest {
  public static void main(String[] args) {
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";
    String user = "root";
    String password = "";
    try {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, user, password);

      
        File file = new File("./laurapausini.jpg");
        int length = (int) file.length();
        InputStream fin = new FileInputStream(file);

      
        PreparedStatement pstmt = conn.prepareStatement(
              "INSERT INTO files(description,filecontent) VALUES(?, ?)");
        pstmt.setString(1, "Logo");
        pstmt.setBinaryStream (2, fin, length);
        pstmt.executeUpdate();
        pstmt.clearParameters();
        pstmt.close();
        fin.close();

      /*
        * 读取blob输出到文件
        */

        Statement stmt = conn.createStatement();
        ResultSet result = stmt.executeQuery("SELECT * FROM files");
        result.next();
        String description = result.getString(1);
        Blob blob = result.getBlob(2);

        
        System.out.println("描述:" + description);
        FileOutputStream fout = new FileOutputStream("./laurapausini2.jpg");
        fout.write(blob.getBytes(1, (int)blob.length()));
        fout.flush();
        fout.close();
                                                    
        stmt.close();
        conn.close();
    }
    catch(ClassNotFoundException e) {
        System.out.println("找不到驱动");
        e.printStackTrace();
    }
    catch(SQLException e) {
        e.printStackTrace();
    }
    catch(IOException e) {
        e.printStackTrace();
    }
  }
}

阅读(3990) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-11-26 14:53:30

// 一个比较差的例子。。。。。。。。。。。仅供参考。。 2,3,10,11,12,17,18 linux下的mysql数据库和windows下的mysql数据库可以互移(已经测试将linux下的mysql数据库移动到window下的mysql下的相关目录,可以正常访问该数据库); mysql数据库怎么插入图片,大文本? 在存在的表中添加列: mysql> alter table addressbook add pic blob; Query OK, 1 row affected (0.23 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from addressbook; +----+------+----------------+--------------+------+ | id | name | address | phone | pic | +----+------+----------------+-------