1

I'm having a bit of an issue with a project that I'm doing for my internship. I have to create a webpage that can receive data from the page and insert it into a database so that I can then show it through a table. I'm using JSF with primefaces, bootstrap and javascript and are trying to send the data over to a managed bean that will process it and insert it into the database.

Here's my xhtml page:

   <form id="CargarM">
                    <center>
                        <table class="table">
                            <thead>
                                <tr class="success">
                                    <th colspan="2">Fecha</th>
                                    <th>AUDIT</th>
                                    <th>TTRAN</th>
                                    <th>INPUT</th>
                                    <th>HORA INICIO</th>
                                    <th>HORA FIN</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td id="spanDate" colspan="2"></td>
                                    <td colspan="1"><input type="number" name="Audit" value="#{CargarController.audit}"/></td>
                                    <td><input type="number" name="Ttran" value="#{CargarController.ttran}"/></td>
                                    <td><input type="number" name="Input" value="#{CargarController.input}"/></td>
                                    <td><input type="time" id="HoraInicio" name="HoraInicio" value="#{CargarController.inicio}"/></td>
                                    <td><input type="time" id="HoraFin" name="HoraFin" value="#{CargarController.fin}"/></td>
                                </tr>        

                            </tbody>
                        </table>
                        <!--<button type="submit" class="btn btn-success">Cargar</button>-->
                        <p:button  onclick="#{CargarController.CargaDatosMar()}" value="Cargar"></p:button>
                        <p:button  class="btn btn-success">Resetear</p:button>
                        <a href="#menu-toggle" class="btn btn-success" id="menu-toggle">Toggle Menu</a>
                    </center>
                </form>

Users will be able to put in all the required information and it should send it over to the method CargarDatosMar in the managedbean CargarController, shown below:

   public void CargaDatosMar() throws Exception {
    System.out.println("Entre");
    if (audit != null) {
        System.out.println("Entre otra vez");
        Connection con;
        PreparedStatement sql = null;
        Conexion conn = new Conexion();
        con = conn.getConnection();
        try {

            dia = new Date().toString();
            sql = con.prepareStatement("INSERT INTO CierreMar VALUES ('(?)','(?)','(?)','(?)','(?)','(?)')");
            sql.setString(1, dia);
            sql.setString(2, audit);
            sql.setString(3, ttran);
            sql.setString(4, input);
            sql.setString(5, inicio);
            sql.setString(6, fin);

            int rs = sql.executeUpdate();
            System.out.println("Sali");

        } catch (SQLException e) {
            throw e;
        }
    }
}

I also created the required Getter and Setter methods

public String getDia() {
    return dia;
}

public void setDia(String dia) {
    this.dia = dia;
}

public String getAudit() {
    return audit;
}

public void setAudit(String audit) {
    this.audit = audit;
}

public String getTtran() {
    return ttran;
}

public void setTtran(String ttran) {
    this.ttran = ttran;
}

public String getInput() {
    return input;
}

public void setInput(String input) {
    this.input = input;
}

public String getInicio() {
    return inicio;
}

public void setInicio(Object inicio) {
    this.inicio = inicio.toString();
}

public String getFin() {
    return fin;
}

public void setFin(Object fin) {
    this.fin = fin.toString();
}

And lastly I'll show the method I use to create the connection with the SQL Server database that I'm using.

public class Conexion {

public static Connection getConnection(){
String username="BODCOMVE\\peferrer";
String password="";
String url="jdbc:sqlserver://PEFERRER\\SQLEXPRESS;databaseName=PasantiasBOD";
    try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection con = DriverManager.getConnection(url,username,password);
        return con;
    }catch(Exception ex){

        System.out.println("Error al establecer la conexion" + ex.getMessage());
        return null;

    }
}

public static void close(Connection con){
    try{
        con.close();
    }catch(Exception ex){

    }
}

}

And since I've already talked a lot I'll just get right down to it. I've been researching this for the last couple of days and I noticed that for some reason when I start my page the CargarDatosMar method gets called as soon as my page starts loading, this, of course means that the variables that it's trying to access are null and as thus it throws a NullPointer. This is the reason why I put the code in an if(audit!=null) since it was the only way to access the page. I also discovered this thanks to the print message I put up above that code.

I've been trying to figure out why the method keeps getting called when the page starts and how I can fix it, but sadly I can't find any problem like this no matter where I look. I've tried changing the onClick call I put on the submit button to instead send the data to the class through an Action inside of the xhtml form but it gave the same problem.

So as a summary: I don't know why the managebean runs before I even finish entering the page and of course once it runs it won't run it again. I think I read that this is actually something that ManageBeans are supposed to do but if that's the case then what should I do to prevent it from just running at the start and to actually let me enter, or can I use something else as a way to send the data to my database.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Flaze24
  • 29
  • 3
  • You can't have Expression Language ( = #{CargarController.CargaDatosMar()}) in any of the on... attributes. Only javascript goes there. I think you're looking for a 'p:commandButton action="#{CargarController.CargaDatosMar()}"'. Also you must use "h:form" instead of "form" in jsf. And you can't ever use "class", use "styleClass" instead. And you can't use "input", use "p:inputText" instead. All in all look for a jsf or primefaces tutorial. Jsf and primefaces are component based frameworks, so you should use their own tags instead of standard html tags – Jaqen H'ghar Oct 29 '16 at 08:47
  • All pf components are here: http://www.primefaces.org/showcase/ – Jaqen H'ghar Oct 29 '16 at 08:51
  • Well that really helped. Thank you so much. Though now I'm having a weird problem for some reason. I changed things like you told me but now when I try to upload the data into the database the system...does nothing, like glassfish's console doesn't show anything, not even the messages I asked the system to prin out so I could track its progress through the inserting method. I also put up inputNumber instead of inputText because I was using input type number before but I don't know if that might cause a problem. – Flaze24 Nov 02 '16 at 12:30

0 Answers0