1
 <h:form id="login_frm">
                <h2>Login:</h2>
                <table width="250" border="0" cellspacing="0" cellpadding="2">
                    <tr>
                        <td colspan="2">
                            <h:message for="leagueName" />
                            <p></p>
                            <h:message for="leagueNation" />
                            <p></p>
                            <h:message for="leagueSize" />
                            <p></p>
                            <h:message for="leagueLogo" />
                            <p></p>
                        </td>
                    </tr>

                        <td><h:outputText value="League Name : "/></td>
                        <td><h:inputText id="leagueName" value="#{DBConnection.leagueName}" required="true" requiredMessage="League Name required" /></td>

                    <tr>
                        <td><h:outputText value="League Nation : "/></td>
                        <td><h:inputText id="leagueNation" value="#{DBConnection.leagueNation}" required="true" requiredMessage="League Nation required!" /></td>
                    </tr>
                    <tr>
                        <td><h:outputText value="League Size : "/></td>
                        <td><h:inputText id="leagueSize" value="#{DBConnection.leagueSize}" required="true" requiredMessage="League Size required" /></td>
                    </tr>
                    <tr>
                        <td><h:outputText value="League Logo : "/></td>
                        <td><h:inputText id="leagueLogo" value="#{DBConnection.leagueLogo}" required="true" requiredMessage="League Logo required!" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <h:commandButton actionListener="#{DBConnection.insertLeague()}" value="Insert" type="submit"/></td>
                    </tr>
                </table>
            </h:form>

Here is my method in the DBConnection class

package database;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import java.sql.*;
import java.util.*;

@ManagedBean(name="DBConnection")
@RequestScoped
public class DBConnection {
    private String username;
    private String password;
    private String dbusername;

    public String getDbpassword() {
        return dbpassword;
    }
    public String getDbusername() {
        return dbusername;
    }

    private String leagueName;
    private String leagueNation;
    private int leagueSize;
    private String leagueLogo;

    public String getLeagueName() {
        return leagueName;
    }

    public void setLeagueName(String leagueName) {
        this.leagueName = leagueName;
    }

    public String getLeagueNation() {
        return leagueNation;
    }

    public void setLeagueNation(String leagueNation) {
        this.leagueNation = leagueNation;
    }

    public int getLeagueSize() {
        return leagueSize;
    }

    public void setLeagueSize(int leagueSize) {
        this.leagueSize = leagueSize;
    }

    public String getLeagueLogo() {
        return leagueLogo;
    }

    public void setLeagueLogo(String leagueLogo) {
        this.leagueLogo = leagueLogo;
    }



    private String dbpassword;
    Connection con;
    Statement ps;
    ResultSet rs;
    String SQL_Str;

    public void dbData(String UName)
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/footballdb","root","");
            ps = con.createStatement();
            SQL_Str="Select * from users where user_name like ('" + UName +"')";
            rs=ps.executeQuery(SQL_Str);
            rs.next();
            dbusername=rs.getString(2).toString();
            dbpassword=rs.getString(4).toString();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            System.out.println("Exception Occur :" + ex);
        }
    }
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    public String checkValidUser()
    {
        dbData(username);

        if(username.equalsIgnoreCase(dbusername))
        {

            if(password.equals(dbpassword))
                return "home.xhtml";
            else
            {
                return "invalid";
            }
        }
        else
        {
            return "invalid";
        }
    }


    List<Player> players = new ArrayList<Player>();

    public List<Player> getPlayers() {
        players = displayPlayers();
        return players;
    }

    public void setPlayers(List<Player> players) {
        this.players = players;
    }

    public String insertLeague(String leagueName, String leagueNation, String leagueSize, String leagueLogo) {
        try
        {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/footballdb","root","");
            ps = con.createStatement();
            SQL_Str="INSERT INTO league(LeagueName, LeagueNation, LeagueSize, LeagueLogo) VALUES('"+leagueName+"', '"+leagueNation+"','"+leagueSize+"','"+leagueLogo+"')";
            ps.executeQuery(SQL_Str);
            return "displayLeagues.xhtml";
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            System.out.println("Exception Occur :" + ex);
            return "displayLeagues.xhtml";
        }
    }
}

As you can see I do have the method in my class, I have all the required getter and setter methods and I have tried it with both action and actionListener on the insertLeague page. Anyone know why this is happening, its really confusing me.

Also I have tried referencing the method with and without parenthesis

StackTrace: https://pastebin.com/613dJYUp

taygetos
  • 3,005
  • 2
  • 21
  • 29
TiernO
  • 427
  • 6
  • 20
  • would you please add error stack trace? – Mohsen Dec 06 '18 at 23:24
  • @Spara Done :) Added it via pastebin so its not too difficult to navigate through – TiernO Dec 06 '18 at 23:27
  • insertLeague has 4 parameters but you didn't pass anything in your jsf file – Mohsen Dec 06 '18 at 23:28
  • @Spara I passed them in and it doesnt work, when i click the "insert button" nothing happens – TiernO Dec 06 '18 at 23:31
  • how did you passed them? I think you should pass like this `#{DBConnection.insertLeague(DBConnection.leagueName, DBConnection.leagueNation, DBConnection.leagueSize, DBConnection.leagueLogo)}` – Mohsen Dec 06 '18 at 23:33
  • @Spara Yes i did that, the error in the console says Can not issue data manipulation statements with executeQuery() – TiernO Dec 06 '18 at 23:35
  • 1
    you should use `executeUpdate()` instead of `executeQuery()` – Mohsen Dec 06 '18 at 23:37
  • @Spara It works thank you! How do I make you you get the solution to the question? – TiernO Dec 06 '18 at 23:44
  • Just vote up my answer and make it as an accepted answer. for more information visit https://stackoverflow.com/help/someone-answers – Mohsen Dec 06 '18 at 23:46
  • If you couldn't find visit https://meta.stackexchange.com/tour – Mohsen Dec 06 '18 at 23:53
  • 1
    Hi, 1: stacktraces (like all code) should be posted inline, not in pastebin or the likes. 2: read [ask] and [mcve] 3: Please read https://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao – Kukeltje Dec 07 '18 at 07:44
  • 1
    Possible duplicate of [commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated](https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value) – Kukeltje Dec 07 '18 at 09:13

1 Answers1

1

You should pass parameters to insertLeague method like this :

#{DBConnection.insertLeague(DBConnection.leagueName, DBConnection.leagueNation, DBConnection.leagueSize, DBConnection.leagueLogo)}

And also for update query you should use executeUpdate() instead of executeQuery()

Mohsen
  • 4,536
  • 2
  • 27
  • 49
  • Bad 'design'... all these are already present in the jsf 'controller'. OP should split the code up in controller, service and dao and also look at https://stackoverflow.com/questions/8463178/what-to-use-managed-beans-backing-beans-or-entity-beans and https://stackoverflow.com/questions/10301363/jpa-entity-as-jsf-bean – Kukeltje Dec 07 '18 at 09:02