当前位置:编程学习 > JS >>

从数据库中读取一个图片并保存为一个图片文件

import java.sql.*;
import java.io.*;

public class WriteDB {
        public static void main(String[] args)  {
        Connection conn=null;
        String driver="com.MySQL(和PHP搭配之最佳组合).jdbc.Driver";//驱动
        String url="jdbc:MySQL(和PHP搭配之最佳组合)://127.0.0.1/test?useUnicode=true;characterEncoding=8859_1";// 数据库联接

        try{
               Class.forName(driver).newInstance();
               conn=DriverManager.getConnection(url,"root","");
               File file=new File(args[0]);
               FileInputStream fis=new FileInputStream(file);
               PreparedStatement pstmt=conn.prepareStatement(
                                "insert into picture(picture) values(?)");
               int bytes=(int)file.length();
               System.out.println(bytes);
  
               pstmt.setBinaryStream(1,fis,bytes);//1为插入的参数1,2。。。fileInStream为插入的数据,bytes为字节长度
               pstmt.executeUpdate();

               conn.close();
               fis.close();
     }catch(Exception e){
              System.out.println(e.getMessage());
     }
     }
}

补充:Web开发 , Jsp ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,