My code works, delete method works, select method works, but the update method doesn't work because overwrite the same, here it is the code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.FacesException;
@ManagedBean
@SessionScoped
public class Teszt implements Serializable {
String msg;
int id;
String name;
int age;
String URL = "jdbc:mysql://IP-cím:Port/osszesito";
String USER = "osszesito";
String PASSWORD = "Password";
String DRIVER = "com.mysql.jdbc.Driver";
public Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DRIVER);
dbConnection = DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("Connection completed.");
} catch (SQLException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
System.out.println(cnfe.getMessage());
System.exit(-1);
}
return dbConnection;
}
public List < Teszt > selectTesztTable() throws SQLException {
ResultSet rs = null;
PreparedStatement pst = null;
Connection con = getDBConnection();
String stm = "select * from teszt";
List < Teszt > records = new ArrayList < Teszt > ();
try {
pst = con.prepareStatement(stm);
pst.execute();
rs = pst.getResultSet();
while (rs.next()) {
Teszt objectMeghiv = new Teszt();
objectMeghiv.setId(rs.getInt(1));
objectMeghiv.setName(rs.getString(2));
objectMeghiv.setAge(rs.getInt(3));
records.add(objectMeghiv);
}
return records;
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
rs.close();
pst.close();
con.close();
}
return records;
}
public void deleteOsszesito(Teszt t) {
Connection connection = getDBConnection();
PreparedStatement pst = null;
try {
String sql = "delete from teszt where id=?";
pst = connection.prepareStatement(sql);
pst.setInt(1, t.getId());
pst.executeUpdate();
pst.close();
connection.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void updateTable(Teszt t) throws SQLException {
List < TesztSetGet > list = new ArrayList < TesztSetGet > ();
Connection connection = null;
PreparedStatement pst = null;
ResultSet rs = null;
String sql = "update teszt set name=?, age=? where id=?";
TesztSetGet meghiv = new TesztSetGet();
try {
connection = getDBConnection();
pst = connection.prepareStatement(sql);
pst.setString(1, t.getName());
pst.setInt(2, t.getAge());
pst.setInt(3, t.getId());
System.out.println(t.getName() + " " + meghiv.age + " " + id);
pst.executeUpdate();
} catch (SQLException se) {
se.printStackTrace();
se.getMessage();
throw new FacesException(se);
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
throw new FacesException(e);
} finally {
if (pst != null) {
pst.close();
}
if (connection != null) {
connection.close();
}
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isEditable() {
return editable;
}
public void setEditable(boolean editable) {
this.editable = editable;
}
}
<?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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
>
<h:head>
<title>Teszt</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="style.css"/>
</h:head>
<ui:debug />
<h:form>
<p:fieldset legend="Teszt:">
<p:dataTable id="dataTable" value="#{Teszt.selectTesztTable()}" var="t" paginator="true" rows="15">
<p:column>
<f:facet name="header">ID:</f:facet>
<h:inputText value="#{t.id}"/>
</p:column>
<p:column>
<f:facet name="header">Name:</f:facet>
<h:inputText value="#{t.name}"/>
</p:column>
<p:column>
<f:facet name="header">Age:</f:facet>
<h:inputText value="#{t.age}" />
</p:column>
<p:column><f:facet name="header">DELETE</f:facet>
<p:commandButton value="DELETE" action="#{Teszt.deleteOsszesito(t)}" update="@form" />
</p:column>
<p:column><f:facet name="header">UPDATE</f:facet>
<p:commandButton value="Update" action="#{Teszt.updateTable(t)}" update="@form">
<f:ajax render="@all" execute="@form" />
</p:commandButton>
</p:column>
</p:dataTable>
</p:fieldset>
</h:form>
</html>
There are three fields. These fields are in inputText. When I modify the fields and i click update button, then the fields return to original state :(
Why?
Thank you very much