Accessing MySQL database through EJB and JPA entity and Assigning Security Control
Posted by: Victor Ronda
Date: March 12, 2013 04:52AM

Dear All. Good day! I trust this catches you well.

I am developing an EJB application, a project here in my company. Because this is the very first time we work with EJBs I can't help but have 2 important questions, about which your kind assistance is highly appreciated.

(1) How can I insert data in database tables by calling the EJB from a Java SE client main method? By way of test I wrote a simple EJB and Java code. Here is the snippet:

@Stateless
public class insertRecordBean implements insertRecordRemote {


@PersistenceContext(unitName = "EJBDB-ejbPU")
private EntityManager em;

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void createTest(int id, String name) {

em.getTransaction().begin();
Testejb ts = new Testejb();
ts.setTId(id);
ts.setNome(name);
em.persist(ts);

and Java client main method:

public class Main {
@EJB
private static insertRecordRemote insertRecordBean;

@Resource
static UserTransaction ut;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Establishing connection to the bean...");
try{


InitialContext initialContext = new InitialContext();
insertRecordBean = (insertRecordRemote) initialContext.lookup
("server.insertRecordRemote");

ut.begin();

int id = 2;
String nome = "Flavia";

insertRecordBean.createTest(id, nome);
ut.commit();

// End of code snippets

A call similar to the above results in a simple EJB that does not access mysql db. What is missing,please?


(2) Each Java client will access some tables on the mysql db, with some privileges. If they were accessing the database from, say, mysql command-line shell they would have to enter username and password. How can I do a similar authentication in the java client main method? so that a user enters his credentials before doing any operations?

Looking forward to hear from you,
Thanks a million,
Victor

Options: ReplyQuote


Subject
Written By
Posted
Accessing MySQL database through EJB and JPA entity and Assigning Security Control
March 12, 2013 04:52AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.