1

Html code:

<form action="insert.jsp" name="form" method="post">
<tr><td colspan=2>Name:</td><td> <input type = "text" name = "pname"></td></tr>
<tr><td colspan=2>Photo:</td><td><input type="file" name="Photo" accept="image/jpeg"></td></tr>

insert.jsp:

<%@ page errorPage = "/pages/error.jsp" %>
<%@ page language="java" import="java.sql.*" %>
<%@ page language="java" import="java.io.*" %>


<% 
response.setContentType("text/html");

Connection con=null;
Statement stmt=null;
ResultSet rs=null;
PreparedStatement ps = null;

Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:xe";
con=DriverManager.getConnection(url,"system","password");

try{
    stmt=con.createStatement();
    ps = con.prepareStatement("insert into student values(?, ?)");
    ps.setString(2, request.getParameter("pname"));
    ps.setString(4, request.getParameter("Photo"));
    ps.executeUpdate();
}catch(Exception e){
    out.println("error:"+e.getMessage());
}

Questions:
How to check the data is being inserted or not and How to display image using jsp file?

Rahul
  • 11
  • 1
  • Pls response as quick as possible. Thank you – Rahul Apr 12 '15 at 19:41
  • Uh! You cannot retrieve the contents submitted by `` as a request parameter such as `HttpServletRequest#getParameter("Photo")`. You need to add a special attribute to `
    `, `enctype = "multipart/form-data"` to signal the browser that you are interested in submitting mutipart contents. Any request parameters using the `HttpServletRequest#getParameter("paramName")` method will then return `null`. You have to parse the request yourself using some known, well-documented libraries like Apache Commons FileUpload and its dependent library Apache Commons IO.
    – Tiny Apr 12 '15 at 20:34
  • If you are using Servlet 3.0 or above, you can grab the file contents (`javax.​servlet.​http.Part`) using the `HttpServletRequest#getPart("Photo")` method without using any additional dependencies. (You have also misplaced named parameters in `PreparedStatement`. Take care of them also). – Tiny Apr 12 '15 at 20:35
  • http://stackoverflow.com/a/2424824/1391249 – Tiny Apr 13 '15 at 08:22

0 Answers0