前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java大数据

Java大数据

作者头像
汤高
发布2018-01-11 17:27:54
6770
发布2018-01-11 17:27:54
举报
文章被收录于专栏:积累沉淀积累沉淀

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

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 {

代码语言:javascript
复制
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())”;

代码语言:javascript
复制
    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);
    }
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-06-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
大数据
全栈大数据产品,面向海量数据场景,帮助您 “智理无数,心中有数”!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档