看了看没发现问题,特意试了试,也没问题有点区别是我用的com.mysql.jdbc.Driver

解决方案 »

  1.   

    你把那些Exception粘出来,我看了一下也没有什么问题。
      

  2.   

    给你一个我写的,和你的功能差不多,但是没问题。
    import java.io.*;
    import java.util.*;
    import java.sql.*;
    public class BlobColumn 
    {
    public static void main(String[] args) 
    {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost:3306/test";
    String user = "root";
            String password = "roger";
    Vector stream=null;
    stream=new Vector();
            try
    {
    Class.forName(driver);
            }
    catch(ClassNotFoundException e) 
    {
    System.out.println("ClassNotFoundException ->"+e);
            }
    try
    {
                Connection conn = DriverManager.getConnection(url, user, password);
    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO friend VALUES(?,?,?)");
    try
    {
    //String path="D:\\book\\photo\\";
    //String path="D:\\pictrue\\美女精华\\";
    String path="D:/pictrue/girl/";
    File d=new File(path);//建立当前目录中文件的File对象
    File list[]=d.listFiles();//取得代表目录中所有文件的File对象数组
    for(int i=0;i<list.length;i++)
    {
    if(list[i].isFile())
    {
    String FileName=list[i].getName();
    InputStream fin = new FileInputStream(path+FileName);
    int length = (int) fin.available();
    System.out.println("FileName ->"+FileName+"     Size ->"+length);
    stream.add(fin);
    pstmt.setString(1,FileName);
    pstmt.setBinaryStream(2,fin,length);
    pstmt.setDouble(3,length);
    pstmt.addBatch();
    }
    }
    }
    catch (Exception e)
    {
    System.out.println("IOException ->"+e);
    }
    int[] res=pstmt.executeBatch();
               // pstmt.clearParameters();
                pstmt.close();
    for (int i=0;i<stream.size();i++)
    {
    try
    {
    InputStream in=(InputStream)stream.get(i);
    in.close();
    }
    catch (Exception e)
    {
    System.out.println("close exception ->"+e);
    }
    }
             /*   fin.close();
                // 從資料庫取出檔案  
    Statement stmt = conn.createStatement();
                ResultSet result = stmt.executeQuery("SELECT * FROM picture");
                result.next();
                String description = result.getString(1);
                Blob blob = result.getBlob(2);
    System.out.println("blob length ->"+(int)blob.length());
                // 寫入檔案           
    System.out.println("檔案描述:" + description);
                FileOutputStream fout = new FileOutputStream("girl_2.jpg");
    fout.write(blob.getBytes(1, (int)blob.length()));
                fout.flush();
                fout.close();    */       
               // conn.close();
    }
    catch(Exception e) 
    {  
    System.out.println("SQLException ->"+e);
            }   
    }
    }