<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>
这是我在DBConnection类中的方法
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";
}
}
}
正如你所看到的,我的类中确实有这个方法,我有所有必需的getter和setter方法,我已经在insertLeague页面上用action和actionListener进行了尝试。任何人都知道为什么会发生这种事,这真的让我很困惑
此外,我还尝试过引用带括号和不带括号的的方法
StackTrace:https://pastebin.com/613dJYUp
您应该将参数传递给insertLeague
方法,如下所示:
#{DBConnection.insertLeague(DBConnection.leagueName, DBConnection.leagueNation, DBConnection.leagueSize, DBConnection.leagueLogo)}
对于更新查询,您应该使用executeUpdate()
而不是executeQuery()