Improve company productivity with a Business Account.Sign Up

x
?
Solved

JSP (inputs) -> HashMap -> Database

Posted on 2006-11-07
8
Medium Priority
?
657 Views
Last Modified: 2010-05-18
Hi

I have wriiten a JSP that will display textboxs in tabular format(look like grid layout).
I am putting each textbox value into a HashMap.... each row has 8 text boxes....
and accessing outside.

My question is how to update the values that I have access to database.
Now the values are in HashMap.

Below is the code that gives idea how I am accessing ...
My question is how can I update this values to database....
It would be great if you can tell me how to update... if you have any example that would be helpfull...


<%@ page language="java" import="java.sql.*" import="java.util.*" %>
<html>
<body>
<%
List rowval = (List)request.getAttribute("data");
Iterator listIt = rowval.iterator ();
while (listIt.hasNext ()) {
      HashMap colval = new HashMap();
      colval = (HashMap)listIt.next();
    String id;
    for(int i = 1; i <= colval.size(); i++) {
    Integer key = new Integer(i);
      String iKey = key.toString();
      if(i == 1){
      id = (String)colval.get(iKey);
      }
      else{
%>
      <%= colval.get(iKey) %>
<%  
      }
   }
 %>
 <br>
 <%
 }
%>
</body>
</html>

Thanks
0
Comment
Question by:basirana
  • 4
  • 2
  • 2
8 Comments
 
LVL 11

Expert Comment

by:Manish
ID: 17896654
Are you want to update the database from JSP?
0
 
LVL 11

Expert Comment

by:Manish
ID: 17896666
0
 
LVL 9

Expert Comment

by:matthew016
ID: 17897126
Not sure I understand perfectly,

But to be able to modify the data in the HashMaps,
u have to create a form, in your .jsp, and in this form u need to have input text fields, and put the "iKey"s in the text fields,
then when u submit the form, u forward to a servlet that will access the database with JDBC
(for jdbc tutorial see link of karanw)

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:basirana
ID: 17899164
Report wit textboxes - program1

<%@ page language="java" import="java.sql.*" %>
<%!
Connection connection ;
Statement statement ;
ResultSet rs ;
ResultSetMetaData rsmd ;
%>
<HTML>
<HEAD>
<TITLE>
Functional Data
</TITLE>
</HEAD>
<BODY>

<%
      Class.forName("oracle.jdbc.driver.OracleDriver") ;
      connection = DriverManager.getConnection( "jdbc:oracle:thin:@.....",  "...", "...") ;
      statement =  connection.createStatement() ;
      rs =   statement.executeQuery( "SELECT * FROM EMP") ;
      rsmd = rs.getMetaData() ;

%>
  <h3> Functional Report</h3>
  <form method="POST" action ="update.jsp">
   <TD height = 10><input type="submit" value="update">
     <TABLE WIDTH="100%" BORDER="+5" >
    <TR > <h5>
        <%
        for(int i = 1 ; i <= rsmd.getColumnCount() ; i++) {
        %> </h5>
            <TH>
                    <font size=2><%= rsmd.getColumnLabel(i) %></font>
            </TH>
        <% } %>
    </TR>
    <% int r=0; %>
    <% while(rs.next()) {
    r++;
    int c=0;
    %>
    <TR>
    <TD height = 10><font size=2> <input type="hidden" name=<%="r"+r+"c"+c++%> value= <%= rs.getString(1) %> ></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(2) %></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(3) %></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(4) %></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(5) %></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(6) %></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(7) %></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(8) %></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(9) %></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(10) %></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(11) %></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(12) %></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(13) %></font>  </TD>
    <TD height = 10><font size=2> <input type="text" name=<%="r"+r+"c"+c++%> value= <%= rs.getString(14) %> ></font> </TD>
    <TD height = 10><font size=2> <input type="text" name=<%="r"+r+"c"+c++%> value= <%= rs.getString(15) %> ></font> </TD>
    <TD height = 10><font size=2> <input type="text" name=<%="r"+r+"c"+c++%> value= <%= rs.getString(16) %> ></font> </TD>
    <TD height = 10><font size=2> <input type="text" name=<%="r"+r+"c"+c++%> value= <%= rs.getString(17) %> ></font> </TD>
    <TD height = 10><font size=2> <input type="text" name=<%="r"+r+"c"+c++%> value= <%= rs.getString(18) %> ></font> </TD>
    <TD height = 10><font size=2> <input type="text" name=<%="r"+r+"c"+c++%> value= <%= rs.getString(19) %> ></font> </TD>
    <TD height = 10><font size=2> <input type="text" name=<%="r"+r+"c"+c++%> value= <%= rs.getString(20) %> ></font> </TD>
    <TD height = 10><font size=2> <input type="text" name=<%="r"+r+"c"+c++%> value= <%= rs.getString(21) %> ></font> </TD>
    <TD height = 10><font size=2> <%= rs.getString(22) %></font>  </TD>
    <TD height = 10><font size=2> <%= rs.getString(23) %></font>  </TD>
     <TD height = 10><font size=2> <%= rs.getString(24) %></font>  </TD>
    </TR>
    <% } %>
    <tr><input type="hidden" name="count" value=<%=r%> ></tr>
  </TABLE>
 </form>
