推荐:JSP初级教程之跟我学JSP(七)第七章“备注型”超长文本的操作——Clob类型数据的存取 存放 oracle 留言板的正文内容,用VARCHAR2()是不行的,VARCHAR2()(可变长度的字符串)只能存4000字节,也就是2000个汉字,这也太少了啊,查一下 数据库 类型的资料,发现有这么几个类型: LONG,2G
out.print("<script>");
out.print("alert(’操作成功!’);");
out.print("window.location.href=’upphoto.htm’;"); out.print("</script>");
}//if
}//try
catch(Exception e)
{out.print(e);}
%>
</body>
</html>
---------------------------------------------------------------------------- upphoto.jsp对图片进行存入数据库操作。注意需要将得到的文件的路径改变一下格式:c:\d\e.jpg改成c:\\d\e.jpg
------------------------------showphoto.jsp--------------------------------- <%@ page contentType="text/html;charset=gb2312"%> <html>
<head>
<title>显示图片</title>
</head>
<body>
<%
String id=request.getParameter("vid"); %>
<table>
<tr>
<td colspan="3"> <img border="1" src="http://ringz/photo?id=<%=id%>"></td> </tr>
</table>
</body>
</html>
--------------------------------------------------------------------------- showphoto.jsp的这句是关键:src="http://ringz/photo?id=<%=id%>",它说明调用了一个Servlet,这个Servlet的名字叫photo,而且需要给它传一个值(id)。下面看这个Servlet的代码: ---------------------------PhotoServlet.java------------------------------ package ringz.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.sql.*;
public class PhotoServlet extends HttpServlet//和javabean一样是“类”,所以类名同样要和文件名一致
{
private String CLASSFORNAME = "oracle.jdbc.driver.OracleDriver"; private String SERVANDDB = "jdbc:oracle:thin:name/password@ringz:1521:rock"; Connection con = null;
PreparedStatement psmt = null;
ResultSet rs = null;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
int id = Integer.parseInt(request.getParameter("id")); try
{
Class.forName(CLASSFORNAME); con=DriverManager.getConnection(SERVANDDB); con.setAutoCommit(false);
String sql = "select * from clb where id="+id;
psmt = con.prepareStatement(sql); rs = psmt.executeQuery();
if (rs.next())
{
Blob bb = rs.getBlob("blob");
InputStream instream = bb.getBinaryStream();
response.setContentType("image/*"); OutputStream outStream = response.getOutputStream(); byte[] bytes = new byte[1024];
int i=0;
while ( (i = instream.read(bytes)) != -1)
{
outStream.write(bytes, 0, i);
}
instream.close();
outStream.close();
outStream = null;
con.commit();
rs.close();
psmt.close();
con.close();
}//if
}//try
catch (Exception ex)
{}
}//doGet
}
分享:JSP初级教程之跟我学JSP(六)第六章 jsp 实现画柱状统计图 这一节的内容是用jsp生成一个统计——统计一年内每个月完成的报修任务量。 Java里和画图有关的是java.awt包,由于我构想的图只是由矩形组成,那么用到的方法也就这么几个:fillRect,drawRect,setColor,setFont,drawString。