troubleshooting Question

confused over a silly problem

Avatar of aman0711
aman0711 asked on
JavaJava EE
38 Comments2 Solutions354 ViewsLast Modified:
Hi Experts,

          I am real confused with this problem. I have all the code yet I dont know how to
              get this fixed.

              so the scanrio is:

              In the snapshot you can see a form. The "Transaction" dropdown has two values:
              "Add Note", "Update Note".

              If user picks "Add Note", a simple insert statement is invoked and this part is working fine.

              When the user picks "Update Note", the page refreshes and the notes already present in the
              database are display (Shown in the snapshot).

               So in my Servlet, I had this piece of code:

            if (transType.equalsIgnoreCase("Add Note")) //TRANSACTION TYPE IS "Add Note"
                {
                        nt.addNotes(noteInfo); //invoke addNotes method in notes class
                  }
                  else {
                        list =  nt.updateNote(); // invoke updateNote method and return the table
                        getServletContext().setAttribute("notesTable", list);
                  }
            

          Now the place where I am confused is:

              Once this table is displayed after picking "Update Note" dropdown, User can click on anyone
              of the check boxes and update that particular note.

              I have posted my Notes.java class, where I have a condition inside addNote() for insert and update
              query (I know the update query wont make any sense in this method).

              updateNote() is right below it which returns the already present notes in database.

              How do I solve this problem?
package com.si.dao;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Vector;
 
import com.si.admin.Note;
import com.si.admin.NoteTable;
import com.si.dao.DbConn;
 
 
public class Notes {
	
//	prod
	String url = "jdbc:oracle:thin:@xyz12.abcd.com:1522:td08";
	String user = "user1";
	String password = "pass1";
	String driver = "oracle.jdbc.driver.OracleDriver";
 
 
	 Connection conn= null;
     PreparedStatement pstmt = null;
     Statement stmt = null;
 	 ResultSet rs = null;
	
     DbConn db = null;
        
       public String addNotes(Note noteInfo) {
    	 
    	   System.out.println("Inside Add Note method");
    	   
    	 String fromDate = noteInfo.getFromDate();
    	 String toDate = noteInfo.getToDate();
    	 String placement = noteInfo.getPlacement();
    	 String note = noteInfo.getNotes();
    	 String transaction = noteInfo.getTransType();
    	 String attuid = noteInfo.getAttUid();
    	     
    	 
    	 if (transaction.equalsIgnoreCase("Add Note"))
    	 {
    	 
      	 String qry = "INSERT INTO SIWEB_NOTES(PLACEMENT, FROM_DATE, TO_DATE, NOTE, CREATED_BY) VALUES (?,?,?,?,?)";
      	 
    	     try {
    	    	 
    		  db = new DbConn();
    		  Class.forName(driver);
    		  conn = DriverManager.getConnection(url, user, password);
    		  pstmt = conn.prepareStatement(qry); 
    		  
    		  
    		  
    		  SimpleDateFormat df=new SimpleDateFormat("MM/dd/yyyy", Locale.US);
    		  
    		   java.util.Date sqlFromDate=null;
    		   java.util.Date sqlToDate=null;
			try {
				 sqlFromDate = df.parse(fromDate);
				  sqlToDate = df.parse(toDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		  
    		   
    		   pstmt.setString(1, placement);
    		   pstmt.setDate(2, new java.sql.Date(sqlFromDate.getTime()));
    		   pstmt.setDate(3, new java.sql.Date (sqlToDate.getTime()));
    		   pstmt.setString(4, note);
    		   pstmt.setString(5, attuid);
    		   
    		   pstmt.executeUpdate();
    		      		    
    		  
    	 }  catch (SQLException sqle) {
 			sqle.printStackTrace();
    	 } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				conn.close();
				pstmt.close();
				} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
    	  } // if ends here 
    	 
    	 
    	 else {
    		 
    		  System.out.println("Inside update block");
    		  
    		  String qry = "UPDATE SIWEB_NOTES SET PLACEMENT = ?, FROM_DATE = ?, TO_DATE = ?, NOTE = ?, UPDATED_BY = ?";
    		 
    		   try {
      	    	 
    	    		  db = new DbConn();
    	    		  Class.forName(driver);
    	    		  conn = DriverManager.getConnection(url, user, password);
    	    		  pstmt = conn.prepareStatement(qry); 
    	    		  
    	    		  
    	    		  
    	    		  SimpleDateFormat df=new SimpleDateFormat("MM/dd/yyyy", Locale.US);
    	    		  
    	    		   java.util.Date sqlFromDate=null;
    	    		   java.util.Date sqlToDate=null;
    				try {
    					 sqlFromDate = df.parse(fromDate);
    					  sqlToDate = df.parse(toDate);
    				} catch (ParseException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    	    		  
    	    		   
    	    		   pstmt.setString(1, placement);
    	    		   pstmt.setDate(2, new java.sql.Date(sqlFromDate.getTime()));
    	    		   pstmt.setDate(3, new java.sql.Date (sqlToDate.getTime()));
    	    		   pstmt.setString(4, note);
    	    		   pstmt.setString(5, attuid);
    	    		   
    	    		   pstmt.executeUpdate();
    	    		      		    
    	    		  
    	    	 }  catch (SQLException sqle) {
    	 			sqle.printStackTrace();
    	    	 } catch (ClassNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} finally {
    				try {
    					conn.close();
    					pstmt.close();
    					} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    	    		 
    	 }
 
 	   	 
    	return fromDate;
     
       }
       
       
       public Vector<NoteTable> updateNote() {
    	      	   
    	    System.out.println("Inside Update Note method");
    	   
    	    NoteTable noTab;
    	 	     	
            Vector<NoteTable> nTabVec = new Vector<NoteTable>();
       	    String qry = "SELECT CREATED_BY, UPDATED_BY, PLACEMENT, TO_CHAR(FROM_DATE, 'MM/DD/YYYY'), TO_CHAR(TO_DATE, 'MM/DD/YYYY'), NOTE FROM SIWEB_NOTES";
    	      
    	     try {
    	    	db = new DbConn();
       		    Class.forName(driver);
       		    conn = DriverManager.getConnection(url, user, password); 
    	        	    	 
				pstmt = conn.prepareStatement(qry);
				
				rs = pstmt.executeQuery(qry);
				
				while (rs.next()) {
					
					noTab = new NoteTable();
					
					noTab.setCreatedBy(rs.getString(1));
					noTab.setUpdatedBy(rs.getString(2));
					noTab.setPlacement(rs.getString(3));
					noTab.setFrom(rs.getString(4));
					noTab.setTo(rs.getString(5));
					noTab.setNote(rs.getString(6));
					
					nTabVec.add(noTab);	
																						
					}
						
			  } catch (SQLException e) {
				// TODO Auto-generated catch block
			    	e.printStackTrace();
		      	} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				           try {
				        	   pstmt.close();
				        	   rs.close();
				        	   conn.close();
				           } catch (Exception e) {
				        	   e.printStackTrace();
				           }
				
		      	}
		   	
		      	return nTabVec;
			
       }
       
    	   	
 }
snap1.PNG
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 38 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 38 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros