简体   繁体   中英

JDBC mysql Setting and retrieving data from a database

any help would be greatly appreciated. Been stuck on this for a while and seem to be confusing myself more and more!

I have simple database with a table 'VowelCount' which has the columns (A, E, I, O and U) and one row underneath which stores how many of each vowel have been typed by a user.

The number of vowels needs to be updated each time the user inputs a new sentence and then the vowels for the current input, and all previous inputs need to be displayed.

I have figured out how to show the current vowels. But having trouble updating the data to the database table and retrieving it back again and storing it into a variable so that it can be added to the running total

If that makes any sense!

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++;
            } 
    }
} 

}

Here is the input xhtml screen:

<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>

Here is the xhtml output:

<?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>

You have to call the next() method on the ResultSet variable. The ResultSet in JDBC works as a kind of pointer and this pointer starts before the first result. Since you don't iterate over the result set, your probably getting some default value for int, this being value zero.

PS I would suggest having a simpler model for your table, like just vowelCountTable(letter, count). You can then restrict the domain of the letter column to only vogals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM