Hi friend,
Code to update record retieve at the Same time:
Some points to be remmeber:
1.Create a "process.jsp" file to update and retrieve record from database.
2.Using set the arraylist in the request parameter and forward to next page.
"process.jsp"
<%@ page import="java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
<%
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String first_name = request.getParameter("first_name");
String last_name = request.getParameter("last_name");
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:
mysql://localhost:3306/";;
String dbName = "user_register";
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String userPassword = "root";
java.util.Date now = new java.util.Date();
String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
String strDateNew = sdf.format(now) ;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,username,userPassword);
Statement st = conn.createStatement();
Statement stmt = conn.createStatement();
String strQueryUpdate = "update user_register set first_name='"+first_name+"',last_name='"+last_name+"' where username='"+userName+"' and password='"+password+"'";
int i = st.executeQuery(strQuery);
String strQuery = "select * from user_register";
//out.println(strQuery);
ResultSet rs = st.executeQuery(strQuery);
ArrayList al = new ArrayList();
ArrayList arList;
while(rs.next())
{
arList = new ArrayList
String first_name= rs.getString("first_name");
String last_name= rs.getString("last_name");
al.add(first_name);
al.add(last_name);
arList.add(al);
}
request.setAttribute("arList",arList);
String nextJSP = "/home.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
Display record from database:
"home.jsp"
<%
List arlist = (ArrayList)request.getAttribute("arList");
Iterator itr = arlist.iterator();
Iterator itrrecord;
while(itr.hasNext())
{
Arraylist alrecord = (ArrayList)itr.next();
itrrecord = arlist.iterator();
while(itr.hasNext())
{
out.println(itr.next());
}
}
%>
Thanks
For read more details to visit....
http://www.roseindia.net/servlets/web-application.shtmlThanks