Java大数据

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


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);
    }
    }

}

上一篇:RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密


下一篇:一起玩转树莓派(17)——BMP180数字压力传感器应用