Solved

JSP (inputs) -> HashMap -> Database

Posted on 2006-11-07
8
618 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat users xml 7 122
web service Rest Client creation 4 87
stateless EJB with glassfish 2 73
mask rule in struts validation.xml 5 87
If you thought ransomware was bad, think again! Doxware has the potential to be even more damaging.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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