1

I am displaying data is a jsp page from "resultset.getString("column_name")" attribute which works fine for my columns.

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*"%>
 <%@ page import="java.sql.*"%>

 <%@ page import="java.util.*"%>
  <%@ page import="java.text.*"%>
 <%@ page import="javax.servlet.*"%>
 <%@ page import="javax.servlet.http.*"%>
 <%@ page import="javax.servlet.http.HttpSession"%>
 <%@ page language="java"%>
 <%@ page session="true"%>
 <%@ page import="java.sql.*"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.servlet.ServletOutputStream"%>

<style>


th, td {
  padding: 15px;
}
</style>
<%
    String id = request.getParameter("name");
    String driverName = "com.mysql.jdbc.Driver";
    String connectionUrl = "jdbc:mysql://localhost:3306/";
    String dbName = "employee";
    String userId = "root";
    String password = "root";

    try {
        Class.forName(driverName);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

   String iurl1=null;
    Blob employee_dp=null;
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
%>
<h2 align="center"><font><strong>Retrieve data from database in jsp</strong></font></h2>



    <%
        try {
            connection = DriverManager.getConnection(connectionUrl + dbName, userId, password);
            statement = connection.createStatement();
            String sql = "SELECT * FROM employee_details where ID='" + session.getAttribute("ID") + "'";

            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
    %>



    <table border="0" class="display" >

        <tbody>
            <tr>
                <td>ID : </td>
                <td><%=resultSet.getString("ID")%></td>
            </tr>
            <tr>
                <td>First Name : </td>
                <td><%=resultSet.getString("name")%></td>
            </tr>
            <tr>
                <td>Last Name : </td>
                <td><%=resultSet.getString("Last_Name")%></td>
            </tr>
            <tr>
                <td>D.O.B. : </td>
                <td><%=resultSet.getString("DOB")%></td>

            </tr>

            <tr>
                <td>image : </td>
                  <td><img alt="Smiley face" src="<%=resultSet.getString("employee_dp") %>" width="500" height="500"/></td>

            </tr>


               <tr>
                <td>Attach address proof </td>
                <td>  <input type="file" name="txtfile"> </td>

            </tr>

        </tbody>
    </table>





<%}}
catch (Exception e) {
out.println("DB problem"); 
return;
}
finally {
try {
resultSet.close();
statement.close();
connection.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
%>

Here is the code for my jsp displaying page. Driver code to this is a basic login page which redirects to my this jsp page. I changed the code a bit and tuned it to see if my image is available or not. I tried by just giving the direct destination under "src" tag , it worked but when i retrieve it through resultSet.getString(column_name) it shows bits of symbols. If i use .getBlob I get a empty image box

If i use .getString I get a lot of symbols

  • 1
    Try adding the problem statement, current progress, actual output and desired output to the question to make it understandable. – anees Apr 30 '20 at 17:14
  • try adding the code snippet to the question. – anees Apr 30 '20 at 17:14
  • @AneesIjaz ,thank you for responding,I have added my code and my issue with the two statements as well. – Pranav Sharma May 01 '20 at 11:20
  • check [this](https://stackoverflow.com/questions/11192020/display-blob-image-through-jsp/11192125#11192125) post to achieve above . – Swati May 01 '20 at 11:26
  • @Swati ,thank you maam it helped me a lot to gain knowledge, While i gained another aproach to do so . I did it by adding images in a folder in my project folder and then i named it same as primary key and used to retrieve it by .getString("column name") so i used the command This solved my purpose. – Pranav Sharma May 04 '20 at 12:08

0 Answers0