Solved

JSP (inputs) -> HashMap -> Database

Posted on 2006-11-07
8
613 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
html form inside div 3 91
Display the newly inserted values 3 74
nested if has else if 13 92
JavaScript parse form with colon separated values 2 73
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In 2017, ransomware will become so virulent and widespread that if you aren’t a victim yourself, you will know someone who is.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 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