Advertisement
Advertisement
| 01.15.2008 at 08:26AM PST, ID: 23084400 | Points: 500 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 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: 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: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: |
// Table ADMIN:
// Username nvarchar(20) PK
// Password nvarchar(20)
**** Entity bean: Admin ****
@Entity
@Table(name = "Admin", schema = "dbo", catalog = "TungaRestaurant", uniqueConstraints = {})
public class Admin implements java.io.Serializable {
private String username;
private String password;
public Admin(String username, String password) {
this.username = username;
this.password = password;
}
@Id
@Column(name = "Username", unique = true, nullable = false, insertable = true, updatable = true, length = 20)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "Password", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
*** Session Bean to manage Admin Entity: AdminBean ***
@Stateless
public class AdminBean implements Serializable, AdminMan, AdminBeanLocal, AdminBeanRemote {
@PersistenceContext
EntityManager em;
public int delete(Admin admin) {
return delete (admin.getUsername());
}
public int delete(String usr) {
int retVal = 0;
Admin admin = em.find(Admin.class, usr);
try
{
em.remove(admin);
}
catch (Exception ex)
{
retVal = -1;
}
return retVal;
}
public Admin findAdmin(String usr) {
return em.find(Admin.class, usr);
}
public int save(Admin admin) {
int retVal = 0;
try {
Admin dbadmin = em.find(Admin.class, admin.getUsername());
if (dbadmin == null)
em.persist(admin);
else
{
dbadmin.setPassword(admin.getPassword());
em.flush();
}
} catch (Exception ex) {
retVal = -1;
}
return retVal;
}
}
*** Interface AdminMan ****
public interface AdminMan {
/*
* Return an the Admin with username
* @param String username
* @return a tunga.ejb.Admins object
*/
public Admin findAdmin(String usr);
/*
* Save the Admin's information to database, if the Admin is existed, the info
* will be update, if not, a new Admin will be inserted to database.
* @param tunga.ejb.Admin the Admin object that holds information
* @return 0 if ok, <0 if not
*/
public int save(Admin admin);
/*
* Delete the Admin from the database
* @param tunga.ejb.Admin the Admin object that holds information
* @return 0 if ok, <0 if not
*/
public int delete(Admin admin);
/*
* Delete the Admin from the database
* @param int the Admin's username
* @return 0 if ok, <0 if not
*/
public int delete(String usr);
}
*********************************
The Client:
**** File: jndi.properties ****
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
***** File: log4j.properties *****
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug, stdout
**** Class test Client
public class TestEJBClient {
public static void main(String[] args) {
Context context;
try
{
context = new InitialContext();
AdminMan adminMan = (AdminMan)context.lookup("AdminBean");
Admin admin = new Admin("admin","123");
adminMan.save(admin);
} catch (NamingException e)
{
e.printStackTrace();
}
}
}
|