可以把图片转换成流然后向SQL 中的字段中写入。。

解决方案 »

  1.   

    String sql = "insert into t (name,photo) values('jack',?')";
    PrepareStatement pstmt = conn.createPrepareStatement(sql);
    sql.setByte(1,photoBytesArray,byteArrayLength);
    //or
    //sql.setInputStream(1,photoFileStream,length);
    pstmt.executeUpdate();
      

  2.   

    我试过了有错
    "upImage.java": Error #: 300 : method setInputStream(int, java.io.FileInputStream, int) not found in interface java.sql.PreparedStatement at line 24, column 12
    请在帮忙
      

  3.   

    Connection conn = null;
      try{
        DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
        conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://IP:1433;DataBaseName=Moftec","sa","mymm");    
      }
      catch(Exception e )
      {
        //捕捉异常并抛出IOException
        out.println("Can not connec to the database! The exception is " + e.toString());
      }  
      FileInputStream  fis=null;  
      File  file  =  new  File("E:/java/chart.jpg");
      try{  
        fis  =  new  FileInputStream(file);  
      }catch(FileNotFoundException  e){
        out.println("Not find file!");
      }
      PreparedStatement  ps  =  conn.prepareStatement("Insert into gs_img (lei,years,img) values (?,?,?)");  
      ps.setString(1,"ivan");
      ps.setInt(2,4);  
      ps.setBinaryStream(3,fis,(int)file.length());
      ps.executeUpdate();
      ps.close();  
      try{  
        fis.close();  
        out.println("写进去了!");
      }catch(IOException  e){
        out.println("fis cann't cloase!");
      }
    保证正确。
      

  4.   

    上面程序在jsp 中测试通过
      

  5.   

    麻烦你再看看,我这么写和你那么写有什么不一样,为什么不行?
    是因为装载驱动程序不一样的原因吧??
    还是因为我数据库表的字段类型的不同,
    我的数据库中用data存储图信息,类型为binary!!
    真的很感谢你再给我解释一下
    import java.sql.*;
    import java.io.*;
    public class upImage {
       public static void main(String args[]) {
         try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection ("jdbc:odbc:SQLname","sa","");
            File files = new File("d:\\saint.jpg");
            FileInputStream fis=new FileInputStream(files);
            PreparedStatement ps = conn.prepareStatement("INSERT INTO Image (id,name,data) VALUES (?,?,?)");
            ps.setInt(1,4);
            ps.setString(2, files.getName());
            ps.setInputStream(3,fis,(int)files.length());
            ps.executeUpdate();
            ps.executeUpdate();
            fis.close();
            ps.close();      }catch (Exception e) {
            System.out.println(e.toString());
          }    }}
      

  6.   

    我给你的程序的数据表中 data 字段是imag 类型的。
      

  7.   

    ps.setInputStream 这个不对吧,应该是 ps.setBinaryStream 吧