1.创建一个maven的web项目,加入oracle10驱动包依赖
依赖如下
<!-- Oracle驱动包 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.2.0</version>
</dependency>
2.创建jdbc获取数据库连接的工具类(未使用连接池,仅供测试)
public class OracleConnTool{
private static String url = "jdbc:oracle:thin:@数据库ip地址:1521:ORCL";
private static String user = "你的数据库用户名";
private static String pwd = "你的数据库密码";
public static Connection getConnection(){
Connection conn = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url,user,pwd);
if(null != conn){
System.out.println("数据库连接成功。con是:" + conn);
}else{
Systme.out.println("连接失败!");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
3.创建一个Servlet测试使用
仅贴出doGet方法了
@Override
protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
String sql = "select * from DATA_11";
Statement pstmt = null;
Connection conn = OracleConnTool.getConnection();
try{
pstmt = conn.createStatement();
ResultSet rs = pstmt.executeQuery(sql);
rs.next();//只读一条测试数据出来。
InputStream is = rs.getBinaryStream("XP");
System.out.println(is.toString());
resp.setContentType("image/*");
ServletOutputStream os = resp.getOutputStream();
byte[] b = new byte[1024];
int len;
while((len = is.read(b)) > 0){
os.write(b,0,len);
}
os.close();
is.close();
rs.close();
}catch (Exception e){
e.printStackTrace();
}finally{
try{
pstmt.close();
}catch(Excetion e){
e.printStackTrace();
}
}
}
4.jsp页面
<img src="你写的servlet名称">
5.没有意外的话就可以显示你存在Oracle数据库的Blob类型的图片了。