Naga
JSP
3 Answer(s)      5 years and 2 months ago
Posted in : JSP-Servlet
im having the problem related to the jsp

data base is:

create table brand (BrandName varchar(25),Description varchar(100),Image Blob);




i displayed the data from my sql to the jsp page

this is the code


<%@ page language="java" import="java.sql.*,java.io.*"%>
<html>
<head><title>Manage Brands</title>

</head>
<body background="./images/background_fullwidth.jpg">

<table border=0>
<tr><td><img src="./images/Untitled-30.jpg" width="990" height="170"></td>

</table >
<img src="./images/5.jpg" width="990" height="5"><br>
<table align="center" border=0>
<tr><td><a href="admin_home.html" ><b>Admin Home</b></a></td>
<td><a href="man_brands.jsp" ><b>Manage Brands</b></a></td>
<td><a href="man_cat.jsp" ><b>Manage Categories</b></a></td>
<td><a href="man_users.jsp"><b>Manage Users</b></a></td>
<td><a href="man_prod.jsp"><b>Manage Products</b></a></td>
<td><a href="man_orders.jsp" ><b>Manage Orders</b></a></td>
<td><a href="admin_logout.html" ><b>Log Out</b></a></td></tr>
</table >
<img src="./images/5.jpg" width="990" height="5"><br>

<table width=990>
<tr bgcolor="darkblue"><td align="center"><font color="white"><b>BRAND LIST</b></font></td></tr>
</table>
<a href="new_prod.html">Add New Brand</a>
<table border="1" borderColor="#ffe9bf" cellPadding="0" cellSpacing="0" width="990" height="63">
<tbody>
<td bgColor="#008080" width="47" align="center" height="19"><font color="#ffffff"><b>Sr.
No.</b></font></td>
<td align="center" bgColor="#008080" width="107" height="19"><font color="#ffffff"><b>Brand Name</b></font></td>
<td align="center" bgColor="#008080" width="224" height="19"><font color="#ffffff"><b>Image</b></font></td>
<td align="center" bgColor="#008080" width="270" height="19"><font color="#ffffff"><b>Description</b></font></td>
<td align="center" bgColor="#008080" width="270" height="19"><font color="#ffffff"><b>Delete</b></font></td>

<%
//String DRIVER = "com.mysql.jdbc.Driver";
Class.forName("com.mysql.jdbc.Driver").newInstance();


Connection con=null;
ResultSet rst=null;
Statement stmt=null;
PrintWriter pw=response.getWriter();

try{
int i=1;
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/shopping","root","password");
stmt=con.createStatement();
rst=stmt.executeQuery("select * from brand");
while(rst.next()){

if (i==(i/2)*2){
%>
<tr>
<td bgColor="#ffff98" vAlign="top" width="47" align="center" height="19"><br><br><%=i%>.</td>
<td name="bname" align="center" bgColor="#ffff98" vAlign="top" width="107" height="19"><br><br><b><%=rst.getString(1)%></b></td>
<td align="center" bgColor="#ffff98" vAlign="top" width="224" height="19">
<img id=<%=i%> src="./images/<%=rst.getString(3)%>" width="100" height="100">&nbsp;</td>
<td align="center" bgColor="#ffff98" vAlign="top" width="270" height="19"><br><br><%=rst.getString(2)%></td>
<td align="center" bgColor="#ffff98" vAlign="top" width="270" height="19"><br><br> <a href="./DbrandServlet" style="color: #ffff98"><img src="./images/PNG-Symbol-Delete.png" width="25" height="25" ></a></td>
</tr>
<%
}else{
%>
<tr>
<td bgColor="#ffcc68" vAlign="top" width="47" align="center" height="19"><br><br><%=i%>.</td>
<td align="center" bgColor="#ffcc68" vAlign="top" width="107" height="19"> <br><br><b> <%=rst.getString(1)%></B></td>
<td align="center" bgColor="#ffcc68" vAlign="top" width="224" height="19">
<img id=<%=i%> src="./images/<%=rst.getString(3)%>" width="100" height="100"></a>&nbsp;</td>
<td align="center" bgColor="#ffcc68" vAlign="top" width="270" height="19"><br><br><%=rst.getString(2)%></td>
<td align="center" bgColor="#ffcc68" vAlign="top" width="270" height="19"><br><br><a href="./DbrandServlet" style="color: #ffcc68"> <img src="./images/PNG-Symbol-Delete.png" width="25" height="25" ></a></td>
</tr>
<% }
i++;
}
rst.close();
stmt.close();
con.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
%>
</tbody>
</table>
</center>
</div>


</body>
</html>



and the out put is like


BRand name image description delete
raymonde some image dfjdfdfklf here is the delete pic


now what i want is when delete image is clicked then whole row must be deleted form the database and the remaining data will be showed in the same jsp page



can any one help me plzzzzzzzzzzzzz.





Advertisement
View Answers

May 31, 2010 at 1:01 PM


Hi Friend,

Try the following code:

1)brand.jsp:

