how to perform operation on data retrieved from database in jsp?

how to perform operation on data retrieved from database in jsp?

i developed a project on student marks system.in this project i successfully entered marks into the database from the jsp page and i can also retrieved data from database based on the input given but i want to do some addition operation on data retrieved from database and the present to the user PLEASE HELP ME TO SOLVE THIS PROBLEM THANKS IN ADVANCE

View Answers

June 17, 2011 at 3:52 PM

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();
}
function deleteRecord(id){
    var f=document.form;
    f.method="post";
    f.action='delete.jsp?id='+id;
    f.submit();
}
</script>
</head>
<body>

<br><br>
<form method="post" name="form">
<table border="1">
<tr><th>First Name</th><th>Last Name</th><th>Address</th><th>Contact No</th><th>Marks</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 student";
st = con.createStatement();
ResultSet rs = st.executeQuery(query);
%>
<%
while(rs.next()){
%>
<tr><td><%=rs.getString("firstname")%></td>
<td><%=rs.getString("lastname")%></td>
<td><%=rs.getString("address")%></td>
<td><%=rs.getString("phone")%></td>
<td><%=rs.getString("marks")%></td>

<td><input type="button" name="edit" value="Edit" style="background-color:green;font-weight:bold;color:white;" onclick="editRecord(<%=rs.getString("id")%>);" ></td>
<td><input type="button" name="delete" value="Delete" style="background-color:red;font-weight:bold;color:white;" onclick="deleteRecord(<%=rs.getString("id")%>);" ></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>First Name</th><th>Last Name</th><th>Address</th><th>Contact No</th><th>Marks</th></tr>
<%
String id=request.getParameter("id");
int no=Integer.parseInt(id);
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
String query = "select * from student where id='"+no+"'";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next()){
%>
<tr>
<td><input type="text" name="fname" value="<%=rs.getString("firstname")%>"></td>
<td><input type="text" name="lname" value="<%=rs.getString("lastname")%>"></td>
<td><input type="text" name="address" value="<%=rs.getInt("address")%>"></td>
<td><input type="text" name="phone" value="<%=rs.getString("phone")%>"></td>
<td><input type="text" name="marks" value="<%=rs.getString("marks")%>"></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 17, 2011 at 3:55 PM

3)update.jsp:

<%@page import="java.sql.*"%>
<%
        String ide=request.getParameter("id");
        int num=Integer.parseInt(ide);
        String fname=request.getParameter("fname");
        String lname=request.getParameter("lname");
        String address=request.getParameter("address");
        int contact=Integer.parseInt(request.getParameter("phone"));
        int marks=Integer.parseInt(request.getParameter("marks"));
            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 student set firstname='"+fname+"',lastname='"+lname+"',address='"+address+"',phone="+contact+",marks="+marks+" where id="+num+"");
                response.sendRedirect("application.jsp");
            }
            catch(Exception e){
                System.out.println(e);
            }
%>

4)delete.jsp:

<%@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");
        Statement st = conn.createStatement();
        st.executeUpdate("DELETE FROM student WHERE id = '"+no+"'");
        response.sendRedirect("application.jsp");
    }
    catch(Exception e){}
%>









Related Tutorials/Questions & Answers:
how to perform operation on data retrieved from database in jsp?
how to display data into textboxes which is retrieved from the database
Advertisements
how to display data into textboxes which is retrieved from the database
how to display data into textboxes which is retrieved from the database
displaying data retrieved from a database in a jsp page
How To Display both image and data into Swing JTable which is retrieved from ms access database
Retrive data from database and perform binary tree operations on that data in jsp or java
how to retrieve data from database ?????/
how to retrieve data from database ?????/
how to retrieve data from database ?????/
how to retrieve data from database ?????/
JSP1
how to retrieve data from database
How to use next and previous button(or href) for database table that is retrieved from MySQL DB using jsp,jstl,javascript
How to use next and previous button(or href) for database table that is retrieved from MySQL DB.
how to use String tokenizer on table that is retrieved from database using jsp servlet
how to display data from database in jsp
JSP2
how to use bean to retrieve data from database
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp
How to display data from database in a TableView
How to show data from database in textbox in jsp
Swing JTable which is retrieved from MySQL
how to display data from jsp file into database
How To Fetch Data From Database Into JTextArea
selecting data from database
Protect JSPs from direct access
fetch data from database in javascript
retrive data from database
retrieve data from mysql database and store it in a variable ?
retrive data from database?
How to Retrieve Data from the database and write into excel file using Java
how to store JTree data hierarchically in mysql database from netbeans
How we delete a data of database from front end jsp page
How to get the data from the database using Servlet or JSP program
how to retreive data from database of a particular user after login
How to insert data from textfields into database using hibernate?
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples
Using JOINING Operation in JDBC
How to get the most recent data from the sql database
how to get the data from database - Java Server Faces Questions
how to retreive data from database of a particular user after login
how to get the data from database&how to display the database data in jsf - Java Server Faces Questions
delete data from database - SQL
How to get data from Oracle database using JSP
how to store the data in a array retrived from the database - JSP-Servlet
how to insert data from database using oops concepts - Development process
how to add data dynamically from database into a dropdown list in a jsp

Ads