Book of tasks on programming. Old version

 

 by Aliaksandr N. Prykhodzka

 

прямое включение, CD-ROM, path, floppy, объектный, then, абстрактный, sheet, assembler, NTFS, статический, database, object, переход, pointer, cycle, procedure
 

for valuable work you must have JavaScript (allow active content)

JSP. D.6. Nest of tasks. Session beans. EJB. J2EE. JSP

 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.6.1     Answer

(session stateless bean, transaction starts before operation and closed after operation at once) We have database containing table with name Tabl1. Table have column XX of type "integer number". Firstly, table is empty. Determine content of table after launching the following application a few times and inputting pair of values (1, A), (2, A), (3, A), (4, A), (5, A) into fields ("Input value:", "Input user name:").


Текст файла jsp1.jsp такой же как из задачи JSP.6.1.


Текст файла Interprise1Bean.java

package pr_c;

import java.rmi.*;
import javax.ejb.*;
import javax.naming.NamingException;
import java.sql.SQLException;
import javax.transaction.SystemException;
import javax.transaction.NotSupportedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.RollbackException;

public class Enterprise1Bean implements SessionBean {
SessionContext sessionContext;
String pp;

public void ejbCreate() {
}
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}

public void doDo() throws NamingException, SQLException,
SystemException, NotSupportedException, IllegalStateException,
SecurityException, HeuristicRollbackException,
HeuristicMixedException, RollbackException {

javax.transaction.UserTransaction utx;
javax.sql.DataSource dataSour;
java.sql.Connection Conn;
java.sql.Statement Stmt;

javax.naming.Context context = new javax.naming.InitialContext();

dataSour = (javax.sql.DataSource)
context.lookup("java:comp/env/jdbcDatabase1");
Conn = dataSour.getConnection();

Stmt = Conn.createStatement();

utx = sessionContext.getUserTransaction();

utx.begin();

java.sql.ResultSet rs = Stmt.executeQuery("select sum(XX) from Tabl1");
rs.next();
int jj = rs.getInt(1);
Stmt.executeUpdate("insert into Tabl1 (XX) values ("+Integer.toString(jj)+")");

utx.commit();

Stmt.close();
Conn.close();
}

public String getPp() {
return pp;
}

public void setPp(String pp) {
this.pp = pp;
try {
doDo();
}
catch (RollbackException ex) { }
catch (HeuristicMixedException ex) { }
catch (HeuristicRollbackException ex) { }
catch (SecurityException ex) { }
catch (IllegalStateException ex) { }
catch (NotSupportedException ex) { }
catch (SystemException ex) { }
catch (SQLException ex) { }
catch (NamingException ex) {
}
}

}

 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.6.2     Answer

(session stateless bean, transaction starts before operation and closed after operation at once) We have database containing table with name Tabl1. Table have column XX of type "integer number". Firstly, table is empty. Determine content of table after launching the application from task JSP.6.1 a few times and inputting pair of values (1, A), (2, B), (3, A), (4, B), (5, A) into fields ("Input value:", "Input user name:").


 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.6.3     Answer

(session stateless bean, transaction starts before operation and closed after operation at once) We have database containing table with name Tabl1. Table have column XX of type "integer number". Firstly, table have 3 records with values XX equal to 1, 2, 3. Determine content of table after launching the following application a few times and inputting pair of values (1, A), (2, A) into fields ("Input value:", "Input user name:").


Текст файла Interprise1Bean.java

package pr_c;

import java.rmi.*;
import javax.ejb.*;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.NotSupportedException;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.naming.*;


