图片的数据库存储
---摘自互联网import java.io.*;
import java.sql.*;
public class InsertBlob {
public static void main(String args[]) { String username;
String password;
String url;
String dropString;
String createString; username = "system"; password = "manager"; // The URL that will connect to TECFA's MySQL server
// Syntax: jdbc:TYPEachineort/DB_NAME
url = "jdbcracle:thin:@cs05:1521s05";
// ---- configure END // INSTALL/load the Driver (Vendor specific Code)
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
} try {
Connection con;
Statement stmt; // Establish Connection to the database at URL with usename and password
con = DriverManager.getConnection(url, username, password);
System.out.println ("Ok, connection to the DB worked. Let's see if we can insert something:"); // con.setAutoCommit(false); FileInputStream fis=null;
File file = new File("z.jpg");
try{
fis = new FileInputStream(file);
} catch(FileNotFoundException e) {}
PreparedStatement ps = con.prepareStatement("insert into binary_data values (?,?,?,?,?,?)");
ps.setInt(1,1);
ps.setString(2,"樱花");
ps.setBinaryStream(3,fis,3098);
ps.setString(4,"z.jpg");
ps.setString(5,"3098");
ps.setString(6,"jpg");
ps.executeUpdate();
ps.close();
try{
fis.close();
} catch(IOException e) {} System.out.println ("Image Items have been inserted, you can now run the QueryCoffees program");
// Close resources
// stmt.close();
con.close();
}
// print out decent erreur messages
catch(SQLException ex) {
System.err.println("==> SQLException: ");
while (ex != null) {
System.out.println("Message: " + ex.getMessage ());
System.out.println("SQLState: " + ex.getSQLState ());
System.out.println("ErrorCode: " + ex.getErrorCode ());
ex = ex.getNextException();
System.out.println("");
}
} }
}
---摘自互联网import java.io.*;
import java.sql.*;
public class InsertBlob {
public static void main(String args[]) { String username;
String password;
String url;
String dropString;
String createString; username = "system"; password = "manager"; // The URL that will connect to TECFA's MySQL server
// Syntax: jdbc:TYPEachineort/DB_NAME
url = "jdbcracle:thin:@cs05:1521s05";
// ---- configure END // INSTALL/load the Driver (Vendor specific Code)
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
} try {
Connection con;
Statement stmt; // Establish Connection to the database at URL with usename and password
con = DriverManager.getConnection(url, username, password);
System.out.println ("Ok, connection to the DB worked. Let's see if we can insert something:"); // con.setAutoCommit(false); FileInputStream fis=null;
File file = new File("z.jpg");
try{
fis = new FileInputStream(file);
} catch(FileNotFoundException e) {}
PreparedStatement ps = con.prepareStatement("insert into binary_data values (?,?,?,?,?,?)");
ps.setInt(1,1);
ps.setString(2,"樱花");
ps.setBinaryStream(3,fis,3098);
ps.setString(4,"z.jpg");
ps.setString(5,"3098");
ps.setString(6,"jpg");
ps.executeUpdate();
ps.close();
try{
fis.close();
} catch(IOException e) {} System.out.println ("Image Items have been inserted, you can now run the QueryCoffees program");
// Close resources
// stmt.close();
con.close();
}
// print out decent erreur messages
catch(SQLException ex) {
System.err.println("==> SQLException: ");
while (ex != null) {
System.out.println("Message: " + ex.getMessage ());
System.out.println("SQLState: " + ex.getSQLState ());
System.out.println("ErrorCode: " + ex.getErrorCode ());
ex = ex.getNextException();
System.out.println("");
}
} }
}
(文/邵望)
大体思路
1)创建ServletOutputStream对象out,用于以字节流的方式输出图像
2)查询数据库,用getBinaryStream方法返回InputStream对象in
3)创建byte数组用作缓冲,将in读入buf[],再由out输出 注:下面的例程中数据库连接用了ConnectionPool,以及参数的获得进行了预处理 package net.seasky.music; import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import net.seasky.util.*;
import net.seasky.database.DbConnectionManager; public class CoverServlet extends HttpServlet {
private static final String CONTENT_TYPE = "image/gif";
public void init(ServletConfig config) throws ServletException {
super.init(config);
} public void doGet(HttpServletRequest request, HttpServletResponse response
) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
int albumID;
ServletOutputStream out = response.getOutputStream();
try {
albumID = ParamManager.getIntParameter(request,"albumID",0);
}
catch (Exception e) {
response.sendRedirect("../ErroePage.jsp");
return;
}
try {
InputStream in=this.getCover(albumID);
int len;
byte buf[]=new byte[1024];
while ((len=in.read(buf,0,1024))!=-1) {
out.write(buf,0,len);
}
}
catch (IOException ioe) {
ioe.printStackTrace() ;
}
} private InputStream getCover(int albumID) {
InputStream in=null;
Connection cn = null;
PreparedStatement pst = null;
try {
cn=DbConnectionManager.getConnection();
cn.setCatalog("music");
pst=cn.prepareStatement("SELECT img FROM cover where ID =?");
pst.setInt(1,albumID);
ResultSet rs=pst.executeQuery();
rs.next() ;
in=rs.getBinaryStream("img");
}
catch (SQLException sqle) {
System.err.println("Error in CoverServlet : getCover()-" + sqle);
sqle.printStackTrace() ;
}
finally {
try {
pst.close() ;
cn.close() ;
}
catch (Exception e) {
e.printStackTrace();
}
}
return in;
} public void destroy() {
}
}