写入:
out = response.getWriter();
Connection conn = DriverManager.getConnection("", "", "");
//INSERT A FILE INTO THE DATABASE!
File file = new File("D:\\d.bmp");
FileInputStream fis = new FileInputStream(file);
String strsql ="INSERT INTO tp(tp,dm) VALUES (?,'01')";
PreparedStatement ps = conn.prepareStatement(strsql);
ps.setBinaryStream(1, fis, (int)(file.length()));
ps.executeUpdate();
out.println("INSERT FILE OK:::::");
ps.close();
fis.close();
读出:
PreparedStatement pres = conn.prepareStatement("SELECT tp FROM tp WHERE dm='01'");
ResultSet res = pres.executeQuery();
if (res != null)
{
while(res.next())
{
InputStream is = res.getBinaryStream(1);
// use the stream in some way here
OutputStream outstr = new BufferedOutputStream( new FileOutputStream("d:\\new.bmp"));
// Read the URL and write it to a file
byte bufferb[] = new byte[64];
// Buffer to store lines
int nread;
while(0<=(nread = is.read(bufferb)))
outstr.write(bufferb, 0, nread);
is.close();
outstr.close();
}
res.close();
}
pres.close();