こんにちは皆さん、私はprimefacesライブラリのデータテーブルを持っています。これには、各セルを編集する機能を含めようとしています。ユーザーが行で「保存」ボタンを押すと、編集された値がデータベースに更新されますが、現在ユーザーがセルを編集すると、セルをクリックしてセルを2番目に変更しても、値は以前と同じになり、新しい値は保存されません。また、ユーザーが最後に保存を押したときの別の問題もありますデータベースに渡される行の値は常に Null ですが、これら 2 つの問題を解決するにはどうすればよいですか?
ここにxhtmlがあります
<p:dataTable id="dataTable" var="u" value="#{userBean.getUserList()}"
paginator="true" rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15,25"
editable="true" editMode="cell"
>
<p:column>
<!--
<p:ajax event="rowEdit" listener="{u.onEdit}" update=":form:messages" />
<p:ajax event="rowEditCancel" listener="{u.onCancel}" update=":form:messages" />
-->
<f:facet name="header">
User ID
</f:facet>
#{u.userID}
</p:column>
<p:column headerText="Name" >
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{u.name}" />
</f:facet>
<f:facet name="input">
<p:inputText id="NameInput" value="#{u.name}"
style="width:96%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Email">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{u.email}" />
</f:facet>
<f:facet name="input">
<p:inputText id="EmailInput" value="#{u.email}"
/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Address">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{u.address}" />
</f:facet>
<f:facet name="input">
<p:inputText id="AddressInput" value="#{u.address}"
/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<f:facet name="header">
Created Date
</f:facet>
#{u.created_date}
</p:column>
<p:column>
<f:facet name="header">
Delete
</f:facet>
<h:commandButton value="Delete" action="#{user.delete(u.userID)}" />
</p:column>
<p:column>
<f:facet name="header">
Save Edit
</f:facet>
<h:commandButton value="Save" action="#{user.editData(u.userID)}" />
</p:column>
</p:dataTable>
ここにバッキングビーンがありますが、現在これはデータベースの値でデータベースを更新するだけです
public void editData(long userID) {
System.out.println(name);
PreparedStatement ps = null;
Connection con = null;
if (userID != 0) {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
System.out.println(name);
String sql = "UPDATE user1 set name = '" + name + "', email = '" + email + "', address = '" + address + "' WHERE userId=" + userID;
ps = con.prepareStatement(sql);
int i = ps.executeUpdate();
if (i > 0) {
System.out.println("Row updated successfully");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
データブルはもともとデータベースから値を取得します
ありがとう
データベースからの値をデータテーブルに入力する方法は次のとおりです/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package richard.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import richard.test.User;
@ManagedBean(name = "userBean")
@SessionScoped
public class UserBean {
List<User> list;
PreparedStatement ps = null;
Connection con = null;
ResultSet rs = null;
public List<User> getList() {
return list;
}
public List<User> getUserList() {
list = new ArrayList<User>();
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
String sql = "select * from user1";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
User usr = new User();
usr.setUserID(rs.getLong("userId"));
usr.setName(rs.getString("name"));
usr.setEmail(rs.getString("email"));
usr.setAddress(rs.getString("address"));
usr.setCreated_date(rs.getDate("created_date"));
list.add(usr);
Map<Long, Boolean> checked = new HashMap<Long, Boolean>();
List<User> checkedItems = new ArrayList<User>();
for (User item : list) {
if (checked.get(item.getUserID()) != null) {
checkedItems.add(item);
usr.delete(usr.getUserID());
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
}
HERE は完全なユーザー Bean コードです
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package richard.test;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.component.UIColumn;
import javax.faces.event.ActionEvent;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.event.CellEditEvent;
import org.primefaces.event.RowEditEvent;
@ManagedBean
@RequestScoped
public class User {
List<User> list;
PreparedStatement ps = null;
Connection con = null;
ResultSet rs = null;
private long userID = 1;
private String name;
private String address;
private Date created_date;
private String email;
boolean editable;
public boolean isEditable() {
return editable;
}
public void setEditable(boolean editable) {
this.editable = editable;
}
public String editAction(User order) {
order.setEditable(true);
return null;
}
public long getUserID() {
return userID;
}
public void setUserID(long userID) {
this.userID = userID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getCreated_date() {
return created_date;
}
public void setCreated_date(Date created_date) {
this.created_date = created_date;
}
public String add() {
System.out.println("In add");
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
int i = 0;
if (userID != 0) {
PreparedStatement ps = null;
Connection con = null;
try {
System.out.println("about to add to db");
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
String sql = "INSERT INTO user1( name, email, address, created_date) VALUES(?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, email);
ps.setString(3, address);
if (created_date != null) {
String date = fmt.format(created_date);
Object obj = date;
if (obj == null) {
ps.setDate(4, null);
} else {
java.sql.Date dt = java.sql.Date.valueOf(new String(date));
ps.setDate(4, dt);
}
}
i = ps.executeUpdate();
System.out.println("Data Added Successfully");
} catch (Exception e) {
System.out.println(e);
} finally {
try {
con.close();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (i > 0) {
return "output";
} else {
return "invalid";
}
} else {
return "invalid";
}
}
public void delete(long userID) {
PreparedStatement ps = null;
Connection con = null;
if (userID != 0) {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
String sql = "DELETE FROM user1 WHERE userId=" + userID;
ps = con.prepareStatement(sql);
int i = ps.executeUpdate();
if (i > 0) {
System.out.println("Row deleted successfully");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void editData(long userID) {
PreparedStatement ps = null;
Connection con = null;
if (userID != 0) {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
System.out.println(name);
String sql = "UPDATE user1 set name = '" + name + "', email = '" + email + "', address = '" + address + "' WHERE userId=" + userID;
ps = con.prepareStatement(sql);
int i = ps.executeUpdate();
if (i > 0) {
System.out.println("Row updated successfully");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}