import java.io.InputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ShowFileFromDatabase { public void streamResult() throws SQLException, ClassNotFoundException { Connection con=null; Statement smt=null; ResultSet rs=null; InputStream data=null; String filename=null; Float size=null; byte [] b=null; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:avi","sa","p@ssw0rd"); smt=con.createStatement(); rs=smt.executeQuery("select fname,data,size from fileData where fno =1"); while(rs.next()) { filename=(String)rs.getString(1); data=(InputStream)rs.getBinaryStream(2); b = rs.getBytes(2); size=(Float)rs.getFloat(3); } System.out.println(filename); System.out.println(b.toString()); System.out.println(data.toString()); System.out.println(size); } catch(SQLException e){ System.out.println(e.getMessage()); } catch(NullPointerException e){ System.out.println("No Record in the Table"); } finally { smt.close(); con.close(); } } public static void main(String args[]) throws SQLException, ClassNotFoundException { ShowFileFromDatabase s=new ShowFileFromDatabase(); s.streamResult(); } } <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> Insert title here

Select files to upload:
import java.util.*; import java.sql.*; import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class upLoad extends HttpServlet { private static final long serialVersionUID = 1L; @SuppressWarnings({ "unchecked", "deprecation" }) protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { // Parse multi part HTTP POST request. try { // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Set factory constraints //factory.setSizeThreshold(yourMaxMemorySize); //factory.setRepository(yourTempDirectory); // or //Create a factory for disk-based file items //DiskFileItemFactory factory = new DiskFileItemFactory( // yourMaxMemorySize, yourTempDirectory); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint //upload.setSizeMax(yourMaxRequestSize); // Parse the request List /* FileItem */ items = upload.parseRequest(request); // Process the uploaded items Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.isFormField()) { processFormField(item, response); } else { if(item.getSize()!=0) processUploadedFile(item, response); } } } catch (Exception e) { } } } private void processUploadedFile(FileItem item, HttpServletResponse response) throws IOException, ClassNotFoundException, SQLException { PrintWriter out=response.getWriter(); Connection con=null; PreparedStatement pstn=null; //Blob b=null; // b=(Blob)item.getInputStream(); // out.println(item.getString()); out.println("
"); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:login"); pstn=con.prepareStatement("insert into fileData (fname,data,size,fno) values (?,?,?,?)"); pstn.setString(1, item.getName()); pstn.setBinaryStream(2, (InputStream)item.getInputStream(),(int)item.getSize()); pstn.setFloat(3, item.getSize()); pstn.setInt(4, 1); pstn.execute(); } catch(SQLException e){ System.out.println(e.getMessage()); } finally { pstn.close(); con.close(); } } private void processFormField(FileItem item, HttpServletResponse response) throws IOException { } } import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.sql.*; public class Upload { /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { File f = new File("c:\\AB\\avi.txt"); FileInputStream fin = new FileInputStream(f); Connection con = null; PreparedStatement pstn=null; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:avi","sa","p@ssw0rd"); pstn = con.prepareStatement("insert into foo (text_data) values (?)"); pstn.setBinaryStream(1, (InputStream)fin,(int)f.length()); pstn.execute(); }catch(Exception e) { System.out.println(e); } finally { if(con != null) { try { pstn.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.sql.*; public class getData { /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { Connection con = null; ResultSet rs=null; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection( "jdbc:sqlserver://01HW183323/SQLEXPRESS:PortNo;databaseName=PortugalCaseManagement;integratedSecurity=false;"); Statement smt=con.createStatement(); rs=smt.executeQuery("select * from PortugalCaseManagament.dbo.foo"); while(rs.next()) { System.out.println("hello "+rs.getBinaryStream(1)); } }catch(Exception e) { System.out.println(e); } finally { if(con != null) { try { rs.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }