这里有从数据库读取取图片到Panel的程序,写回到数据库也差不多,自己琢磨一下吧//从数据库中读取图片
private void readImageToPanel()
{
String strNo = null;
InputStream image = null;
Date date = null;
try
{
ResultSet rs = statement.executeQuery("select c_no, c_date, c_image from tb_test where c_no = '0001'");
while (rs.next())
{
System.out.println("write image to panel:");
strNo = rs.getString("c_no");
date = rs.getDate("c_date");
image = rs.getBinaryStream("c_image");
System.out.println("c_no:" + strNo + " c_date:" + date + " c_image:" + image); //DEBUG if(image == null)
{
rs.close();
System.out.println("image data is null");
return;
}
}
rs.close();
}
catch (SQLException ex)
{
ex.printStackTrace();
} if(image == null)
return; //读取图片并绘制到Panel上
BufferedImage bufferedImage = null;
try
{
bufferedImage = ImageIO.read(image);
}
catch (Exception ex1)
{
ex1.printStackTrace();
}
if (bufferedImage != null)
{
ImagePanel imagePanel = new ImagePanel(strNo, date, bufferedImage);
this.getContentPane().add(imagePanel);
} }
//显示图片的Panel
class ImagePanel
extends JPanel
{
protected String m_strNo; protected String m_imageDate; protected Image m_source; protected Image m_img; public ImagePanel(String imageNo, Date imageDate, Image imageSource)
{
m_strNo = imageNo;
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
m_imageDate = dateFormat.format(imageDate);
m_source = m_img;
m_img = imageSource;
setBackground(Color.white);
setBorder(new MatteBorder(1, 1, 2, 2, Color.black));
}
public void setScaledSize(int w, int h)
{
m_img = m_source.getScaledInstance(w, h, Image.SCALE_SMOOTH);
repaint();
}
public Dimension getPreferredSize()
{
Insets ins = getInsets();
return new Dimension(m_img.getWidth(null) + ins.left + ins.right, m_img.getHeight(null) + ins.top + ins.bottom);
}
public Dimension getMaximumSize()
{
return getPreferredSize();
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public void paint(Graphics g)
{
g.setColor(getBackground());
g.drawImage(m_img, 0, 0, this);
g.setColor(Color.red);
g.drawString(this.m_strNo, 0, 10);
g.drawString(this.m_imageDate, 0, 20);
paintBorder(g);
}
}
private void readImageToPanel()
{
String strNo = null;
InputStream image = null;
Date date = null;
try
{
ResultSet rs = statement.executeQuery("select c_no, c_date, c_image from tb_test where c_no = '0001'");
while (rs.next())
{
System.out.println("write image to panel:");
strNo = rs.getString("c_no");
date = rs.getDate("c_date");
image = rs.getBinaryStream("c_image");
System.out.println("c_no:" + strNo + " c_date:" + date + " c_image:" + image); //DEBUG if(image == null)
{
rs.close();
System.out.println("image data is null");
return;
}
}
rs.close();
}
catch (SQLException ex)
{
ex.printStackTrace();
} if(image == null)
return; //读取图片并绘制到Panel上
BufferedImage bufferedImage = null;
try
{
bufferedImage = ImageIO.read(image);
}
catch (Exception ex1)
{
ex1.printStackTrace();
}
if (bufferedImage != null)
{
ImagePanel imagePanel = new ImagePanel(strNo, date, bufferedImage);
this.getContentPane().add(imagePanel);
} }
//显示图片的Panel
class ImagePanel
extends JPanel
{
protected String m_strNo; protected String m_imageDate; protected Image m_source; protected Image m_img; public ImagePanel(String imageNo, Date imageDate, Image imageSource)
{
m_strNo = imageNo;
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
m_imageDate = dateFormat.format(imageDate);
m_source = m_img;
m_img = imageSource;
setBackground(Color.white);
setBorder(new MatteBorder(1, 1, 2, 2, Color.black));
}
public void setScaledSize(int w, int h)
{
m_img = m_source.getScaledInstance(w, h, Image.SCALE_SMOOTH);
repaint();
}
public Dimension getPreferredSize()
{
Insets ins = getInsets();
return new Dimension(m_img.getWidth(null) + ins.left + ins.right, m_img.getHeight(null) + ins.top + ins.bottom);
}
public Dimension getMaximumSize()
{
return getPreferredSize();
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public void paint(Graphics g)
{
g.setColor(getBackground());
g.drawImage(m_img, 0, 0, this);
g.setColor(Color.red);
g.drawString(this.m_strNo, 0, 10);
g.drawString(this.m_imageDate, 0, 20);
paintBorder(g);
}
}
//将数据库中的图片读出存到硬盘文件d:\temp\temp1.jpg
private void readImageToFile()
{
try
{
ResultSet rs = statement.executeQuery("select c_no, c_date, c_image from tb_test where c_no = '0001'");
while (rs.next())
{
System.out.println("write image to file:");
InputStream image = rs.getBinaryStream("c_image");
System.out.println("c_no:" + rs.getString("c_no") + " c_date:" + rs.getDate("c_date") + " c_image:" + image); //DEBUG if(image == null)
{
rs.close();
System.out.println("image data is null");
return;
} byte[] x = new byte [10*1024];
int lengthRead = 0;
String fileName = "d:\\temp\\temp1.jpg";
FileOutputStream outputStream = new FileOutputStream(fileName); while ((lengthRead = image.read(x)) != -1)
{
outputStream.write(x);
}
image.close();
outputStream.close();
}
rs.close();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
catch (IOException ex1)
{
ex1.printStackTrace();
}
}
这个在sql server里面是不是用varbinary啊?