public class conn
{
public OleDbConnection con
public OleDbCommand command
public OleDbDataReader dr
public OleDbDataAdapter ada
public conn()
{
try
{
con = new OleDbConnection("provider=microsoft.jet.oledb.4.0data source="+System.Web.HttpContext.Current.Server.MapPath("你的数据库地址"))
con.Open()
}
catch (InvalidOperationException one)
{
System.Web.HttpContext.Current.Response.Write(one.Message)
return
}
catch (OleDbException two)
{
System.Web.HttpContext.Current.Response.Write(two.Message)
return
}
}
public void close()
{
con.Close()
}
public void cmd(string str,int m)
{
command = new OleDbCommand(str,con)
try
{
if(m==1)
{
command.ExecuteNonQuery()
}
else
{
dr = command.ExecuteReader()
}
}
catch(InvalidOperationException one)
{
System.Web.HttpContext.Current.Response.Write(one.Message)
return
}
}
public void da(string str)
{
ada = new OleDbDataAdapter(str,con)
}
}
///////////////////////////以下是 *** 作代码/////////////
conn con = new conn()打开数据库
con.cmd("select 数据库存放图片路径的字段 from 表 where 条件")
if(con.dr.Read())
Response.Write("<img src=\""+con.dr[0].ToString()+"\">")//输出图片
else
Response.Write("信息不存在")
con.close()//关闭数据库
给你提供个ACCESS版的VB代码,使用时调用这些过程即可:'使用ADODB.Stream来保存/读取图像文件到数据库
'引用Microsoft ActiveX Data Objects 2.5 Library及以上版本
'保存文件到数据库中
Sub SaveFile()
Dim Stm As New ADODB.Stream
Dim Cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCnn As String
strCnn = "Provider=Microsoft.Jet.OLEDB.4.0Persist Security Info=FalseData Source=" &_
App.Path &"\DB1.mdb"
Cnn.Open strCnn
'读取文件到内存(二进制模式)
With Stm
.Type = adTypeBinary
.Open
.LoadFromFile App.Path + "\Image1.bmp"
End With
With rs
.Open "SELECT * FROM TABLE1", Cnn, 1, 3
.AddNew
.Fields("IMAGE") = Stm.Read
.Update
End With
rs.Close
Stm.Close
Set rs = Nothing
Set Cnn = Nothing
Set Stm = Nothing
End Sub
'从数据库中读取图像文件
Sub ReadFile()
Dim Stm As New ADODB.Stream
Dim Cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCnn As String
strCnn = "Provider=Microsoft.Jet.OLEDB.4.0Persist Security Info=FalseData Source=" &_
App.Path &"\DB1.mdb"
Cnn.Open strCnn
rs.Open "SELECT IMAGE FROM TABLE1 WHERE ID = 18", Cnn, adOpenKeyset, adLockReadOnly
'保存到文件
With Stm
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write rs("IMAGE")
.SaveToFile App.Path + "\Image2.bmp"
End With
'显示图片
Picture1.Picture = LoadPicture(App.Path + "\Image2.bmp")
rs.Close
Stm.Close
Set rs = Nothing
Set Cnn = Nothing
Set Stm = Nothing
End Sub
我把你的代码稍微改造了下,我这边是可以显示图片的。代码如下:
数据库 *** 作部分:
package com.databaseimport java.io.InputStream
import java.sql.*
/**
* @作者 王建明
* @创建日期 13-10-7
* @创建时间 下午12:32
* @版本号 V 1.0
*/
public class DataBaseUtil {
public static InputStream getImageStreamFromDataBase() {
Connection conn = null
try {
Class.forName("com.mysql.jdbc.Driver")
conn =
DriverManager.getConnection("jdbc:mysql://localhost/quickstart", "root", "123456")
Statement stmt = conn.createStatement()
String sql = "select book_image from tbl_book where id=1 "
ResultSet rs = stmt.executeQuery(sql)
if (rs.next()) {
return rs.getBinaryStream("book_image")
}
} catch (Exception e) {
System.out.println("出现异常: " + e.getMessage())
} finally {
try {
if (conn != null)
conn.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
return null
}
}
servlet部分:
package com.servletimport com.database.DataBaseUtil
import javax.servlet.ServletException
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
/**
* @作者 王建明
* @创建日期 13-10-7
* @创建时间 下午12:18
* @版本号 V 1.0
*/
public class ShowImage extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response)
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream in = DataBaseUtil.getImageStreamFromDataBase()
OutputStream toClient = response.getOutputStream()
response.reset()
response.setContentType("image/jpg")//或gif
int len = 10*1024*1024
byte[] P_Buf = new byte[len]
int i
while((i = in.read(P_Buf)) != -1){
toClient.write(P_Buf, 0, i)
}
in.close()
toClient.flush()
toClient.close()
}
}
web.xml中的servlet配置:
<servlet><servlet-name>ShowImage</servlet-name>
<servlet-class>com.servlet.ShowImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowImage</servlet-name>
<url-pattern>/showImage</url-pattern>
</servlet-mapping>
页面中加载图片方式:
<img src="showImage" />希望对你有帮助O(∩_∩)O~
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)