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

Ads

 
 
 

Share on Google+Share on Google+

anand
how to perform operation on data retrieved from database in jsp?
2 Answer(s)      5 years and 11 months ago
Posted in : JSP-Servlet

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

Ads
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:

<[email protected] language="java"%>
<[email protected] 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:

<[email protected] 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:

<[email protected] 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){}
%>

Ads









Related Tutorials/Questions & Answers:
Advertisements
Tutorials   
Java Spring Hibernate Struts Training fat image from db fat image from db fat image from db fat image from db unable to get value returned from javascript variable in page How to change image size before adding to mysql using jsp Design a registration.jsp page with input for user registration details Cart Quantity jsp servlet coding issue servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? image pdf marking pdf marking Multiple image upload using JSP Read code from excel sheet and upload into database using JSP Student java about search engine Convert BLOB to PDF and save it to a location how to copy files from remote to local machine using SFTP SSL in java? Like Team viewer Socket and Servert -Jsp how to copy files from remote location to local using java? How To Store Multilple Images In MySQL database dynamically with image uploader and then retrieve them in image gallery using java how to create SOAP based web service in java? how to fetch data from mysql database table and draw a bar chart on that data using in jsp how to create users in LDAP using java? how to authenticate user using TIVOLI api in java? how to create users using IBM Tivoi API in jsp/servelet? Java/J2ee Please explain this code as many packages and classes are unknown to me .! how to download the uploaded folder files using jsp Login Query image is display from path of mysql database please reply the code to store dynamic fields in database? Reloading issue in jsp How to get Button Id ? calling one jap page from another jsp page login login Problem to get connection from DAO class to JDBC Page Loading Time Fix table's column's name row(1st row of the table) so that it does not move up when the table is scrolled up to view more rows below Sending hidden values

Ads

 
Advertisement null

Ads