import com.fujitsu.shun.ShunConnection;
import com.fujitsu.shun.ShunPreparedRecordID;
import com.fujitsu.shun.ShunPreparedStatement;
import com.fujitsu.shun.ShunResultSet;
import com.fujitsu.shun.common.ShunException;
public class TranAppSample
{
public static void main(String[] args)
{
ShunConnection con = null;
ShunPreparedStatement pstmt = null;
ShunPreparedRecordID rid = null;
ShunResultSet rs = null;
try
{
// Set up a search expression and a return expression for searching for the course where the number of participants is to be increased
// Search expression
String sCourseQuery = "/course/name == 'Business negotiation'";
// Return expression
String sCourseReturn = "/";
//Used to get the ID of the record to be updated
String updateCourseRecordID = null;
//Update data
String updateCourseData =
"<course>"
+ " <name>Business negotiation</name>"
+ " <instructor>"
+ " <first-name>Max</first-name>"
+ " <last-name>Cameron</last-name>"
+ " </instructor>"
+ " <capacity>40</capacity>"
+ " <current-auditors>31</current-auditors>"
+ "</course>";
//Set up a search expression and a return expression for searching for the students to be added to the attendance information
// Search expression
String sStudentQuery = "/student/e-mail == 'mary\\.tompson@def\\.com'";
// Return expression
String sStudentReturn = "/";
//Used to get the ID of the record to be updated
String updateStudentRecordID = null;
//Update data
String updateStudentData =
"<student>"
+ " <first-name>Mary</first-name>"
+ " <last-name>Tompson</last-name>"
+ " <e-mail>mary.tompson@def.com</e-mail>"
+ " <course>Chinese language</course>"
+ " <course>Business negotiation</course>"
+ "</student>";
// Create the ShunConnection object
// Host name for the connection destination: DirSvr1
// Port number: 33101
// If the Shunsaku File name is shunsakuFile1
con = new ShunConnection("DirSvr1", 33101, "shunsakuFile1");
//Search for the course information and student information to be updated
// Specify a search expression and create the ShunPreparedStatement object
pstmt = con.prepareSearch(sCourseQuery, sCourseReturn);
// Execute the search and create the ShunResultSet object
rs = pstmt.executeSearch();
// Get the record ID for the course information obtained, and the XML document for that record
while (rs.next())
{
updateCourseRecordID = rs.getRecordID();
}
rs.close();
pstmt.close();
// Specify a search expression and create the ShunPreparedStatement object
pstmt = con.prepareSearch(sStudentQuery, sStudentReturn);
// Execute the search and create the ShunResultSet object
rs = pstmt.executeSearch();
// Get the record ID for the student information obtained, and the XML document for that record
while (rs.next())
{
updateStudentRecordID = rs.getRecordID();
}
rs.close();
pstmt.close();
// Turn off auto-commit mode
con.setAutoCommit(false);
// Update course information
rid = con.prepareUpdateRecordID();
// Add update data
rid.add(updateCourseRecordID, updateCourseData);
// Update data if the data was set up successfully
if (0 < rid.getCount())
{
rid.updateByRecordID();
System.out.println("Finished updating course information");
}
rid.close();
// Update student information
rid = con.prepareUpdateRecordID();
// Add update data
rid.add(updateStudentRecordID, updateStudentData);
// Update data if the data was set up successfully
if (0 < rid.getCount())
{
rid.updateByRecordID();
System.out.println("Finished updating student information");
}
rid.close();
// Commit the updates made so far
con.commit();
con.close();
}
// Processing performed when an error occurs during execution of the application
catch (ShunException ex)
{
//Roll the transaction back
if (con != null)
{
try
{
con.rollback();
}
catch (ShunException e)
{
int errorLevel = ex.getErrLevel();
switch( errorLevel ) {
case ShunException.SHUN_ERROR :
System.out.println("Error level: SHUN_ERROR");
break;
case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK :
System.out.println("Error level: SHUN_ERROR_TRANSACTION_ROLLEDBACK");
break;
case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
System.out.println("Error level: SHUN_ERROR_CONNECTION_TERMINATED");
break;
}
System.out.println("Error message: " + ex.getMessage());
ex.printStackTrace();
}
}
int errorLevel = ex.getErrLevel();
switch( errorLevel ) {
case ShunException.SHUN_ERROR :
System.out.println("Error level: SHUN_ERROR");
break;
case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK :
System.out.println("Error level: SHUN_ERROR_TRANSACTION_ROLLEDBACK");
break;
case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
System.out.println("Error level: SHUN_ERROR_CONNECTION_TERMINATED");
break;
}
System.out.println("Error message: " + ex.getMessage());
ex.printStackTrace();
}
catch (Exception ex)
{
System.out.println("Error message: " + ex.getMessage());
ex.printStackTrace();
}
finally
{
try
{
if (rs != null)
rs.close();
}
catch (ShunException ex)
{
int errorLevel = ex.getErrLevel();
switch( errorLevel ) {
case ShunException.SHUN_ERROR :
System.out.println("Error level: SHUN_ERROR");
break;
case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK :
System.out.println("Error level: SHUN_ERROR_TRANSACTION_ROLLEDBACK");
break;
case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
System.out.println("Error level: SHUN_ERROR_CONNECTION_TERMINATED");
break;
}
System.out.println("Error message: " + ex.getMessage());
ex.printStackTrace();
}
try
{
if (pstmt != null)
pstmt.close();
}
catch (ShunException ex)
{
int errorLevel = ex.getErrLevel();
switch( errorLevel ) {
case ShunException.SHUN_ERROR :
System.out.println("Error level: SHUN_ERROR");
break;
case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK :
System.out.println("Error level: SHUN_ERROR_TRANSACTION_ROLLEDBACK");
break;
case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
System.out.println("Error level: SHUN_ERROR_CONNECTION_TERMINATED");
break;
}
System.out.println("Error message: " + ex.getMessage());
ex.printStackTrace();
}
try
{
if (rid != null)
rid.close();
}
catch (ShunException ex)
{
int errorLevel = ex.getErrLevel();
switch( errorLevel ) {
case ShunException.SHUN_ERROR :
System.out.println("Error level: SHUN_ERROR");
break;
case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK :
System.out.println("Error level: SHUN_ERROR_TRANSACTION_ROLLEDBACK");
break;
case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
System.out.println("Error level: SHUN_ERROR_CONNECTION_TERMINATED");
break;
}
System.out.println("Error message: " + ex.getMessage());
ex.printStackTrace();
}
try
{
if (con != null)
con.close();
}
catch (ShunException ex)
{
int errorLevel = ex.getErrLevel();
switch( errorLevel ) {
case ShunException.SHUN_ERROR :
System.out.println("Error level: SHUN_ERROR");
break;
case ShunException.SHUN_ERROR_TRANSACTION_ROLLEDBACK :
System.out.println("Error level: SHUN_ERROR_TRANSACTION_ROLLEDBACK");
break;
case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
System.out.println("Error level: SHUN_ERROR_CONNECTION_TERMINATED");
break;
}
System.out.println("Error message: " + ex.getMessage());
ex.printStackTrace();
}
}
}
} |