2014-10-29 22 views
0

我想要做的是向數據庫中顯示數據信息,並且該信息應該可以進行編輯。使用<h:commandButton>加載<h:inputText>中的數據庫中的數據或<commandLink>

我只有一個.xhtml頁面,我想要在該頁面中進行所有CRUD操作。 當前項目正在進行插入,選擇和刪除。 謝謝,任何幫助將不勝感激。

這是.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> 
    <h:head> 
     <h:outputStylesheet library="css" name="styles.css"/> 
     <title>#{msgs.indexTitulo}</title> 
    </h:head> 

    <h:body> 

     <h:messages errorClass="errors" /> 

     <h:outputText value="#{msgs.indexTitulo}" styleClass="emphasis"/> 


     <h:form> 
      <h:panelGrid columns="2"> 
       #{msgs.nombrePlaca} 
       <h:inputText id="placa" value="#{vehiculo.placa}" required="true" label="#{msgs.nombrePlaca}" /> 

       #{msgs.marca} 
       <h:inputText value="#{vehiculo.marca}" required="true" label="#{msgs.marca}" /> 


       #{msgs.modelo} 
       <h:inputText value="#{vehiculo.modelo}" required="true" label="#{msgs.modelo}"/> 


       #{msgs.color} 
       <h:inputText value="#{vehiculo.color}" required="true" label="#{msgs.color}"/> 


       #{msgs.campoAgencia} #{msgs.si} 
       <h:selectBooleanCheckbox value="#{vehiculo.agencia}"/> 


       #{msgs.anio} 
       <h:inputText value="#{vehiculo.anio}" required="true" label="#{msgs.anio}" /> 
       <br/> 


      </h:panelGrid> 
      <h:commandButton value="#{msgs.botonSubmit}" action="#{vehiculo.insertVehiculo()}"/> 

      <h:button value="Limpiar" type="reset"/> 


     </h:form> 

     <h:form > 

      <!-- --> 

      <h:dataTable id="myTable" value="#{vehiculo.seleccionar()}" var="vehiculo"> 
       <h:column>     
        <f:facet name="header">Placa</f:facet>     
        #{vehiculo.placa} 
       </h:column> 
       <h:column> 
        <f:facet name="header">Marca</f:facet> 
        #{vehiculo.marca} 
       </h:column> 
       <h:column> 
        <f:facet name="header">Modelo</f:facet> 
        #{vehiculo.modelo} 
       </h:column> 
       <h:column> 
        <f:facet name="header">Color</f:facet> 
        #{vehiculo.color} 
       </h:column> 

       <h:column> 
        <f:facet name="header">ID</f:facet> 
        #{vehiculo.id} 
       </h:column> 



       <h:column> 
        <h:commandLink value="Eliminar vehiculo" action="#{vehiculo.eliminarVehiculo(vehiculo.id)}"/> 

       </h:column> 

       <h:column> 

        <h:commandButton value="Actualizar vehiculo" action="#{vehiculo.seleccionarPorID(vehiculo.id)}" > 

        <f:ajax execute="@form" render="myTable" /> 


        </h:commandButton> 


       </h:column> 
      </h:dataTable> 


      <!-- --> 



     </h:form> 

    </h:body> 
</html> 

這是豆。

package beans; 

import conexionJDBC.ConexionJDBC; 
import java.io.Serializable; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.List; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 

@ManagedBean(name = "vehiculo") 
@RequestScoped 
public class Vehiculo implements Serializable { 

