mysql中clob怎么用_clob和blog在mysql数据库里的使用

package com.java1234.jdbc.chap06.sec02;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.sql.Blob;

import java.sql.Clob;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import com.java1234.jdbc.model.Book;

import com.java1234.jdbc.util.DbUtil;

public class Demo1 {

private static DbUtil dbUtil=new DbUtil();

/**

* 添加图书

* @param book

* @return

* @throws Exception

*/

private static int addBook(Book book)throws Exception{

Connection con=dbUtil.getCon(); // 获取连接

String sql="insert into t_book values(null,?,?,?,?,?,?)";

PreparedStatement pstmt=con.prepareStatement(sql);

pstmt.setString(1, book.getBookName());  // 给第一个坑设置值

pstmt.setFloat(2, book.getPrice());  // 给第二个坑设置值

pstmt.setString(3, book.getAuthor()); // 给第三个坑设置值

pstmt.setInt(4, book.getBookTypeId());  // 给第四个坑设置值

File context=book.getContext(); // 获取文件

InputStream inputStream=new FileInputStream(context);

pstmt.setAsciiStream(5, inputStream,context.length());  // 给第五个坑设置值

File pic=book.getPic(); // 获取图片文件

InputStream inputStream2=new FileInputStream(pic);

pstmt.setBinaryStream(6, inputStream2, pic.length()); // 给第六个坑设置值

int result=pstmt.executeUpdate();

dbUtil.close(pstmt, con);

return result;

}

public static void getBook(int id)throws Exception{

Connection con=dbUtil.getCon();

String sql="select * from t_book where id=?";

PreparedStatement pstmt=con.prepareStatement(sql);

pstmt.setInt(1, id);

ResultSet rs=pstmt.executeQuery();

if(rs.next()){

String bookName=rs.getString("bookName");

float price=rs.getFloat("price");

String author=rs.getString("author");

int bookTypeId=rs.getInt("bookTypeId");

Clob c=rs.getClob("context");

String context=c.getSubString(1, (int)c.length());

Blob b=rs.getBlob("pic");

FileOutputStream out=new FileOutputStream(new File("d:/pic2.jpg"));

out.write(b.getBytes(1, (int)b.length()));

out.close();

System.out.println("图书名称:"+bookName);

System.out.println("图书价格:"+price);

System.out.println("图书作者:"+author);

System.out.println("图书类型ID:"+bookTypeId);

System.out.println("图书内容:"+context);

}

dbUtil.close(pstmt, con);

}

public static void main(String[] args)throws Exception {

/*File context=new File("c:/helloWorld.txt");

File pic=new File("c:/pic1.jpg");

Book book=new Book("helloWorld", 100, "小锋", 1,context,pic);

int result=addBook(book);

if(result==1){

System.out.println("添加成功!");

}else{

System.out.println("添加失败!");

}*/

getBook(18);

}

}

package com.java1234.jdbc.model; import java.io.File; /**  * 图书模型  * @author caofeng  *  */ public class Book { private int id; private String bookName; private float price; private String author; private int bookTypeId; private File context; private File pic; public Book(int id, String bookName, float price, String author, int bookTypeId) { super(); this.id = id; this.bookName = bookName; this.price = price; this.author = author; this.bookTypeId = bookTypeId; } public Book(String bookName, float price, String author, int bookTypeId) { super(); this.bookName = bookName; this.price = price; this.author = author; this.bookTypeId = bookTypeId; } public Book(String bookName, float price, String author, int bookTypeId, File context) { super(); this.bookName = bookName; this.price = price; this.author = author; this.bookTypeId = bookTypeId; this.context = context; } public Book(String bookName, float price, String author, int bookTypeId, File context, File pic) { super(); this.bookName = bookName; this.price = price; this.author = author; this.bookTypeId = bookTypeId; this.context = context; this.pic = pic; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getBookTypeId() { return bookTypeId; } public void setBookTypeId(int bookTypeId) { this.bookTypeId = bookTypeId; } public File getContext() { return context; } public void setContext(File context) { this.context = context; } public File getPic() { return pic; } public void setPic(File pic) { this.pic = pic; } @Override public String toString() { return "["+this.id+","+this.bookName+","+this.price+","+this.author+","+this.bookTypeId+"]"; } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值