Académique Documents
Professionnel Documents
Culture Documents
1 of 5
http://ljug.cofares.net/2013/09/simple-crud-using-jsf-primeface-and.html
Follow by Email
Simple CRUD Using JSF (standard) and MySQL with netbeans and Tomcat
The Data
The database (an example)
Translate
Select Language
create database TestDB;
use TestDB;
2015 (4)
The handy ORM
2014 (55)
2013 (29)
dcembre (8)
novembre (5)
octobre (2)
septembre (9)
Simple CRUD
Using JSF
(standard) and
MySQL with
ne...
PrimeFaces EL
Extensions for
UI
Authorization
Java EE7 :
Glassfish 4
Java Server Face
Apache OpenJPA
project
Configuring
Security in
Glassfish v3
Creating a ...
L'administration
scurise doit
tre active
pour ...
GlassFish Server
Open Source
Edition 4.0
Download
Jdbc glassfish
realm mysql
aot (2)
janvier (3)
}
public void setDob(Date dob) {
2012 (11)
this.dob = dob;
28-07-2015 00:04
Lebanese Java User Group: Simple CRUD Using JSF (standard) and ...
2 of 5
http://ljug.cofares.net/2013/09/simple-crud-using-jsf-primeface-and.html
}
public String getEmail() {
return email;
java (9)
glassfish (6)
netbeans (5) j2ee (4) JSF
(3) git (3) mysql (3)
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [userid=" + userid + ", firstName=" + firstName
+ ", lastName=" + lastName + ", dob=" + dob + ", email="
+ email + "]";
}
}
1
2 package dao;
3
4 import entities.User;
5 import gm.GM;
6 import java.sql.Connection;
7 import java.sql.PreparedStatement;
8 import java.sql.ResultSet;
9 import java.sql.SQLException;
10 import java.sql.Statement;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.naming.NamingException;
14
15 /**
16 *
17 * @author pascalfares
18 */
19 public class UserDao {
20 public static void addUser(User user) throws SQLException, NamingException {
21
Connection connection = GM.getTestDB().getConnection();
22
PreparedStatement preparedStatement = connection
23
.prepareStatement("insert into users(firstname,lastname,dob,email) values (?, ?, ?, ? )");
24
// Parameters start with 1
25
preparedStatement.setString(1, user.getFirstName());
26
preparedStatement.setString(2, user.getLastName());
27
preparedStatement.setDate(3, new java.sql.Date(user.getDob().getTime()));
28
preparedStatement.setString(4, user.getEmail());
29
preparedStatement.executeUpdate();
30
31
32 }
33
34 public static void deleteUser(int userId) throws NamingException, SQLException {
35
Connection connection = GM.getTestDB().getConnection();
36
PreparedStatement preparedStatement = connection
37
.prepareStatement("delete from users where userid=?");
38
// Parameters start with 1
39
preparedStatement.setInt(1, userId);
40
preparedStatement.executeUpdate();
41
42 }
43
44 public static void updateUser(User user) throws NamingException, SQLException {
45
Connection connection = GM.getTestDB().getConnection();
46
PreparedStatement preparedStatement = connection
47
.prepareStatement("update users set firstname=?, lastname=?, dob=?, email=?" +
48
"where userid=?");
49
// Parameters start with 1
50
preparedStatement.setString(1, user.getFirstName());
51
preparedStatement.setString(2, user.getLastName());
52
preparedStatement.setDate(3, new java.sql.Date(user.getDob().getTime()));
53
preparedStatement.setString(4, user.getEmail());
54
preparedStatement.setInt(5, user.getUserid());
55
preparedStatement.executeUpdate();
28-07-2015 00:04
Lebanese Java User Group: Simple CRUD Using JSF (standard) and ...
3 of 5
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 }
98
http://ljug.cofares.net/2013/09/simple-crud-using-jsf-primeface-and.html
}
public static List<User> getAllUsers() throws NamingException, SQLException {
List<User> users = new ArrayList<User>();
Connection connection = GM.getTestDB().getConnection();
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from users");
while (rs.next()) {
User user = new User();
user.setUserid(rs.getInt("userid"));
user.setFirstName(rs.getString("firstname"));
user.setLastName(rs.getString("lastname"));
user.setDob(new java.util.Date(rs.getDate("dob").getTime()));
user.setEmail(rs.getString("email"));
users.add(user);
}
return users;
}
public static User getUserById(int userId) throws NamingException, SQLException {
User user = new User();
Connection connection = GM.getTestDB().getConnection();
PreparedStatement preparedStatement = connection.
prepareStatement("select * from users where userid=?");
preparedStatement.setInt(1, userId);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
user.setUserid(rs.getInt("userid"));
user.setFirstName(rs.getString("firstname"));
user.setLastName(rs.getString("lastname"));
user.setDob(rs.getDate("dob"));
user.setEmail(rs.getString("email"));
}
return user;
}
The UI
The ManagedBean
Create the UserControl managedBean
1 package mb;
2
3 import dao.UserDao;
4 import entities.User;
5 import java.sql.SQLException;
6 import java.util.List;
7 import javax.faces.bean.ManagedBean;
8 import javax.faces.bean.SessionScoped;
9 import javax.naming.NamingException;
10
11 /**
12 * A minimal Managed Bean for CRUD 2 attributes
13 * one for current User
14 * one for the list of users
15 * @author pfares
16 */
17 @ManagedBean
18 @SessionScoped
19 public class UserControl {
20 private User selectedUser;
21 private List<User> lusers;
22 /**
23 * Creates a new instance of UserControl
24 */
25 public UserControl() {
26
selectedUser=new User();
28-07-2015 00:04
Lebanese Java User Group: Simple CRUD Using JSF (standard) and ...
4 of 5
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 }
http://ljug.cofares.net/2013/09/simple-crud-using-jsf-primeface-and.html
/**
* @return the selectedUser
*/
public User getSelectedUser() {
return selectedUser;
}
/**
* @param selectedUser the selectedUser to set
*/
public void setSelectedUser(User selectedUser) {
this.selectedUser = selectedUser;
}
/**
* @return the lusers
*/
public List<User> getLusers() throws NamingException, SQLException {
lusers=UserDao.getAllUsers();
return lusers;
}
/**
* @param lusers the lusers to set
*/
public void setLusers(List<User> lusers) {
this.lusers = lusers;
}
public String create() throws SQLException, NamingException {
UserDao.addUser(selectedUser);
return "index";
}
28-07-2015 00:04
Lebanese Java User Group: Simple CRUD Using JSF (standard) and ...
5 of 5
http://ljug.cofares.net/2013/09/simple-crud-using-jsf-primeface-and.html
18
requiredMessage="Please enter your email address."
19
validatorMessage="Invalid email format"
20
value="#{userControl.selectedUser.email}">
21
<f:validateRegex
22
pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" />
23
</h:inputText>
24
<h:message for="email" />
25
<h:inputText value="#{userControl.selectedUser.dob}" >
26
<f:convertDateTime pattern="dd-MM-yyyy" />
27
</h:inputText>
28
<h:commandButton action="#{userControl.create()}" value="Create" />
29
</h:form>
30
</f:view>
31 </h:body>
32 </html>
Accueil
28-07-2015 00:04