public class Enterprise1Bean implements SessionBean {

SessionContext sessionContext;
javax.transaction.UserTransaction utx;
javax.sql.DataSource dataSour;
java.sql.Connection Conn;
java.sql.Statement Stmt;
String pp;

public void ejbCreate() {
utx = sessionContext.getUserTransaction();
try {
utx.begin();
}
catch (SystemException ex) { }
catch (NotSupportedException ex) {
}

Context context = null;
try {
context = new javax.naming.InitialContext();
dataSour = (javax.sql.DataSource)
context.lookup("java:comp/env/jdbcDatabase1");
Conn = dataSour.getConnection();
Stmt = Conn.createStatement();
}
catch (NamingException ex1) { }
catch (SQLException ex) { }
}

public void ejbRemove() {
try {
utx.commit();
}
catch (SystemException ex) { }
catch (IllegalStateException ex) { }
catch (SecurityException ex) { }
catch (HeuristicRollbackException ex) { }
catch (HeuristicMixedException ex) { }
catch (RollbackException ex) {
}

try {
Stmt.close();
Conn.close();
} catch (SQLException ex1) { }
}

public void ejbActivate() {
}
public void ejbPassivate() {
}

public void setSessionContext(SessionContext Par_sessionContext) {
sessionContext = Par_sessionContext;
}

public void doInsert(String Par1) throws SQLException {
Stmt.executeUpdate("insert into Tabl1 (XX) values ("+Par1+")");
}

public void setPp(String Par_pp) {
this.pp = Par_pp;
try {
doInsert(Par_pp);
}
catch (SQLException ex) { }
}

public String getPp() {
return pp;
}

}

 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.6.4     Answer

(session stateless bean, transaction starts before operation and closed after operation at once) Application and database from task JSP.6.3. Determine content of table after launching the application a few times and inputting pair of values (1, A), (2, B) into fields ("Input value:", "Input user name:").


 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.6.5     Answer

(session stateless bean, transaction starts before creating bean and closed after his destruction) Condition of task is from JSP.6.1. Content of file jsp1.jsp is without changes. File Interprise1Bean.java is following. Question is the same.


Текст файла Interprise1Bean.java

package pr_c;

import java.rmi.*;
import javax.ejb.*;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.NotSupportedException;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.naming.*;


public class Enterprise1Bean implements SessionBean {

SessionContext sessionContext;
javax.transaction.UserTransaction utx;
javax.sql.DataSource dataSour;
java.sql.Connection Conn;
java.sql.Statement Stmt;
String pp;

public void ejbCreate() {
utx = sessionContext.getUserTransaction();
try {
utx.begin();
}
catch (SystemException ex) { }
catch (NotSupportedException ex) {
}

Context context = null;
try {
context = new javax.naming.InitialContext();
dataSour = (javax.sql.DataSource)
context.lookup("java:comp/env/jdbcDatabase1");
Conn = dataSour.getConnection();
Stmt = Conn.createStatement();
}
catch (NamingException ex1) { }
catch (SQLException ex) { }
}

public void ejbRemove() {
try {
utx.commit();
}
catch (SystemException ex) { }
catch (IllegalStateException ex) { }
catch (SecurityException ex) { }
catch (HeuristicRollbackException ex) { }
catch (HeuristicMixedException ex) { }
catch (RollbackException ex) {
}

try {
Stmt.close();
Conn.close();
} catch (SQLException ex1) { }
}

public void ejbActivate() {
}
public void ejbPassivate() {
}

public void setSessionContext(SessionContext Par_sessionContext) {
sessionContext = Par_sessionContext;
}

public void doInsert(String Par1) throws SQLException {
Stmt.executeUpdate("insert into Tabl1 (XX) values ("+Par1+")");
}

public void setPp(String Par_pp) {
this.pp = Par_pp;
try {
doInsert(Par_pp);
}
catch (SQLException ex) { }
}

public String getPp() {
return pp;
}

}

 

 

Calculator

/ - division

\ - rest

S - sum of numbers from and to

P - multiply numbers from and to

D.6.6     Answer

(session stateless bean, transaction starts before creating bean and closed after his destruction) Condition of task is from JSP.6.2. Application is from JSP.6.5.


 

©   Aliaksandr Prykhodzka    1993 - 2007