Java 操作MySql Blob 字段
来源:互联网 发布:oracle的sql语句 编辑:程序博客网 时间:2024/06/09 12:01
Java 操作MySql Blob字段
import java.sql.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class myImg
{
Connection conn;
Statement st;
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,?,?)");
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()
{
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)
{
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();
}
}
- Java 操作MySql Blob 字段
- MySQL 操作Blob字段工具(Java)
- mysql操作BLOB字段
- mysql blob字段存储图片操作示例
- PHP操作Mysql中的BLOB字段
- 使用java操作oracle的blob字段
- java操作oracle数据库中的blob字段
- java 纯sql 操作blob..字段
- JAVA读取MySQL中的blob字段
- 使用java语言操作,如何来实现MySQL中Blob字段的存取
- ORACLE操作BLOB字段
- 有关blob字段操作
- C#操作BLOB字段
- jeecg 操作BLOB字段
- mysql blob字段导入
- hibernate处理MySQL中Blob字段的更新操作
- java 写blob字段
- java对oracle中blob字段的操作
- jDom 和dom4j 输出的中文乱码的解决方案
- ajax简单的封装一下
- Android之Adapter用法总结
- linux: repair filesystem
- 10位数字的比较,找出最大值。
- Java 操作MySql Blob 字段
- 数据结构之栈的应用----算术表达式的实现
- 使用 IAR for 51 实验 LED 灯模拟二进制加减法.
- Android自动化测试解决方案
- 数据结构之栈的应用----迷宫求解
- OpenCV2.4 例程解析一
- 黑马程序员_Java集合总结(二)
- web前台时间控件
- C#在VisualStudio中操作SQL数据库文件