Solved

JSP (inputs) -> HashMap -> Database

Posted on 2006-11-07
8
621 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Deploying our service is a grudge match between customer benefits and customer pain. In one corner, rolling out fixes (yay!) and delivering new features (double yay!). In the other corner, training on new features (boo – sounds like work), and chan…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 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