creating instance of table in jsp

creating instance of table in jsp

i face senario look kie as follows; 1)i write a code in jsp to retrieve the data from database. 2)the out put file is obviously a jsp page and shows the output in table manner. 3) now i want to make the instance of the data present in that paarticular table.... how i do that?

View Answers

June 1, 2012 at 12:09 PM

The given code retrieves the data from the database and display it into html table.

<%@page import="java.sql.*"%>
<table border=1>
<tr><th>Name</th><th>Age</th><th>Address</th><th>Phone No</th></tr>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:student","","");
String sql = "select * from data where id= ?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1, 1);
ResultSet rs = stmt.executeQuery();
if(rs.next()){
%>
<tr><td><%=rs.getString("name")%></td><td><%=rs.getInt("Age")%></td><td><%=rs.getString("address")%></td><td><%=rs.getInt("phoneNo")%></td></tr>
<%
}
rs.close();
stmt.close();
con.close();
}
catch(Exception e){
System.out.println(e);
}
%>
</table>

June 1, 2012 at 12:11 PM

The given code retrieve data from database and display in the html table. At each row, there is a button which consists of that particular id. When the user clicks the particular edit button, that data will get shown in another page and allow the user to update the record.

1)application.jsp:

<%@ page import="java.sql.*" %>
<html>
<head>
<script language="javascript">
function editRecord(id){
    var f=document.form;
    f.method="post";
    f.action='edit.jsp?id='+id;
    f.submit();
}
</script>
</head>
<body>

<br><br>
<form method="post" name="form">
<table border="1">
<tr><th>Name</th><th>Address</th><th>Contact No</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 employee";
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:green;font-weight:bold;color:white;" onclick="editRecord(<%=rs.getString(1)%>);" ></td>
</tr>
<%
}
%>
<%
}
catch(Exception e){
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>

2)edit.jsp:

<%@page language="java"%>
<%@page import="java.sql.*"%>
<form method="post" action="update.jsp">
<table border="1">
<tr><th>Name</th><th>Address</th><th>Contact No</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 employee where id='"+no+"'";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next()){
%>
<tr>
<td><input type="text" name="name" value="<%=rs.getString("name")%>"></td>
<td><input type="text" name="address" value="<%=rs.getString("address")%>"></td>
<td><input type="text" name="contact" value="<%=rs.getInt("contactNo")%>"></td>
<td><input type="text" name="email" value="<%=rs.getString("email")%>"></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>

June 1, 2012 at 12:12 PM

continue..

3)update.jsp:

<%@page import="java.sql.*"%>
<%
String ide=request.getParameter("id");
int num=Integer.parseInt(ide);
String name=request.getParameter("name");
String address=request.getParameter("address");
int contact=Integer.parseInt(request.getParameter("contact"));
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 employee set name='"+name+"',address='"+address+"',contactNo="+contact+",email='"+email+"' where id='"+num+"'");
response.sendRedirect("/examples/jsp/application.jsp");
}
catch(Exception e){
System.out.println(e);
}
%>









