2007-10-23

工作日志20071023(MySQL的用法章)

关键字: 工作日志
今天要花一天时间来玩MySQL
1.命令行
2.得到连接
3.批量插入数据
4.blob的插入
5.非jdbc方式(hibernate)
6.非java方式(Ruby)

以下为私事。
7.去接四姨与四姨夫(北京站)
-------------------------
8.老板要打东西,发现HP的驱动不行了。。。。
重安驱动。。。疯子了。
评论
抛出异常的爱 2007-10-23
package com.fengchunhua.sqltools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.Blob;

public class LotoOfMenmoneyBLOBIntoDB {
	private static Connection conn = JDBCConnectTools.getMySQLConnection();
	/**
	 * @param args
	 * @throws SQLException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws SQLException, IOException {
		LotoOfMenmoneyBLOBIntoDB db = new LotoOfMenmoneyBLOBIntoDB();
		db.insertIntoDB();
		db.getBlob();
		conn.commit();
  
	}	
	public void setConn(Connection conn){
		this.conn = conn;
	}
	public void setSql(String sql){
		
	}
	public void insertIntoDB() throws SQLException, IOException{
         try {  
   
             //创建一个PreparedStatement实例  
             PreparedStatement pstmt=conn.prepareStatement("insert into typetest (id,name,tid,nameblob) values(1,'mao','1001',?)");  
             File file = new File("D:\\mydoc\\workspace\\SQLsmallPage\\a.jpg") ;  
             FileInputStream fis = new FileInputStream(file);              
             //把输入流设置为预处理语句的对象。 
             System.out.println(file.length());
             pstmt.setBinaryStream(1, fis, (int)file.length());        
             //执行更新  
             pstmt.executeUpdate();                    
             pstmt.close();  
             fis.close();  
   
         }  
         catch(Exception e)  
         {  
             e.printStackTrace();  
         }  

	}
	public void getBlob() throws SQLException{
		
		try{
	             //创建一个Statement实例  
	             Statement stmt=conn.createStatement();  
	             ResultSet rst=stmt.executeQuery("select * from typetest where id=1");  
	             rst.next();   
	             //获得blob数据和它的输入流,然后通过输入流把数据写到文件中。         
	             Blob blob = (Blob) rst.getBlob("nameblob") ;  
	             FileOutputStream out=new FileOutputStream(new File("D:\\mydoc\\workspace\\SQLsmallPage\\d.jpg"));  
	             InputStream in=blob.getBinaryStream();  
	             int i;  
	             while((i=in.read())!=-1)  
	             out.write(i);  
	             //关闭输入、输出流.  
	             in.close();  
	             out.close();  
	   
	         } catch(Exception e){  
	             e.printStackTrace();  
	               
	         }  

	}

}
抛出异常的爱 2007-10-23
4.bolb的插入死活也插不进去。
Data too long for column
这个错误
在google.cn上死活找不到原因,都是说代码问题的。

忙了两个小时。。。。
换 了google.com英文版之后头一个记录就是。。。
http://www.google.com/search?hl=en&q=java+blob++Data+too+long+for+column+&btnG=Google+Search
http://mail-archives.apache.org/mod_mbox/jackrabbit-dev/200604.mbox/%3C44461DE6.5060107@gmail.com%3E
引用
hi daniel
the default schema for mysql uses 'blob' columns for storing
serialized node states.
the blob data type has a maximum size limit of 65k. if your repository needs
to store large numbers of child nodes per node you can use the 'mediumblob'
data type instead.


原来blob只能放65K的大小的东东。。。
我用的图片有66K。。。我这个叫晕。。。。
改完之后的代码:
发表评论

您还没有登录,请登录后发表评论

抛出异常的爱
搜索本博客
我的相册
1569dbfd-b702-3ce0-a83c-605b146f22c2-thumb
长椅
共 39 张
存档
最新评论