</BODY>
</HTML>
0
 

Author Comment

by:basirana
ID: 17899184
Adding rows to Array List -- Program2

<%@ page language="java" import="java.sql.*" import="java.util.*" %>
<html>
<body>
<%int count=Integer.parseInt(request.getParameter("count")) ;%>
<%
List  rowval = new ArrayList();
      for(int r = 1;r<=count;r++){
      HashMap colval = new HashMap();
            for(int c = 1; c <= 8; c++){
            String pname = "r"+r+"c"+c;
            String reqStr=request.getParameter(pname);
            Integer i = new Integer(c);
            String iStr = i.toString();
            colval.put(iStr ,reqStr);
%>
<%= reqStr %>
<%
  }
 rowval.add(colval);
%>
<br>
<%
 }
 request.setAttribute("data",rowval);
 RequestDispatcher view =
 request.getRequestDispatcher("result.jsp");
 //--Use the request dispatche to ask the container to crank JSP
 view.forward(request, response);
%>
</body>
</html>
0
 

Author Comment

by:basirana
ID: 17899216
Accessing the ArrayList objects and HashMap Values -- Program3

<%@ page language="java" import="java.sql.*" import="java.util.*" %>
<%@ page session="false" %>
<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
<%!
Connection connection ;
Statement statement ;
ResultSet rs ;
ResultSetMetaData rsmd ;
%>  
<html>
<body>
<%
try{
      Class.forName("oracle.jdbc.driver.OracleDriver") ;
      connection = DriverManager.getConnection( "jdbc:oracle:thin:@...",  "...", "...") ;
      statement =  connection.createStatement() ;
      connection.setAutoCommit(false);
      statement.clearBatch();
      }catch(Exception exp){
      exp.printStackTrace();
      }
     
%>
<%= "Result Page Update values here" %>
<%
try{
List rowval = (List)request.getAttribute("data");
Iterator listIt = rowval.iterator ();
while (listIt.hasNext ()) {
      HashMap colval = new HashMap();
      colval = (HashMap)listIt.next();

      String id = (String)colval.get("1");
      String name = (String)colval.get("2");
      String address = (String)colval.get("3");
    String sql = "UPDATE EMP  SET "+
                         "name = " + name +
                         "address = " + adress +
                          ......                   
                         
                    "WHERE id =" + id ;
   statement.addBatch(sql);
   }
   }catch(Exception exp){
   exp.printStackTrace();
   }

 try{
 out.println("Starting batch update");
 int[] rowsInserted = statement.executeBatch();
  int n = rowsInserted.length;
      int sum = 0;
      for (int i = 0; i < n; i++)
         sum += rowsInserted[i];

      out.println();
      out.println(n + " rows inserted");

      // If all updates were good, commit the transaction

      if (sum == n) {
         out.println("Committing transaction");
         connection.commit();
      }

      // Otherwise, rollback

      else {
         out.println("Rolling back transaction");
         connection.rollback();
      }
   } catch(Exception exp){
   exp.printStackTrace();
   }
   finally {
     // Close any open JDBC objects
      if (statement != null) statement.close();
      if (connection != null) connection.close();
   }
 
// RequestDispatcher view =
// request.getRequestDispatcher("report.jsp");
//--Use the request dispatche to ask the container to crank JSP
// view.forward(request, response);
%>
</body>
</html>


0
 

Author Comment

by:basirana
ID: 17899269
If you check there above there are 3 programs

program 1 --> display report like form and get inputs from user
program 2 --> get the parameters and put them in HashMap --> ArrayList
program 3 --> get the hashmap values and update the records in the database


Problem 1 is the records are not getting updated
Problem 2 There is not connection handler for the code  

1. How can I reset the code so that I can get connection from connection handler?
2. How to update the values to database. because it is not a single record there are like 100 records?

Thanks
0
 
LVL 9

Accepted Solution

by:
matthew016 earned 1500 total points
ID: 17912578
How do u mean no connection handler ?

Do u have any errors ? Did u try to add some println's ?

The thread execute this block ?
      if (sum == n) {
         out.println("Committing transaction");
         connection.commit();
      }
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

With the functions here, you can parse, convert, and format back and forth between feet and inches and fractions and decimal inches - for normal as well as extreme values and with extreme precision.
As a Tester when requirement in software testing changes consistently every team member should be prepared to deal with the adjustments in the project.
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
Watch the video to know the process of migration of Exchange or Office 365 mailboxes in absence of MS Outlook. It is an eminent tool which can easily migrate Public, Archive user mailboxes from one another Exchange server and Office 365. Kernel Migr…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question