Hi,
 I am very new to faces  while working with data bases I am facing  some
problem
 I am selecting a row and deleting that row by clicking delete button.
And  same record (row) that   I am  adding to  the database  previously
which I deleted directly  commiting in database  1.e in oracle side
While  I  clicked on the refresh button on the browser. . that record is
again deleteing from the database with out clicking on delete button. 

there is any alternative way to get the only selected item.without the
getting from
attribute value 

 Selectone.jsp
<html>
   <%@ taglib uri="http://java.sun.com/jsf/core";  prefix="f" %>
   <%@ taglib uri="http://java.sun.com/jsf/html";  prefix="h" %>
   <%@ taglib uri="http://myfaces.apache.org/tomahawk"; prefix="t"%>
   <f:view>
      <head>
         <f:loadBundle basename="test.messages" var="msgs"/>
         <link href="styles.css" rel="stylesheet" type="text/css"/>
         <title>
            <h:outputText value="#{msgs.pageTitle}"/>
         </title>
      </head>
      <body>
         <h:messages globalOnly="true" showDetail="true"
styleClass="errors"/>
         <h:form>
             <t:dataTable value="#{test.all}" var="test1" 
               styleClass="customers" 
               headerClass="customersHeader" columnClasses="custid,name" > 
                <h:column>
                  <f:facet name="header">
                     <h:outputText value="#{msgs.name}"/>
                  </f:facet>
                 
                  <t:selectOneRadio id="first" forceId="true" 
                    forceIdIndex="false" layout="pageDirection" 
value="#{test.val}" >
                   <f:selectItem itemValue="#{test1.name}"/>
                  </t:selectOneRadio>
                    </h:column>
               </h:panelGroup>         
               <h:column>
                  <f:facet name="header">
                     <h:outputText value="#{msgs.class}"/>
                  </f:facet>
                  <h:outputText value="#{test1.class}"/>
               </h:column>
                 </t:dataTable>   
         
                <h:commandButton id="fir" type="submit" value="delete" 
actionListener="#{test.deleteRow}"/>
    </body>
     </f:view>  
</html>

package mypack;
/* this is naming */
import javax.naming.NamingException;
/* this is sql */
import java.sql.*;
import javax.sql.*;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
/* this is data models*/
import javax.faces.model.ResultDataModel;
import javax.faces.model.ResultSetDataModel;
/* this is faces content*/
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.context.*;
import javax.faces.event.*;
import javax.faces.model.*;
import javax.faces.context.FacesContext;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.html.HtmlDataTable;


public class SelectClass{
  private Connection con;  
  private Statement st;
  private ResultSet rs;
  private String val;
  private PreparedStatement ps; 
  private ResultDataModel model = null;
  private HtmlDataTable table1;
  public ResultDataModel getAll(){
  
    try{  
       openConnection();
       System.out.println("connection is opened");
       st=con.createStatement();
       System.out.println("statement is opened");
           rs=st.executeQuery("select * from test");
           if(rs==null){
        System.out.println("result set is null");
           }
        model = new ResultDataModel(ResultSupport.toResult(rs));
        return model; 
     } 
     catch(SQLException e){
        e.printStackTrace();
     }
     finally{
       try{
              if(rs!=null)
                    rs.close();
                    rs=null;
        }
                catch(SQLException e){
                e.printStackTrace();
                }      
                try{
          if(st!=null)
                        st.close();
                    rs=null;
        }
                catch(SQLException e){
              e.printStackTrace();
                          
                }
        try{
          close();
                }  
                catch(SQLException e){
                  e.printStackTrace();
                          
                }
          }
          return null;  
  }
  public String getVal(){
    return val;
  }
  public void setVal(String val){
    this.val=val;
  }
  public void deleteRow(ActionEvent event){
    System.out.println("clicked");
        Application 
application=FacesContext.getCurrentInstance().getApplication();
        try{ 
          /*System.out.println("the selected value is"+val);    
if(val!=null){
             openConnection();
           System.out.println("deleting  a row");
             ps=con. prepareStatement("delete from test where name=?");  
             ps.setString(1,val);
             ps.execute();
             
         }
          
         
    }    
    catch(SQLException e){
        e.printStackTrace();
    }
        catch(Exception e){
         e.printStackTrace();
    }
    finally{
       try{
              if(rs!=null)
                    rs.close();
                    rs=null;
        }
                catch(SQLException e){
                e.printStackTrace();
                }      
                try{
          if(ps!=null)
                        ps.close();
                    ps=null;
        }
                catch(SQLException e){
              e.printStackTrace();
                          
                }
        try{
          close();
                }  
                catch(SQLException e){
                  e.printStackTrace();
                }
                
        }
    
  } 
  public void openConnection(){
    try{  
     if(con==null){  
       DAOFactory Factory =DAOFactory.getDAOFactory(1);
       con=((OracleFactory)Factory).getConnection();
       System.out.println("connection is in select factory");
          } 
    }
    catch(Exception e){
       e.printStackTrace();
    }  
    
  } 
  
  
  public void close()throws SQLException{
    if(con==null){
      System.out.println("connection already closed");
      return;
     }
      con.close();
      System.out.println("closed connection");  
      con = null;
  } 
}



<?xml version="1.0"?>

<!DOCTYPE faces-config PUBLIC
   "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
   "http://java.sun.com/dtd/web-facesconfig_1_0.dtd";>

<faces-config>
   <application>
     <locale-config>
        <default-locale>en</default-locale>
        <supported-locale>en_US</supported-locale>
     </locale-config>
         <message-bundle>test.messages</message-bundle>
   </application>
   <managed-bean>
      <managed-bean-name>test</managed-bean-name>
      <managed-bean-class>mypack.SelectClass</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
   </managed-bean>
  <!--  <navigation-rule>
     Add a navigation comming from UserRegistration.jsp 
    <from-view-id>/userform.jsp</from-view-id>
    <navigation-case>
       Add a navigation going to results
      <from-outcome>success</from-outcome>
      <to-view-id>/results.jsp</to-view-id>
    </navigation-case>    
  </navigation-rule>  -->
</faces-config>

with regards
shannu sarma

--
View this message in context: 
http://www.nabble.com/problem-with-datatable-and-database-t1733220.html#a4709544
Sent from the MyFaces - Users forum at Nabble.com.

Reply via email to