任何帮助将不胜感激。被困在这一段时间,似乎越来越困惑自己了!
我有一个带有'VowelCount'表的简单数据库,它有一列(A,E,I,O和U),下面有一行,用于存储用户输入的每个元音的数量。
每次用户输入新句子然后输入当前输入的元音时,需要更新元音的数量,并且需要显示所有先前的输入。
我已经想出如何显示当前的元音。但是无法将数据更新到数据库表并再次检索它并将其存储到变量中以便可以将其添加到运行总计中
如果这有意义的话!
package managed_bean;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import java.sql.*;
@Named(value = "dataVowelBean")
@RequestScoped
public class dataVowelBean
{
private String name;
private int numberOfA;
private int numberOfE;
private int numberOfI;
private int numberOfO;
private int numberOfU;
private int totalA;
private int totalE;
private int totalI;
private int totalO;
private int totalU;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
numberOfA();
numberOfE();
numberOfI();
numberOfO();
numberOfU();
storeVowelsInDatabase();
}
public int getNumberOfA()
{
return numberOfA;
}
public int getNumberOfE()
{
return numberOfE;
}
public int getNumberOfI()
{
return numberOfI;
}
public int getNumberOfO()
{
return numberOfO;
}
public int getNumberOfU()
{
return numberOfU;
}
public int getTotalOfA()
{
return totalA;
}
public int getTotalOfE()
{
return totalE;
}
public int getTotalOfI()
{
return totalI;
}
public int getTotalOfO()
{
return totalO;
}
public int getTotalOfU()
{
return totalU;
}
public void storeVowelsInDatabase()
{
int countA;
int countE;
int countI;
int countO;
int countU;
try
{
DriverManager.registerDriver(new ());
Connection con;
con = DriverManager.getConnection(url, "user", "pass");
PreparedStatement stmtGet = con.prepareStatement("SELECT A, E, I, O,
U FROM vowelCountTable");
//countA = stmtGet.executeQuery();
ResultSet rs = stmtGet.executeQuery();
countA = (rs.getInt("A") + getNumberOfA());
totalA = countA;
countE = (rs.getInt("E") + getNumberOfE());
totalE = countE;
countI = (rs.getInt("I") + getNumberOfI());
totalI = countI;
countO = (rs.getInt("O") + getNumberOfO());
totalO = countO;
countU = (rs.getInt("U") + getNumberOfU());
totalU = countU;
PreparedStatement stmtSet = con.prepareStatement("UPDATE
vowelCountTable SET A = ?, E = ?, I = ?, O = ?, U = ?");
stmtSet.setInt(1, totalA);
stmtSet.setInt(2, totalE);
stmtSet.setInt(3, totalI);
stmtSet.setInt(4, totalO);
stmtSet.setInt(5, totalU);
stmtSet.executeUpdate();
stmtSet.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void numberOfA()
{
for (int i = 0; i < name.length(); i++)
{
if (name.charAt(i) == 'a')
{
numberOfA++;
}
}
}
private void numberOfE()
{
for (int i = 0; i < name.length(); i++)
{
if (name.charAt(i) == 'e')
{
numberOfE++;
}
}
}
private void numberOfI()
{
for (int i = 0; i < name.length(); i++)
{
if (name.charAt(i) == 'i')
{
numberOfI++;
}
}
}
private void numberOfO()
{
for (int i = 0; i < name.length(); i++)
{
if (name.charAt(i) == 'o')
{
numberOfO++;
}
}
}
private void numberOfU()
{
for (int i = 0; i < name.length(); i++)
{
if (name.charAt(i) == 'u' || name.charAt(i) == 'U')
{
numberOfU++;
}
}
}
}
这是输入xhtml屏幕:
<h:head>
<title>Vowel Count Example</title>
</h:head>
<h:body>
<h1>This is a vowel count example</h1>
<h:form>
<h2> Please type your name in the box and I will tell you how many vowels were used: </h2>
<h:inputText value="#{dataVowelBean.name}" /> <p> </p>
<h:commandButton type="submit" value="Submit" action="databaseVowelCount" />
</h:form>
</h:body>
这是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:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Vowel Count Output</title>
</h:head>
<h:body>
<h1>Vowel count output</h1>
<h2>You typed: <h:outputText value="#{dataVowelBean.name}" /></h2>
<p> The number of A's: <h:outputText value="#{dataVowelBean.numberOfA}" /></p>
<p> The number of E's: <h:outputText value="#{dataVowelBean.numberOfE}" /></p>
<p> The number of I's: <h:outputText value="#{dataVowelBean.numberOfI}" /></p>
<p> The number of O's: <h:outputText value="#{dataVowelBean.numberOfO}" /></p>
<p> The number of U's: <h:outputText value="#{dataVowelBean.numberOfU}" /></p>
<p> The total number of A's: <h:outputText value="#{dataVowelBean.totalOfA}" /></p>
<p> The total number of E's: <h:outputText value="#{dataVowelBean.totalOfE}" /></p>
<p> The total number of I's: <h:outputText value="#{dataVowelBean.totalOfI}" /></p>
<p> The total number of O's: <h:outputText value="#{dataVowelBean.totalOfO}" /></p>
<p> The total number of U's: <h:outputText value="#{dataVowelBean.totalOfU}" /></p>
</h:body>
答案 0 :(得分:0)
您必须在ResultSet变量上调用next()方法。 JDBC中的ResultSet作为一种指针运行,该指针在第一个结果之前启动。 由于您不会迭代结果集,因此可能会获得int的一些默认值,这是零值。
P.S。我建议你的桌子有一个更简单的模型,比如vowelCountTable(字母,计数)。然后,您可以将字母列的域限制为仅为vogals。