Related Tutorials/Questions & Answers:
creating instance of table in jsp
creating instance of table in jsp  i face senario look kie as follows... file is obviously a jsp page and shows the output in table manner. 3) now i want to make the instance of the data present in that paarticular table.... how i do
creating table in hibernate
creating table in hibernate  how to create table by the help of hibernate   There are 2 alternatives to create table in hibernate: 1...;ADS_TO_REPLACE_5 @Entity @Table(name = "TBLCOURSES") public class Course
Advertisements
java coding for creating table in the console
java coding for creating table in the console  write a java program to create table
Hibernate Creating criteria instance
Hibernate Creating criteria instance In this section you will learn about the creating of criteria instance in Hibernate. An instance of Criteria is created... at first a table from where you will required to retrieve a stored data
Creating a service - JSP-Servlet
Creating a service  I created a database for username and password nd verify them for that I created a loginJSP page, using servlet I am getting username and password and perform validation and display the result back in jsp
MYSQL - mysql copy table to another table example by creating new table
MYSQL - mysql copy table to another table example by creating new table  Hi, I have a table with many fields and hue data is in it. I want to create a new table with all the data from this table. How to create a new table
creating dropdown lists in jsp
creating dropdown lists in jsp  i want to create two dropdown list which are dependent that is the first box choice have to evaluate the second boxs options
creating a feedback form - JSP-Servlet
creating a feedback form  hi, first of all thank you very much... i am refining this problem as follows: i am creating a feedback form...) now i have a database as professor and a table in this database as question
a jsp code for creating a text file
a jsp code for creating a text file  Hello,i need jsp code for creating a new text file for each user, when they login in to the website for creating a new data file. So i need a jsp code for following options. when user login
Creating Hello World JSP Page
Creating Hello World JSP Page  Hi, I am trying to create Hello World example in JSP. How to create Hello World JSP page? Thanks   Hi, To create a JSP page first of create test.jsp on the tomcat web application
JSP Financial Year Table
JSP Financial Year Table  Im trying to design a financial year table...wherein the user enters the data in the table and the same is updated in backend.i have successfully desinged the page but i have certain doubts in jsp
Creating dynamic jsp file in java file to include another jsp file
Creating dynamic jsp file in java file to include another jsp file  I am able to create jsp file in java that includes ; table tr td and img tags..... Need to include jsp file in the same content. The below code doesn't help me
creating a jar file - JSP-Servlet
creating a jar file  Can you give me detail answer 1. i am having a servlet.class file in classes folder 2. web.xml file my questions are 1. where to place the html or jsp files 2. how to create a jar file and how can
Creating methods in servlets - JSP-Servlet
Creating methods in servlets  I created servlet and jsp file.I... --%> JSP Page... mistake and check it : 1.In the JSP page having a "Form" Tag You have
Dynamic table in jsp
to show all values of excel file on jsp page in a table format. All elements are displaying on jsp page but in vertical line. How can i change them in table...Dynamic table in jsp  Hi..... I have a java file to read excel file
Jsp table Pagination
Jsp table Pagination  I tried the code already.But I dont want it with SQL.I want it for oracle database because I'm using oracle.So please help me with this issue. Thanks
Creating a Local Variable in JSP
Creating a Local Variable in JSP      In jsp when we have to create a method or variable we usually declare.... This works like a instance variable.  Now consider a situation where we have
Creating a Hash Table
Creating a Hash Table : Java Util   ... table. What is the hash table and how to create that? Hash Table holds... for several values. Hash Table is created using an algorithm (hashing function
Probem while creating PDF through JSP-Servlet - JSP-Servlet
Probem while creating PDF through JSP-Servlet  Hi, I have a web-app... java app or the batch file through JSP or servlet the PDFCreator is called but I... link: http://www.roseindia.net/jsp/HowtoMakeaPdfandinsertingadata.shtml Hope
display of colors in a table - JSP-Servlet
display of colors in a table  Hi, If i have a table of 4 by 4 boxes, numbering from 1-16 in sequence, how do i make them display one column of one..., Try the following jsp code: table.jsp: Column1,Row1
Need Help in creating online quiz application using JSP - JSP-Servlet
Need Help in creating online quiz application using JSP  Hi, i am creating online Quiz application using JSP and MySQl ,Apache 6 in Netbeans IDE. i... ---------------------------------------------------------------------------------------------------- JSP Page
Creating Table in Echo3
Creating Table in Echo3     ... in the nextapp.echo.app package. There are four constructors provided for creating a table in Echo3.... In this example of creating Table in Echo3 we have created a simple application
creating columns and adding label and textfield in the columns in a jsp page
creating columns and adding label and textfield in the columns in a jsp page  I have create a jsp page with 3 columns.....each column has a title,3 label and corresponding 3 text fields....can u send me the code
how to take input table name in jsp to create table in mysql?
how to take input table name in jsp to create table in mysql?  how to take input table name in jsp to create table in mysql?   Hello Friend...; <form method="post"> Enter Table Name:<input type="text" name="table
Dynamic table data to Excel in JSP
Dynamic table data to Excel in JSP   Iam trying to export dynamic...() { document.form1.des.disabled=0; } function view_Table() { } function...;div align="center"><br> <table width="500" height="300
generating time table - JSP-Servlet
..the code should be in jsp/servlet.. so please help me friends
Java to create table in jsp file that include other jsp file
Java to create table in jsp file that include other jsp file  String jspContent = "table" += "tr" += "td" += "jsp:include page='fileSource'" //this line is not working properly... += "/td" += "/tr>" += "/table" Please refer
Uploading an image into the table - JSP-Servlet
Uploading an image into the table  how to upload an image into the table in java  Hi friend, Code to help in solving the problem..."; /*declare a resultSet that works as a table resulted by execute a specified
Creating a Local Variable in JSP
Creating a Local Variable in JSP          In jsp when we have to create a method... will be applicable in the whole page. This works like a instance variable.  Now
To save table format data in pdf/excel in jsp
To save table format data in pdf/excel in jsp  Hello, I am doing web application project in jsp. In webform ,I am displaying database data in html table. So my question is ,I want so save this html format data in pdf/excel format
how to create database and table using jsp
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... table using jsp code... the table name should be the name of the person
JSP textbox autopopulation on basis of SQL table values
JSP textbox autopopulation on basis of SQL table values  Hi, I need... table is created in MySQL DB: Problem type Status Responsible LEGAL CONTROL NEW ABC LEGAL Dept PENDING PQR There are 2 list box on JSP , one
How to import data from sql server table into an excel file by creating the rows dynamically in the excel according to the dataabase??
How to import data from sql server table into an excel file by creating... data from sql server table into an excel file by creating the rows dynamically in the excel according to the dataabase?? There is a table in sql server having
Creating table using DBCP
= con.createStatement(); String QueryString = "CREATE TABLE user_master1(User_Id INTEGER...(QueryString); System.out.println("Table created!!"); con.close(); } catch
problem on jsp, inserting data into table(mysql).
problem on jsp, inserting data into table(mysql).  hello friends, I have a problem in jsp.I want to insert data, which is given by user through a html page into a table.And the table name also is given by the user.My database
Inserting values into a database table of selected DropDown in jsp.
Inserting values into a database table of selected DropDown in jsp.  http://www.roseindia.net/answers/viewqa/Ajax/15250-DropDown-in-ajax+jsp.html... selected india and then goa. I want to insert it to a table which has two fields
problem in creating web application using servelet, jsp, jdbc and xml - JSP-Servlet
problem in creating web application using servelet, jsp, jdbc and xml  Using Servlet, JSP, JDBC and XML create a web application for a courrier... the technologies JSP,Servlet and JDBC etc... Thanks
how to display data in List Or grid or in table in Jsp
how to display data in List Or grid or in table in Jsp   <%@page...;JSP Reading Text File</title> </head> <body> <...(); } %> </body> </html> this is my jsp code i want to display
instance
instance  What is the exact need of declaring a variable as instance
INSTANCE
INSTANCE  PLEASE TELL ME THE EXACT MEANING OF INSTANCE?AND EXPLAIN
use data from database table as hyperlink value - JSP-Servlet
use data from database table as hyperlink value  I'm creating a web...; For more information on JSP visit to : http://www.roseindia.net/jsp/ Thanks
jsp code for dynamic time table generation - JSP-Servlet
jsp code for dynamic time table generation  hi I am doing my academic project college automation. I want to generate time table dynamically... For the above code, we have created three database tables 1)btech: CREATE TABLE
How to search the selected item in row table using radia button in JSP?
How to search the selected item in row table using radia button in JSP?  How to search the selected item in row table using radia button in JSP
creating new file with File class - JSP-Interview Questions
creating new file with File class   I have a file uploading concept in my project. For that, while creating a new file is it necessary to give the full path of the location from the drive letter in the parameter
Retrieve database from the table dynamically in jsp from oracle using servlet
Retrieve database from the table dynamically in jsp from oracle using servlet  Sir, I have created a table in oracle using eclipse, and added few columns dynamically. I want to retrieve the table with all columns dynamically
How to Sort Column values of a jsp table - JSP-Servlet
How to Sort Column values of a jsp table  Hi Friend Please help me. I am displaying reultset values on multiple jsp page. I want to sort.../jsp/ http://www.roseindia.net/jdbc/ Thanks
Dynamic retrieval od data from database and display it in the table at jsp
Dynamic retrieval od data from database and display it in the table at jsp  Hi friends.... Am creating software for chit fund.. I want to display the details of 20 members in a table format at jsp page by dynamically retrieving
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields

Ads