<%@ page language="java" import="java.sql.*,java.io.*"%>
<html>
<head><title>Manage Brands</title>
<script>
function call(id){
alert(id);
window.location.replace("../DbrandServlet?id="+id);

}
</script>
<body background="./images/background_fullwidth.jpg">
<table border=0>
<tr><td><img src="./images/Untitled-30.jpg" width="990" height="170"></td>
</table >
<img src="./images/5.jpg" width="990" height="5"><br>
<table align="center" border=0>
<tr><td><a href="admin_home.html" ><b>Admin Home</b></a></td>
<td><a href="man_brands.jsp" ><b>Manage Brands</b></a></td>
<td><a href="man_cat.jsp" ><b>Manage Categories</b></a></td>
<td><a href="man_users.jsp"><b>Manage Users</b></a></td>
<td><a href="man_prod.jsp"><b>Manage Products</b></a></td>
<td><a href="man_orders.jsp" ><b>Manage Orders</b></a></td>
<td><a href="admin_logout.html" ><b>Log Out</b></a></td></tr>
</table >
<img src="./images/5.jpg" width="990" height="5"><br>

<table width=990>
<tr bgcolor="darkblue"><td align="center"><font color="white"><b>BRAND LIST</b></font></td></tr>
</table>
<a href="new_prod.html">Add New Brand</a>
<table border="1" borderColor="#ffe9bf" cellPadding="0" cellSpacing="0" width="990" height="63">
<tbody>
<td bgColor="#008080" width="47" align="center" height="19"><font color="#ffffff"><b>Sr.
No.</b></font></td>
<td align="center" bgColor="#008080" width="107" height="19"><font color="#ffffff"><b>Brand Name</b></font></td>
<td align="center" bgColor="#008080" width="224" height="19"><font color="#ffffff"><b>Image</b></font></td>
<td align="center" bgColor="#008080" width="270" height="19"><font color="#ffffff"><b>Description</b></font></td>
<td align="center" bgColor="#008080" width="270" height="19"><font color="#ffffff"><b>Delete</b></font></td>

<%
Class.forName("com.mysql.jdbc.Driver").newInstance();


Connection con=null;
ResultSet rst=null;
Statement stmt=null;
PrintWriter pw=response.getWriter();

try{
int i=1;
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/register","root","root";);
stmt=con.createStatement();
rst=stmt.executeQuery("select * from brand");
while(rst.next()){

if (i==(i/2)*2){
%>
<tr>
<td bgColor="#ffff98" vAlign="top" width="47" align="center" height="19"><br><br><%=i%>.</td>
<td name="bname" align="center" bgColor="#ffff98" vAlign="top" width="107" height="19"><br><br><b><%=rst.getString("brandName")%></b></td>
<td align="center" bgColor="#ffff98" vAlign="top" width="224" height="19">
<img id=<%=i%> src="image.jsp?id=<%=rst.getInt(1)%>" width="100" height="100">&nbsp;</td>
<td align="center" bgColor="#ffff98" vAlign="top" width="270" height="19"><br><br><%=rst.getString("description")%></td>
<td align="center" bgColor="#ffff98" vAlign="top" width="270" height="19"><br><br> <img src="./images/PNG-Symbol-Delete.png" width="25" height="25" onclick='call(<%=rst.getString(1)%>);' ></td>
</tr>


May 31, 2010 at 1:03 PM


continue..

<%
}else{
%>
<tr>
<td bgColor="#ffcc68" vAlign="top" width="47" align="center" height="19"><br><br><%=i%>.</td>
<td align="center" bgColor="#ffcc68" vAlign="top" width="107" height="19"> <br><br><b> <%=rst.getString("brandName")%></B></td>
<td align="center" bgColor="#ffcc68" vAlign="top" width="224" height="19">
<img id=<%=i%> src="image.jsp?id=<%=rst.getInt(1)%>" width="100" height="100"></a>&nbsp;</td>
<td align="center" bgColor="#ffcc68" vAlign="top" width="270" height="19"><br><br><%=rst.getString("description")%></td>
<td align="center" bgColor="#ffcc68" vAlign="top" width="270" height="19"><br><br><img src="./images/PNG-Symbol-Delete.png" onclick='call(<%=rst.getString(1)%>);' width="25" height="25" ></td>
</tr>
<% }
i++;
}
rst.close();
stmt.close();
con.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
%>
</tbody>
</table>
</center>
</div>
</body>
</html>

2)image.jsp:

<%@ page import="java.sql.*,java.io.*,java.util.*" %>
<%

