import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.imageio.ImageIO;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class pictest
{
public pictest(){
JFrame f=new JFrame();
Container p=f.getContentPane();
ppic pic=null;
InputStream in=null;
try{
int i=3;
String url="**";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(url,"","");
//连接到数据库
if(!con.isClosed())
System.out.println("数据库连接成功");
String sql="select * from campusimage";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(sql);
for(int j=0;j<i;j++)
rs.next();
if(rs.next())
in=rs.getBinaryStream(1);
pic=new ppic(in);
rs.close();
st.close();
con.close();
}catch(Exception e){e.printStackTrace();}
p.setLayout(null);
p.add(pic);
f.setBounds(500,400,800,900);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class ppic extends JPanel
{ BufferedImage bi;
ppic(InputStream in){
try{
bi=ImageIO.read(in);
setBounds(0,0,700,600);
}catch(Exception e){e.printStackTrace();}
}
public void paint(Graphics g){
g.drawImage(bi,0,0,700,600,null);
}
}
public static void main(String[] args){
new pictest();
}
}
最新内容请见作者的GitHub页:http://qaseven.github.io/
时间: 2024-09-13 03:31:25