How to Extract row from table view using JSP Code
Hi Friends,
This is vinay here. Hope this mail finds u all in condition.
I am totally new to java world and trying to design a web page using NetBeans IDE 6.8.
My problem exist with retrival of row from a table on click of link.
Table Structure is given bellow:
User_ID First_Name Middle_Name Last_name Role Region Status Email
1 A B C D E F G Update Delete
On click of Update, entire row should get placed into the corresponding textboxes & dropdown.
I am unable to achieve the functionality. So please send me the code if anyone of you have. This is really urgent for me.
Thank in advance....
Regards
Vinay Kumar Rai
View Answers
May 22, 2010 at 12:03 PM
Hi Friend,
Try the following code:
1)user.jsp:
<%@ page import="java.sql.*" %>
<html>
<head>
<script language="javascript">
function editRecord(id){
window.open('
http://localhost:8080/examples/jsp/edituser.jsp?id='+id,'mywindow','width=500, height=350,toolbar=no,resizable=yes,menubar=yes');
}
function deleteRecord(id){
window.open('
http://localhost:8080/examples/jsp/deleteuser.jsp?id='+id,'mywindow','width=500, height=350,toolbar=no,resizable=yes,menubar=yes');
}
</script>
</head>
<body>
<br><br>
<table border="1">
<tr><th>FirstName</th><th>LastName</th><th>Address</th><th>Email</th></tr>
<%
Connection con = null;
String url = "jdbc:
mysql://localhost:3306/";;;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String userName ="root";
String password="root";
int sumcount=0;
Statement st;
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db,userName,password);
String query = "select * from register";
st = con.createStatement();
ResultSet rs = st.executeQuery(query);
%>
<%
while(rs.next()){
%>
<tr><td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><input type="button" name="edit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=rs.getString(1)%>);" ></td>
<td><input type="button" name="delete" value="Delete" style="background-color:#ff0000;font-weight:bold;color:#ffffff;" onclick="deleteRecord(<%=rs.getString(1)%>);" ></td>
</tr>
<%
}
%>
<%
}
catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>
2)edituser.jsp:
<%@page language="java"%>
<%@page import="java.sql.*"%>
<form method="post" action="update.jsp">
<table border="1">
<tr><th>FirstName</th><th>LastName</th><th>Address</th><th>Email</th></tr>
<%
String id=request.getParameter("id");
int no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:
mysql://localhost:3306/test";, "root", "root");
String query = "select * from register where id='"+no+"'";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next()){
%>
<tr>
<td><input type="text" name="firstname" value="<%=rs.getString(2)%>"></td>
<td><input type="text" name="lastname" value="<%=rs.getString(3)%>"></td>
<td><input type="text" name="address" value="<%=rs.getString(4)%>"></td>
<td><input type="text" name="email" value="<%=rs.getString(5)%>"></td>
<td><input type="hidden" name="id" value="<%=rs.getString(1)%>"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Update" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td>
</tr>
<%
}
}
catch(Exception e){}
%>
</table>
</form>
</form>
May 22, 2010 at 12:05 PM
continue..
3)update.jsp:
<%@page import="java.sql.*"%>
<%
String id=request.getParameter("id");
int no=Integer.parseInt(id);
String firstname=request.getParameter("firstname");
String lastname=request.getParameter("lastname");
String address=request.getParameter("address");
String email=request.getParameter("email");
try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:
mysql://localhost:3306/test","root";, "root");
Statement st=null;
st=conn.createStatement();
st.executeUpdate("update register set firstname='"+firstname+"',lastname='"+lastname+"',address='"+address+"',email='"+email+"' where id='"+no+"'");
out.println("Data is updated successfully");
}
catch(Exception e){
out.println(e);
}
%>
4)deleteuser.jsp:
<%@page language="java"%>
<%@page import="java.sql.*"%>
<%
String id=request.getParameter("id");
int no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:
mysql://localhost:3306/test";, "root", "root");
String query = "select * from register where id='"+no+"'";
Statement st = conn.createStatement();
st.executeUpdate("DELETE FROM register WHERE id = '"+no+"'");
out.println("Record is deleted successfully");
}
catch(Exception e){}
%>
For the above code, we have used following database table:
CREATE TABLE `register` (
`id` bigint(20) NOT NULL auto_increment,
`firstname` varchar(40) default NULL,
`lastname` varchar(40) default NULL,
`address` varchar(100) default NULL,
`email` varchar(100) default NULL,
PRIMARY KEY (`id`)
)
Thanks
Related Tutorials/Questions & Answers:
Advertisements
delete row from a table using hibernatedelete
row from a
table using hibernate //
code in java file
String hql="delete
from CONTACT c where ID=6";
Query query=session.createQuery(hql);
//error when executing above
code
CONTACT is not mapped
Insert a row in 'Mysql' table using JSP CodeInsert a
row in 'Mysql'
table using JSP Code
In this section, we will discuss about
how to insert data in Mysql database
using JSP code.
Query...
between your Tomcat server & Mysql database
Table.
Code to insert
row in Mysql
Delete and add row from Table View iPhoneDelete and add
row from Table View iPhone
In this tutorial will learn
how to delete and also
how to add
row into the
table view iPhone, with the help of edit... and Delete buttons on
Table view.
Table views are commonly found in iPhone applications
how to display values from database into table using jsphow to display values
from database into
table using jsp I want to display values
from database into
table based on condition in query,
how... to display books based on either bookname or authorname, for this i created one
jsp page
Delete row and column from table through java code will see
how to delete
row and
column
from given
table through java
code. Java
code... Delete
row and column
from table through java
code... |
+----+----------+------------+---------+
In this
table we will delete the
row having minimum
value of ID then delete
How to extract details from XML? - JSP-ServletHow to
extract details
from XML? I want to
extract details
from http://service.openkapow.com/palanikumar/airportantigua.rest
How can i
extract details
from this link?Here is my program...
Airport
Deleting a Row from SQL Table Using EJB to delete a
row from the SQL
Table. Find out the steps given below that describes
how to delete a particular
row from the database
table using EJB. The steps... Deleting a
Row from SQL
Table Using EJB
Display Blob(Image) from Mysql table using JSP Display Blob(Image)
from Mysql
table using JSP
In this section, we will display blob data(image)
from Mysql database
table
using JSP code.
A Blob stores... Also :
Insert Blob(Image) in Mysql
table using JSP
Download Source CodeADS
How to get table row contents into next jsp pageHow to get
table row contents into next
jsp page Hi,
I have a 30...
code retrieve data
from database and display in the html
table. At each
row... page.But the problem is
how do i get the selected radio button
table row how to display a table from database using servlethow to display a
table from database
using servlet
how to display a
table with values
from servletpage
Hi Friend,
Please go through the following link:ADS_TO_REPLACE_1
http://roseindia.net/
jsp/servlet-
jsp-data
Deleting a Row from SQL Table Using EJB are
going to delete a
row from the SQL
Table. Find out the steps given below that describes
how to delete a particular
row from the database
table using EJB...
Deleting a
Row from SQL
Table Using EJB
how to create database and table using jsp table using jsp code... the
table name should be the name of the person...
how to create database and
table using jsp hi frnds....,
i want to create database and
table in mysql
using jsp.... i have an registration form(name
JSP Delete Record From Table Using MySQLJSP Delete Record
From Table Using MySQL
This tutorial explains you that
how to write a
JSP for deleting a record
from
database
table. In this section you....
In this tutorial you will learn that
how to delete a record of a database
table in
JSP Delete a Specific Row from a Database Table
Delete a Specific
Row from a Database
Table
..., and
in this section we are going to do the same that is,
how to delete a specific
row from a specific database
table. Here is an example with
code
which provides