String connectionURL = "jdbc:mysql://localhost:3306/register";;
if(request.getParameter("id")!=null && request.getParameter("id")!="")
{
int id = Integer.parseInt(request.getParameter("id"));

Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(connectionURL,"root","root");
Statement st1=con.createStatement();
String strQuery = "select image from brand where id="+id;

ResultSet rs1 = st1.executeQuery(strQuery);

String imgLen="";
if(rs1.next()){
imgLen = rs1.getString(1);
}
rs1 = st1.executeQuery(strQuery);
if(rs1.next()){
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs1.getBinaryStream(1);
int index=readImg.read(rb, 0, len);
st1.close();
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
}
catch (Exception e){
e.printStackTrace();
}
}
%>

3)DbrandServlet.java

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DbrandServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id=request.getParameter("id");
PrintWriter out = response.getWriter();
String connectionURL = "jdbc:mysql://localhost:3306/register";;
Connection connection=null;
ResultSet rs;

try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "root");
Statement s = connection.createStatement();
s.executeUpdate("DELETE FROM brand WHERE id = '"+id+"'");
}
catch(Exception e){
System.out.println("Exception is ;"+e);
}
response.sendRedirect("car/brand.jsp");
}
}

In database table, we have created following table:
CREATE TABLE `brand` (
`id` bigint(255) NOT NULL auto_increment,
`brandName` varchar(255) default NULL,
`description` varchar(255) default NULL,
`image` longblob,
PRIMARY KEY (`id`)
)

Thanks


June 5, 2010 at 10:02 PM





thanks for ur post it is working well

and i need one more thing that by clicking the edit pic we need to access the data display the data in the text boxes of html page then modify the data then when i click the edit then it should be update in the data base









Related Tutorials/Questions & Answers:
jsp
jsp  how to include two jsp page
Jsp
Jsp  Can I implement interface in jsp
Advertisements
jsp
jsp  how to create jsp page
JSP
JSP  How to run jsp program in eclipse
jsp
jsp  how jsp translated into servlets
JSP
what is JSP forward tag for  what is JSP forward tag for   It forwards the current request to another JSP page. Below is the syntax for the same:- <jsp:forward page="...url..." /> We can also forward parameter
jsp
jsp  write a code for jsp file generator to generate a jsp page automatically taking html page as input
jsp
jsp  how to create a table in oracle using jsp and the table name is entered in text feild of jsp page
JSP
JSP  Hi, What is JSP? What is the use of JSP? Thanks   Hi, JSP Stands for Java Server Pages. It is Java technology for developing web applications. JSP is very easy to learn and it allows the developers to use Java
jsp
jsp   what is the basic necessities reguired for running jsp using JDBC bridge?? and how to write a jsp code to access the detail present in microsoft access pls send link regarding above
jsp
jsp  how to connect the database with jsp using mysql   Hi Friend, Please visit the following links: http://www.roseindia.net/jsp/connectjspwith_mysql.shtml http://www.roseindia.net/jsp/Accessingdatabase-fromJSP.shtml
jsp
jsp  how to upload image through csv format in jsp
jsp
jsp  I Want complete note on JSP servlet
jsp
jsp  how to delete data from database using jsp
JSP
JSP  FILE UPLOAD-DOWNLOAD code USING JSP
jsp
jsp  how to write hindi in jsp and store in database as unicode
jsp
jsp  code for jsp automatic genration using html
jsp
jsp  why jsp is translated into servlet?pls describe precisly
JSP
JSP  relevant difference between servlet and jsp in 1 line
jsp
jsp  how to pass jsp variable to an html page
jsp
jsp  how to write a servlet and jsp program for user login form
jsp
jsp  how to write a servlet and jsp program for user login form
JSP
JSP   How to create CAPTCHA creation by using JSP? I need an code for CAPTCHA creation by using JSP...   You can create a captcha validation using jsp. Have a look at the following tutorial: Captcha Creation
jsp
jsp  how to include google map in our jsp page
JSP
What are JSP directives, page directives and include directives?  what are jsp directives? and what are page directives? and what are include directives? can you explain taglib directives?   JSP directives JSP
JSP
Can you explain jsp page life cycle what is el how does el search for an attribute in JSP  Can you explain jsp page life cycle what is el how does el...://www.roseindia.net/jsp/simple-jsp-example/LifeCycleOfJspPage.shtml http
JSP
JSP  what are the different scopes an object can have in a jsp page   JSP Scope Variables: Page scope-It makes the data available... the following links: http://www.roseindia.net/jsp/simple-jsp-example
JSP
what is jstl in JSP   what is jstl   JSTL is also called as JSP tag libraries. They are collection of custom actions which canbe accessed as JSP tags. For more information, visit the following link: JSTL Tutorials
JSP
access application data stored in JavaBeans components. The jsp expression...). Before JSP 2.0, we could use only a scriptlet, JSP expression, or a custom tag to include server state in the jsp page output. For more information, visit
JSP
JSP public opinion poll   Develop a JSP for a news channel to conduct public opinion poll on any topic. Users will respond through yes or no options. Later display the result in percentage
jsp
jsp  sir i want to jsp code for online examination like as bank po,,,,,,plz help me sir