Well, sadly I do not even know how to describe this error lol :(
I want to display an image which I have stored it's path in the database. And my code works perfectly for the first time it accesses into the database. However, my code retrieves null for the second time it goes to the database and so on. I stared at my code for couple hours but couldnt find the bug lol :(
getimage.jsp
<form action="getimage" method="get">
<input type="text">
<input type="submit" name="getimage">
</form>
<c:forEach var="image" items="${image_path}">
<img src="${image}"/>
</c:forEach>
servlet:
HttpSession session = request.getSession();
session.removeAttribute("image_path");
List<String> list_image_path = new ArrayList<String>();
ImagePath img = ImageDao.getInstance().getPath(8);
list_image_path.add(img.getImagePath());
session.setAttribute("image_path", list_image_path);
System.out.println(img);
request.getRequestDispatcher("getimage.jsp").forward(request, response);
ImageDao.java
private ImageDao() {
try {
conn = MyDatabase.getConnection();
} catch (SQLException e) {
System.out.println( e.getClass() + " - Can not connect to the databse");
e.printStackTrace();
}
}
public static ImageDao getInstance() {
if(instance == null) {
synchronized( UsersDao.class) {
if( instance == null ){
instance = new ImageDao();
}
}
}
return instance;
}
public static ImagePath getPath( long id) {
ImagePath path = new ImagePath();
String sql = "SELECT * FROM item_image WHERE id=?";
try {
System.out.println(id + ": " + conn);
PreparedStatement pstmt = conn.prepareStatement( sql );
pstmt.setLong(1, id);
ResultSet rs = pstmt.executeQuery(); // Fail right here
if( rs.next() ) {
System.out.println( rs.getLong("id") + ": " + rs.getString("path") + " " + id);
path.setId( rs.getLong( "id" ));
path.setImagePath( rs.getString("path"));
}
pstmt.close();
rs.close();
conn.close();
return path;
} catch( SQLException e ) {
System.out.println("Cant get it");
} finally {
if( conn != null ) { try { conn.close(); } catch( SQLException e) {}};
}
return path;
}
So this is the message display on the console which I embedded inside ImageDao.java : first access:
8: org.postgresql.jdbc4.Jdbc4Connection@3d3cdc3b
8: upload/download.jpg 8
Id: 8 path: upload/download.jpg
second access:
8: org.postgresql.jdbc4.Jdbc4Connection@3d3cdc3b
Cant get it
Id: 0 path: null