This application illustrates how to retrieve image and data from MySQL table using NetBeans IDE.
In this application we create a jsp page to display image. In this JSP page we are using tag to access the servlet, here we are uses servlet but as u wish u can access a jsp page also. To create the project in the NetBeans IDE select File > click on "Create Project" then a New Project window will be appear, select Web from Categories section and select Web Application from Projects section. Click Next.
Put Project Name and click Next.
Choose JavaServer Faces and click Finish.
To create the jsp file right click on Web Pages select New and click JSP as below:
Here is the Source Code of data.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
JSF Application
"dt1" value="#{tableBean.perInfoAll}"
var="item" bgcolor="#F1F1F1" border="10" cellpadding="5"
cellspacing="3" rows="4" width="50%">
"header">
"This is Table Data" />
"header">
"Emp Name" />
"#{item.name}"/>
"header">
"Image" />
"DisplayImage"/>
"footer">
"The End" />
|
To create the package right click on Source Package and select New > Java Package..
The following window will appear then put package name and click Finish.
To create the java class right click on newly created package and select New > Java Class
Put the Class Name and click Finish.
Source Code of DisplayImage.java
package roseindia;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DisplayImage extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String connectionURL = "jdbc:mysql://localhost:3306/test";
java.sql.Connection con=null;
Statement st1;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(connectionURL,"root","root");
st1=con.createStatement();
ResultSet rs1 = st1.executeQuery("select image from pictures where id = '2'");
String imgLen="";
while(rs1.next()){
imgLen = rs1.getString(1);
System.out.println(imgLen.length());
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs1.getBinaryStream(1);
int index=readImg.read(rb, 0, len);
System.out.println("index----------------"+index);
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
st1.close();
response.getOutputStream().close();
} catch (Exception e){
e.printStackTrace();
}
}
} |
Create Bean class to set and get the table column as below the source code of TableBean.java
package roseindia;
import java.sql.*;
import java.util.*;
import java.io.*;
public class TableBean {
Connection con ;
Statement ps;
ResultSet rs;
private List perInfoAll = new ArrayList();
public void setPerInfoAll(List perInfoAll) {
this.perInfoAll = perInfoAll;
}
public List getPerInfoAll() {
int i = 0;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/
test","root","root");
ps = con.createStatement();
rs = ps.executeQuery("select name from emp");
while(rs.next()){
perInfoAll.add(i,new tableData(rs.getString("name")));
i++;
}
} catch (Exception e) {
System.out.println("Error Data : " + e.getMessage());
}
return perInfoAll;
}
public class tableData {
private String name;
public tableData(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
System.out.println("Name: "+name);
return name;
}
}
} |
Servlet Mapping in web.xml
DisplayImage
roseindia.DisplayImage
DisplayImage
/DisplayImage
|
Source Code of faces-config.xml
'1.0' encoding='UTF-8'?>
"1.2"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://j
ava.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
tableBean
class>roseindia.TableBeanclass>
request
|
The Output shown on the Browser as below:
Download Complete Source Code
0 comments:
Post a Comment