博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java大数据
阅读量:4180 次
发布时间:2019-05-26

本文共 4475 字,大约阅读时间需要 14 分钟。

大数据(像数据库中插入图片)


import java.sql.SQLException;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class TestPIC {

protected Shell shell;private Text text;/** * Launch the application. * @param args */public static void main(String[] args) {    try {        TestPIC window = new TestPIC();        window.open();    } catch (Exception e) {        e.printStackTrace();    }}/** * Open the window. */public void open() {    Display display = Display.getDefault();    createContents();    shell.open();    shell.layout();    while (!shell.isDisposed()) {        if (!display.readAndDispatch()) {            display.sleep();        }    }}/** * Create contents of the window. */protected void createContents() {    shell = new Shell();    shell.setSize(450, 300);    shell.setText("SWT Application");    Label label = new Label(shell, SWT.NONE);    label.setBounds(20, 95, 61, 17);    label.setText("头像");    text = new Text(shell, SWT.BORDER);    text.setBounds(113, 95, 192, 23);    Button button = new Button(shell, SWT.NONE);    button.addSelectionListener(new SelectionAdapter() {        @Override        public void widgetSelected(SelectionEvent e) {            FileDialog dialog = new FileDialog(shell,SWT.OPEN);               String basepath=System.getProperty("user.home");//设置 初始路径            dialog.setFilterPath(basepath);            dialog.setFilterExtensions(new String[]{"*.jpg","*.jpeg","*.png"});  //它使用指定的扩展名集合进行过滤            String fileName = dialog.open();            if(fileName==null||"".equals(fileName)){                return;            }            text.setText(fileName);        }    });    button.setBounds(344, 91, 80, 27);    button.setText("浏览");    Button button_1 = new Button(shell, SWT.NONE);    button_1.addSelectionListener(new SelectionAdapter() {        @Override        public void widgetSelected(SelectionEvent e) {            String picpath=text.getText().trim();            PicBiz pb=new PicBiz();            try {                pb.savePic(picpath);            } catch (Exception e1) {                // TODO Auto-generated catch block                e1.printStackTrace();            }        }    });    button_1.setBounds(168, 156, 80, 27);    button_1.setText("保存");}

}

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

import com.yc.dao.dbhelper;

public class PicBiz {

public void savePic(String picpath) throws SQLException {
String picid=UUID.randomUUID().toString();
dbhelper db= new dbhelper();
String sql=”insert into pic values(?,empty_blob())”;

String sql2="select pic from pic where picid=? for update";//for update 锁定这一行数据            Connection con=null;    try {        con=db.getCon();        con.setAutoCommit(false);//关闭隐式提交        PreparedStatement pstmt=con.prepareStatement(sql);//创建语句对象 ,发给数据库执行        pstmt.setString(1, picid);//设置参数        pstmt.execute();//数据库执行语句         pstmt=con.prepareStatement(sql2);        pstmt.setString(1, picid);        pstmt.execute();        ResultSet rs=pstmt.executeQuery(); //得到数据库中语句执行的结果集        if(rs!=null&&rs.next()){            OutputStream oos=null;//像数据库表中输出图片            InputStream iis=null;//从本地读文件            oracle.sql.BLOB b= (oracle.sql.BLOB) rs.getBlob(1);//取出图片所在位置            iis=new BufferedInputStream(new FileInputStream(new File (picpath)));//通过输入流读取图片数据            oos=b.getBinaryOutputStream();//从结果集中取出输入流 用于向数据库写数据            byte[] bs=new byte[1024];            int length=-1;            while(( length=iis.read(bs,0,bs.length))!=-1){                oos.write(bs,0,length);                oos.flush();            }            con.commit();        }    } catch (FileNotFoundException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (Exception e) {        // TODO Auto-generated catch block        e.printStackTrace();        con.rollback();    } finally{        con.setAutoCommit(true);    }    }

}

转载地址:http://ayhai.baihongyu.com/

你可能感兴趣的文章
vs2015 编译obs-studio
查看>>
IOS usleep函数导致线程阻塞
查看>>
简析H264分包组包
查看>>
H.264 视频 RTP 负载格式
查看>>
H.264句法和语法总结(一)句法元素的分层结构
查看>>
H.264句法和语法总结(二)NAL层句法
查看>>
H.264句法和语法总结(三)序列参数集层(SPS)句法
查看>>
H.264句法和语法总结(四)图像参数集语义
查看>>
H.264句法和语法总结(五)片头句法
查看>>
H.264句法和语法总结(六)参考帧队列重排序(reordering)句法
查看>>
H.264句法和语法总结(七)加权预测句法
查看>>
H.264句法和语法总结(八)参考图像序列标记 (marking)操作的语义
查看>>
H.264句法和语法总结(九)片层数据句法
查看>>
H.264句法和语法总结(十一)宏块层预测句法
查看>>
H.264句法和语法总结(十二)子宏块预测句法
查看>>
H.264句法和语法总结(十三)残差句法
查看>>
H.264句法和语法总结(十四)CAVLC 残差句法
查看>>
git修改远程仓库地址
查看>>
vs编译std::numeric_limits::max()编译错误
查看>>
WebRTC的RTP、RTCP协议实现分析
查看>>