I want to store and retrieve the whole data as shown below in mysql db. I've put in my all efforts for building this jsp but I'm not able to understand where's the exact flaw in my code. Can anyone guide me through all this and help me out as it is really important for my project????? THIS IS MY DB Table. mysql> use test; Database changed mysql> desc student; +-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | rollno | int(11) | YES | | NULL | | | name | varchar(30) | YES | | NULL | | | age | int(11) | YES | | NULL | | | file_data | longblob | YES | | NULL | | +-----------+-------------+------+-----+---------+-------+ 4 rows in set (0.08 sec)
index.jsp
1.jsp
<%@ page import="java.io.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.util.zip.*"%> <% String rollno1=request.getParameter("rollno"); String name1=request.getParameter("name"); String age1=request.getParameter("age"); out.println(rollno1); out.println(name1); out.println(age1); %> <% //to get the content type information from JSP Request Header String contentType = request.getContentType(); //here we are checking the content type is not equal to Null and //as well as the passed data from mulitpart/form-data is greater than or //equal to 0 if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) { DataInputStream in = new DataInputStream(request.getInputStream()); //we are taking the length of Content type data int formDataLength = request.getContentLength(); String rollno2=new String(rollno1); String name2=new String(name1); String age2=new String(age1); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; //this loop converting the uploaded file into byte code while (totalBytesRead < formDataLength) { byteRead = in.read(dataBytes, totalBytesRead, formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); //for saving the file name String saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\") + 1,saveFile.indexOf("\"")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1, contentType.length()); int pos; //extracting the index of file pos = file.indexOf("filename=\""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)) .getBytes()).length; // creating a new file with the same name and writing the content in new file FileOutputStream fileOut = new FileOutputStream(saveFile); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); %><Br>
Roll no: | |
Name |
You have successfully upload the file by the name of: <% out.println(saveFile); %> |
Connection connection = null; //String connectionURL = "jdbc:mysql://localhost:8080/test"; ResultSet rs = null; PreparedStatement pst = null; FileInputStream fis;
try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); //connection = DriverManager.getConnection(connectionURL, "root", "root"); Connection con = DriverManager.getConnection("jdbc:mysql:///test","root","123"); File f = new File(saveFile);
String query="insert into student values(?,?,?,?)"; pst= con.prepareStatement(query); //psmnt = connection.prepareStatement("insert into file(file_data) values(?)");
fis = new FileInputStream(f); pst.setString(1,rollno2); pst.setString(2,name2); pst.setString(3,age2); pst.setBinaryStream(4, (InputStream)fis, (int)(f.length())); int s = pst.executeUpdate(); if(s>0){ out.println("Uploaded successfully !"); } else{ out.println("Error!"); } } catch(Exception e){e.printStackTrace();} } %>
retrive.jsp <%@ page import="java.sql.*" %> <%@ page import="java.io.*" %>
<% // declare a connection by using Connection interface Connection connection = null; /* Create string of connection url within specified format with machine name, port number and database name. Here machine name id localhost and database name is mahendra. */ //String connectionURL = "jdbc:mysql://localhost:3306/mahendra"; /*declare a resultSet that works as a table resulted by execute a specified sql query. */ ResultSet rs = null; // Declare statement. PreparedStatement psmnt = null; Connection connection=null; // declare InputStream object to store binary stream of given image. InputStream sImage; try { HttpSession h1=request.getSession(); String rollno=(String)h1.getAttribute("rollno"); // Load JDBC driver "com.mysql.jdbc.Driver" Class.forName("com.mysql.jdbc.Driver").newInstance(); /* Create a connection by using getConnection() method that takes parameters of string type connection url, user name and password to connect to database. */ connection = DriverManager.getConnection("jdbc:mysql:///test","root","123"); /* prepareStatement() is used for create statement object that is used for sending sql statements to the specified database. */ psmnt = connection.prepareStatement("SELECT file_data FROM student WHERE rollno = ?"); psmnt.setString(1,rollno); // here integer number '11' is image id from the table rs = psmnt.executeQuery(); if(rs.next()) { byte[] bytearray = new byte[1048576]; int size=0; sImage = rs.getBinaryStream(1); response.reset(); response.setContentType("image/jpeg"); while((size=sImage.read(bytearray))!= -1 ){ response.getOutputStream().write(bytearray,0,size); } } } catch(Exception ex){ out.println("error :"+ex); } finally { // close all the connections. rs.close(); psmnt.close(); connection.close(); } %>
Ads