    private int id; 
    private Integer placa; 
    private String marca; 
    private String color; 
    private Integer anio; 
    private String modelo; //Modelo del carro, ejemplo: Civic 
    private boolean agencia; //Si es comprado en agencia 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public Integer getPlaca() { 
     return placa; 
    } 

    public void setPlaca(Integer placa) { 
     this.placa = placa; 
    } 

    public String getMarca() { 
     return marca; 
    } 

    public void setMarca(String marca) { 
     this.marca = marca; 
    } 

    public String getColor() { 
     return color; 
    } 

    public void setColor(String color) { 
     this.color = color; 
    } 

    public Integer getAnio() { 
     return anio; 
    } 

    public void setAnio(Integer anio) { 
     this.anio = anio; 
    } 

    public String getModelo() { 
     return modelo; 
    } 

    public void setModelo(String modelo) { 
     this.modelo = modelo; 
    } 

    public boolean isAgencia() { 
     return agencia; 
    } 

    public void setAgencia(boolean agencia) { 
     this.agencia = agencia; 
    } 

    /** 
    * Default constructor. 
    */ 
    public Vehiculo() { 
    } 

    public Vehiculo(int id, Integer placa, String marca, String color, Integer anio, String modelo, boolean agencia) { 
     this.id = id; 
     this.placa = placa; 
     this.marca = marca; 
     this.color = color; 
     this.anio = anio; 
     this.modelo = modelo; 
     this.agencia = agencia; 
    } 

    //Metodos para acceder a la BD// 
    //INSERT 
    public void insertVehiculo() throws SQLException { 

     Connection conn = null; 
     PreparedStatement pstmt = null; 

     ConexionJDBC getConn = new ConexionJDBC(); 


     try { 
      conn = getConn.getConnection(); 

      String sql = "INSERT INTO vehiculo(placa, marca, color, modelo, anio, agencia) VALUES (?,?,?,?,?,?)"; 
      pstmt = conn.prepareStatement(sql); 


      pstmt.setInt(1, placa); 
      pstmt.setString(2, marca); 
      pstmt.setString(3, color); 
      pstmt.setString(4, modelo); 
      pstmt.setInt(5, anio); 
      pstmt.setBoolean(6, agencia); 


      pstmt.executeUpdate(); 


     } catch (SQLException e) { 
      System.out.println("Exception" + e); 
     } finally { 
      getConn.closeConnection(conn); 
      getConn.closeStatement(pstmt); 
     } 

    } 

    //SELECT 
    public List seleccionar() throws SQLException { 

     Connection conn = null; 
     PreparedStatement pstm = null; 
     ResultSet rs = null; 

     ConexionJDBC getConn = new ConexionJDBC(); 


     List<Vehiculo> resultado = new ArrayList(); 

     try { 


      conn = getConn.getConnection(); 

      String sql = "SELECT * FROM vehiculo"; 
      pstm = conn.prepareStatement(sql); 

      rs = pstm.executeQuery(); 



      while (rs.next()) { 

       Vehiculo vehiculo = new Vehiculo(); 

       vehiculo.setPlaca(rs.getInt("placa")); //Importante usar las comillas IMPORTANTISIMO 
       vehiculo.setMarca(rs.getString("marca")); 
       vehiculo.setModelo(rs.getString("modelo")); 
       vehiculo.setColor(rs.getString("color")); 
       vehiculo.setId(rs.getInt("id")); 

       /* Antes se hacia asi: 
       placa = rs.getInt(placa); 
       marca = rs.getString(marca); 
       modelo = rs.getString(modelo); 
       color = rs.getString(color); 
       */ 
       resultado.add(vehiculo); 

      } 



     } catch (SQLException e) { 
     } finally { 
      getConn.closeConnection(conn); 
      getConn.closeResultset(rs); 
      getConn.closeStatement(pstm); 
     } 
     return resultado; 

    } 

    //Eliminar 
    public void eliminarVehiculo(int id) throws SQLException { 

     Connection conn = null; 
     PreparedStatement pstmt = null; 

     ConexionJDBC getConn = new ConexionJDBC(); 

     try { 
      String sql = "Delete from vehiculo where id=" + id; 

      conn = getConn.getConnection(); 
      pstmt = conn.prepareStatement(sql); 
      pstmt.executeUpdate(); 


     } catch (Exception ex) { 
     } finally { 
      getConn.closeConnection(conn); 
      getConn.closeStatement(pstmt); 
     } 

    } 

    //UPDATE 
    public void actualizarVehiculo(int id) throws SQLException { 

     Connection conn = null; 
     PreparedStatement pstm = null; 
     ConexionJDBC getConn = new ConexionJDBC(); 

     try { 
      conn = getConn.getConnection(); 

      String sql = "UPDATE vehiculo SET placa=?, marca=?, color=?, modelo=?, anio=?, agencia=? WHERE id=" + id; 

      pstm.setInt(1, placa); 
      pstm.setString(2, marca); 
      pstm.setString(3, color); 
      pstm.setString(4, modelo); 
      pstm.setInt(5, anio); 
      pstm.setBoolean(6, agencia); 

      pstm.executeUpdate(); 

     } catch (SQLException e) { 
     } finally { 
      getConn.closeConnection(conn); 
      getConn.closeStatement(pstm); 
     } 


    } 

    //Select por ID 
    public List<Vehiculo> seleccionarPorID(int id) throws SQLException { 

     Connection conn = null; 
     PreparedStatement pstm = null; 
     ResultSet rs = null; 

     ConexionJDBC getConn = new ConexionJDBC(); 

     List<Vehiculo> resultado = new ArrayList(); 


     try { 

      conn = getConn.getConnection(); 

      String sql = "Select * from vehiculo where id=" + id; 
      pstm = conn.prepareStatement(sql); 

      rs = pstm.executeQuery(); 

      while (rs.next()) { 
       Vehiculo vehiculo = new Vehiculo(); 

       vehiculo.setId(rs.getInt("id")); 
       vehiculo.setPlaca(rs.getInt("placa")); //Importante usar las comillas IMPORTANTISIMO 
       vehiculo.setMarca(rs.getString("marca")); 
       vehiculo.setModelo(rs.getString("modelo")); 
       vehiculo.setColor(rs.getString("color")); 
       vehiculo.setId(rs.getInt("id")); 

       resultado.add(vehiculo); 
      } 


     } catch (SQLException e) { 
     } finally { 
      getConn.closeConnection(conn); 
      getConn.closeResultset(rs); 
      getConn.closeStatement(pstm); 
     } 

     return resultado; 

    } 
} 
+0

你的問題是什麼? – pL4Gu33 2014-10-29 18:06:34

+0

如何使用中的數據庫加載數據? – ricardoorellana 2014-10-29 18:12:07

回答

0

謝謝RaSh,謝謝dansouza。

我找到了一種方法來填充<h:inputText>與從數據庫中獲得的值,這很容易。

在ManagedBean只需創建一個方法:

//Fill the inputText with values 
    public void loadFields(int id, int placa, String marca, String modelo, String color, boolean agencia, int anio) { 

     //Filling values into the form 

     setId(id); 
     setMarca(marca); 
     setPlaca(placa); 
     setModelo(modelo); 
     setColor(color); 
     setAgencia(agencia); 
     setAnio(anio); 
    } 

然後在.xhtml頁面:

<h:commandButton value="Actualizar vehiculo" action="#{vehiculoMB.cargarCampos(vehi.id, vehi.placa, vehi.marca, vehi.modelo, vehi.color, vehi.agencia, vehi.anio)}" > 

        </h:commandButton> 

我知道這是不是最好的做法,但我學習JSF呢。

希望這可以幫助任何需要它的人。

0

我覺得你有2個錯誤的位置:
1. seleccionarPorID應該返回字符串,就不一一列舉。
2.您應該編寫方法getSeleccionar(),該方法將返回List<Vehiculo>或Vehiculo []。

相關問題