R
R
random2015-04-14 05:42:24
Java
random, 2015-04-14 05:42:24

Primefaces + MySQL?

Good morning, I want to make sure that when you click on the Add button, the data is written to the database and displayed in the table with native JDBC (without hibernate, spring).
Example:
a31689a2240d445a988cb1046249c5eb.PNG645ab12dbab146508ad37225ab223068.PNG
My
DAO sketches:

public static void insert() throws SQLException {
        Statement st = null;
        String sql = "INSERT INTO employee" + "(name, salary) " + "VALUES" + "('test', 12.0)";

        try {
            st = Database.getConnection().createStatement();
            st.executeUpdate(sql);
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

beans:
public void insertData() throws SQLException {
       //......
   }

.xhtml:
<p:panelGrid columns="2">
        <h:outputLabel  value="Name: " />
        <p:inputText value="#{emploee.name}"/>
        <h:outputLabel  value="Salary: " />
        <p:inputText  value="#{emploee.salary}"/>
        <f:facet name="footer">
            <h:commandButton value="Add" action="#{emploee.insertData()}"/>
        </f:facet>
    </p:panelGrid>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
random, 2015-04-14
@demon123

I got it like this.
DAO:

public static void insert(String aName, double aSalary) throws SQLException {
        PreparedStatement ps = null;
        try {
            ps = Database.getConnection().prepareStatement("INSERT INTO table (name, salary) VALUES (?, ?)");
            ps.setString(1,aName);
            ps.setDouble(2, aSalary);
            ps.executeUpdate();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

public void insertData() throws SQLException {
       EmployeeDAO.insert(this.name, this.salary);
   }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question