当前位置: 代码迷 >> Java Web开发 >> help me !jsp中如何读取mysql中的text 和blob类型字段?
  详细解决方案

help me !jsp中如何读取mysql中的text 和blob类型字段?

热度:57   发布时间:2016-04-17 15:57:45.0
help me !jsp中怎么读取mysql中的text 和blob类型字段???
小弟我刚学jsp和mysql没几天,请问高手怎样在jsp页面中读取mysql中的text,blob类型字段啊!

------解决方案--------------------
java.sql.Connection conn;
ResultSet rs=null;
Class.forName( "org.gjt.mm.mysql.Driver ").newInstance();
conn= java.sql.DriverManager.getConnection( "jdbc:mysql://localhost/test ", "root ", " ");
Statement stmt=conn.createStatement();
rs=stmt.executeQuery( "select * from test where id= '1 ' ");
if(rs.next())
{
Blob b = rs.getBlob( "pic ");

int size =(int)b.length();
out.print(size);
InputStream in=b.getBinaryStream();
byte[] by= new byte[size];
response.setContentType( "image/jpeg ");
ServletOutputStream sos = response.getOutputStream();
int bytesRead = 0;
while ((bytesRead = in.read(by)) != -1) {
sos.write(by, 0, bytesRead);
}
in.close();
sos.flush();

}
  相关解决方案