clob和blog在mysql数据库里的使用
来源:互联网 发布:西服 马甲 知乎 编辑:程序博客网 时间:2024/06/04 18:01
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+"]";
}
}
- clob和blog在mysql数据库里的使用
- 在oracle里插入CLOB的程序
- 在数据库中使用CLOB存储
- 数据库的clob和blob操作
- 关于数据库里cascade的用法,在MySQL和Hibernate是不同的
- 关于数据库里cascade的用法,在MySQL和Hibernate是不同的
- mysql和Oracle在对clob和blob字段的处理
- MySQL int(11) 和 int(3) 在数据库里存储一样的结果
- MySQL int(11) 和 int(3) 在数据库里存储一样的结果
- java clob和blob的使用
- java clob和blob的使用
- ORACLE 里的CLOB应用
- 关于ORACLE里由string转成CLOB存入数据库里的问题
- 在SQL Server 2000里设置和使用数据库复制
- 在SQL Server 2000里设置和使用数据库复制
- 在SQL Server 2000里设置和使用数据库复制
- 在SQL Server 2000里设置和使用数据库复制
- 在SQL Server 2000里设置和使用数据库复制
- C++调Python示例
- 有趣与愉快-------罗辑思维整理
- 更安全的signal handling————使用Reentrant Function
- IOS成长之路-使用系统默认声音、震动
- CSS样式生成 多张图片合成为1张 制作过程 代码控制
- clob和blog在mysql数据库里的使用
- 一起学java设计模式--代理模式(结构型模式)
- Android Bitmap 全面解析(三)开源图片框架分析1-UIL(上) ...
- Apple Tree - POJ 3321 树状数组+离散化+N叉树
- Cocos2dx特效 水纹特效1
- Android Bitmap 全面解析(三)开源图片框架分析1-UIL(下) ...
- 最牛html学习路线
- Java数据传递实验
- 跟着Android学设计模式:工厂方法(Factory method)