#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Delete data from database through radio


    I am tring to delete data but I have to click twice on radio button to delete the data, can any one help me, what is the problem ?

    Code:
    import java.io.*;
    import java.sql.*;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    
    public class DataInsertTable extends HttpServlet
    {
    	public void doGet(HttpServletRequest req, HttpServletResponse res)
    		throws IOException, ServletException
    		{
    			res.setContentType("text/html");
    			PrintWriter out = res.getWriter();
    //			PrintWriter pwinsert = res.getWriter();
    //			PrintWriter pwdelete = res.getWriter();
    			
    			Connection con = null;
    			PreparedStatement ps = null;
    			ResultSet rs = null;
    			Statement st = null;
    			
    			
    		    out.println("<html>");
    		    out.println("<head>");
    		    
    		    	out.println("<title>User Data</title>");
    
    		    out.println("</head>");
    
    		    
    		    out.println("<body>");
    		    
    		    out.println("<center><u><h1>User Data</h1></u>");
    		    
    
    		    out.println("<form name='form' >");
    		    
    			out.println("<table border="+2+ "> ");
    			out.println("<tr>");
    				out.println("<td> Select </td>");
    				out.println("<td> Name </td>");
    				out.println("<td> Roll No. </td>");
    				out.println("<td> Class </td>");
    				out.println("<td> Mobile Number </td>");
    			out.println("</tr>");
    
    
    
    
    			try	{
    				String nm = req.getParameter("name");
    				String roll = req.getParameter("roll");
    				String clas = req.getParameter("clas");
    				String mono = req.getParameter("mono");
    
    				
    				
    				try	{
    					Class.forName("oracle.jdbc.driver.OracleDriver");
    				}
    				catch(ClassNotFoundException ex)	{
    					System.out.println("driver not loaded");
    					System.exit(0);
    				}
    
    				String URL = "jdbc:oracle:thin:@192.168.106.87:1521:ORA11G";
    				String Username = "fuel_db";
    				String Password = "fuel123";
    
    
    				con = DriverManager.getConnection(URL,Username,Password);
    
    
    				ps = con.prepareStatement("INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ? )");
    				ps.setString(1,nm);
    				ps.setString(2,roll);
    				ps.setString(3,clas);
    				ps.setString(4,mono);
    
    				int i = ps.executeUpdate();
    				out.println(i);
    				if(i!=0)	{
    					out.println("Your data has been stored in the database");
    				}
    				else	{
    					out.println("Your data could not be stored in the database");
    				}
    			}
    			catch(Exception e)	{
    				out.println(e.getMessage());
    			}
    			
    			
    /*			
    			String idr = "DELETE FROM student WHERE mobileno='0' OR rollno='0'";
    
    			try	{
    				st=con.createStatement();
                    rs = st.executeQuery(idr);
                }
    			catch (Exception e)	{
    				e.getMessage();
                    System.out.println("Error is " +e);
                }
    */
    			
    			
    			String idr = "SELECT * FROM student WHERE name IS NOT NULL";
    //			String idr = "SELECT * FROM student";
    
    			try	{
    				st=con.createStatement();
                    rs = st.executeQuery(idr);
                }
    			catch (Exception e)	{
    				e.getMessage();
                    System.out.println("Error = " +e);
                }
    
    
                try {
                	while (rs.next())	{
                		out.println("<tr>");
                		out.println("<td>" + "<input type=\"radio\" name=\"choise\" value=\"" + rs.getString(1) + "\" /> </br>" + "</td>");
    //            		out.println("<td>" + "<input type=\"submit\" name=\"choise\" value=\"" + rs.getString(1) + "\" /> </br>" + "</td>");
                		out.println("<td>" + rs.getString(1) + "</td>" + "\t <td>"  + rs.getInt(2) + "</td>" + "\t <td>" + rs.getString(3) + "</td>" + "\t <td>" + rs.getInt(4));
                		out.println("</tr>");
                		}
    
                	out.println("<tr>");
                	out.println("<td> </td>");
                		out.println("<td> </td>");
                		out.println("<td>" + "<input type=\"submit\" value=\"Delete Data\" />" + "</td>");
                		out.println("<td> </td>");
                		out.println("<td> </td>");
            		out.println("</tr>");
                }
                catch (Exception e) {
                	e.getMessage();
                	System.out.println("Error" +e);
                }
    
    
                String nm = req.getParameter("choise");
    //            String clas = req.getParameter("choise");
    //            String clas = req.getParameter(nm);
    
                idr = "DELETE from student WHERE name ='"+nm+"' OR rollno='0'" ;
    //            idr = "DELETE from student WHERE (name ='"+nm+"') AND (clas IS NOT NULL)"; 
    //            idr = "SELECT * FROM student WHERE name,rollno,class NOT NULL"; 
    
    
               	try {
    				ps = con.prepareStatement(idr);
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    			
                try {
    				st=con.createStatement();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
                   int pw = 0;
    			try {
    				pw = ps.executeUpdate(idr);
    			} catch (SQLException e1) {
    				e1.printStackTrace();
    			}
                   out.println(pw);
                
          
        		
        		
    /*
        		try	{
        			st=con.createStatement();
                    rs = st.executeQuery(idr);
                }
        		catch (Exception e)	{
        			e.getMessage();
                    System.out.println("Error  " +e);
                }
        		
     */
        		
        		
    //    		System.out.println("Data deleted...");
    
               
                try {  
                    rs.close();
                }
                catch (Exception e) {
                    e.getMessage();
                    System.out.println("Error" +e);
                }
    
                try {
                    st.close();
                } 
                catch (Exception e) {
                    e.getMessage();
                    System.out.println("Error" +e);
               }
    
                out.println("</table>");
                out.println("</form>");           
                out.println("</center>");
                out.println("</body>");
                out.println("</html>");
                out.close();
    	}
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2004
    Location
    Washington DC
    Posts
    2,756
    Rep Power
    1571
    wow, it's a thread from 2005.

    Aside from this being an example of how to not use databases, servlets, or java in general, what do you mean by 'click twice to delete data?'

    When you click the button the first time, how do you know the data isn't deleted? Did you check the database to see if the data is still in there, or are you basing that off of what the UI is showing?
    Open for extension, closed for modification

IMN logo majestic logo threadwatch logo seochat tools logo