Solved

JSP (inputs) -> HashMap -> Database

Posted on 2006-11-07
8
612 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article, I will show you HOW TO: Suppress Configuration Issues and Warnings Alert displayed in Summary status for ESXi 6.5 after enabling SSH or ESXi Shell.
Hyena v12.2 is now available for downloading and is available in English, French, German and Spanish versions.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now