Java 操作MySql Blob 字段
1
import java.sql.*; import java.io.*;
import javax.swing.*; import java.awt.*;
import java.awt.event.*; class myImg
{
Connection conn; Statement st;
2
PreparedStatement ps,pu; ResultSet rs; JLabel l1; JFrame f; JFileChooser jf; String fn; JTextField t1,t2; JButton bi,bimg,bf,bp,bn,bl; JPanel p1,p2,p3,p4; myImg() { f=new JFrame("图片字段操作"); f.setDefaultCloseOperation(3); f.setBounds(100,100,600,500); jf=new JFileChooser("."); Container c=f.getContentPane(); c.setLayout(new BorderLayout()); p1=new JPanel(); p2=new JPanel(); p3=new JPanel(); p4=new JPanel(); p4.setLayout(new BorderLayout()); bi=new JButton("插入记录"); bf=new JButton("第一条记录"); bp=new JButton("上一条记录"); bn=new JButton("下一条记录"); bl=new JButton("最后条记录"); bimg=new JButton("修改照片"); t1=new JTextField(10); t2=new JTextField(10); p3.add(t1); p3.add(bi); p3.add(bimg); p2.add(t2);p2.add(p3);p2.add(bf);p2.add(bp);p2.add(bn);p2.add(bl); p4.add(p2,"Center"); p4.add(p3,"South"); l1=new JLabel(""); p1.add(l1); c.add(p1,"North"); c.add(p4,"South"); f.setVisible(true); try { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/myschool","root","root"); ps=conn.prepareStatement("insert into img values(null,?,?)"); 3
pu=conn.prepareStatement("Update img set Img=? where SName=?"); st=conn.createStatement(); rs=st.executeQuery("select * from img"); rs.next(); mm(); } catch(ClassNotFoundException ex){} catch(SQLException exx){} bf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { rs.first(); mm(); } catch(SQLException ex){} } }); bp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { rs.previous(); mm(); } catch(SQLException ex){} } }); bn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { rs.next(); mm(); } catch(SQLException ex){} } }); bl.addActionListener(new ActionListener() 4
{ public void actionPerformed(ActionEvent e) { try { rs.last(); mm(); } catch(SQLException ex) { System.out.println("error"); } } }); bi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { InputStream in=null; if(jf.showDialog(f,"插入图片")==JFileChooser.APPROVE_OPTION); { fn=jf.getSelectedFile().getPath(); try { File f=new File(fn); in=new FileInputStream(f); ps.setString(2,t1.getText().trim()); ps.setBinaryStream(1,in,(int)f.length()); pu.executeUpdate(); rs=st.executeQuery("select * from img"); } catch(SQLException exx) { System.out.println("error1"); } catch(IOException ex) { System.out.println("error2"); } } } }); bimg.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { 5
} InputStream in=null; if(jf.showDialog(f,"插入图片")==JFileChooser.APPROVE_OPTION); { fn=jf.getSelectedFile().getPath(); try { File f=new File(fn); in=new FileInputStream(f); pu.setBinaryStream(1,in,(int)f.length()); pu.setString(2,t2.getText().trim()); pu.executeUpdate(); rs=st.executeQuery("select * from img"); } catch(SQLException exx) { System.out.println("error1"); } catch(IOException ex) { System.out.println("error2"); } } } }); } void mm() throws SQLException { t2.setText(rs.getString(2)); Blob blob=rs.getBlob(3); if(blob!=null) { ImageIcon icon=new ImageIcon(blob.getBytes(1L,(int)blob.length())); l1.setIcon(icon); } else l1.setIcon(null); } public static void main(String[] ss) { new myImg(); } 6