隨筆-60  評論-35  文章-15  trackbacks-0

          環(huán)境mysql+tomcat:

          <1>先在mysql下建立如下的table. 并insert圖像.

          mysql.sql文件如下:

          CREATE ? TABLE ?photo?(
          photo_no?
          int ( 6 )?unsigned? NOT ? NULL ?auto_increment,
          image ?blob,
          PRIMARY ? KEY ?(`photo_no`)
          )?


          <2>把show.jsp放在tomcat的任意目錄下. show.jsp作用:從數(shù)據(jù)庫中讀出blob,并產(chǎn)生image/jpg.

          show.jsp文件如下:

          <% @?page?contentType = " text/html;?charset=gbk " ? %>
          <% @?page? import = " java.io.* " %>
          <% @?page? import = " java.sql.*,?javax.sql.* " ? %>
          <% @?page? import = " java.util.* " %>
          <% @?page? import = " java.math.* " %>

          <%
          ?String?photo_no?
          = ?request.getParameter( " photo_no " );

          ?
          // mysql連接?
          ?Class.forName( " com.mysql.jdbc.Driver " ).newInstance();
          ?String?URL
          = " jdbc:mysql://localhost:3306/job?user=root&password=111111 " ;
          ?Connection?con?
          = ?DriverManager.getConnection(URL);

          ?
          // oracle連接?
          ?
          // String?URL="jdbc:oracle:thin@localhost:1521:orcl2";
          ?
          // user="system";
          ?
          // password="manager";
          ?
          // Connection?con?=?DriverManager.getConnection(URL,user,password);


          ?
          try {
          ??
          // ?準(zhǔn)備語句執(zhí)行對象
          ??Statement?stmt? = ?con.createStatement();

          ??String?sql?
          = ? " ?SELECT?*?FROM?PHOTO?WHERE?photo_no?=? " + ?photo_no;
          ??ResultSet?rs?
          = ?stmt.executeQuery(sql);
          ??
          if ?(rs.next())? {
          ???Blob?b?
          = ?rs.getBlob( " photo_image " );
          ???
          long ?size? = ?b.length();
          ???
          // out.print(size);
          ??? byte []?bs? = ?b.getBytes( 1 ,?( int )size);
          ???response.setContentType(
          " image/jpeg " );?
          ???OutputStream?outs?
          = ?response.getOutputStream();?
          ???outs.write(bs);
          ???outs.flush();
          ???rs.close();?
          ??}

          ??
          else ? {
          ???rs.close();
          ???response.sendRedirect(
          " ./images/error.gif " );
          ??}

          ?}

          ?
          finally {
          ??con.close();
          ?}

          %>


          <3>把如下文件放在show.jsp的同一目錄下.

          index.html文件如下:

          < HTML >
          < HEAD >
          < TITLE > ?圖像測試? </ TITLE >

          </ HEAD >

          < BODY >
          < TABLE >
          < TR >
          < TD > 圖像測試 </ TD >
          </ TR >
          < TR >
          < TD >< img? src ="show.jsp?photo_no=2" ></ TD >
          </ TR >
          </ TABLE >

          </ BODY >
          </ HTML >
          posted on 2006-06-10 23:55 Q系列類、方法、變量…… 閱讀(829) 評論(0)  編輯  收藏 所屬分類: 網(wǎng)上工具代碼
          主站蜘蛛池模板: 鄂伦春自治旗| 金沙县| 沧州市| 兴业县| 民乐县| 横峰县| 赤壁市| 中宁县| 赣榆县| 连云港市| 东宁县| 青阳县| 岗巴县| 金溪县| 泌阳县| 邢台县| 靖远县| 青龙| 广宁县| 黎城县| 奉化市| 弥渡县| 江门市| 丹巴县| 井研县| 浦北县| 武宣县| 平原县| 灵川县| 新兴县| 吴川市| 江安县| 文登市| 和顺县| 江门市| 阿图什市| 锡林浩特市| 泊头市| 南江县| 宝丰县| 黄浦区|