import com.fujitsu.shun.ShunConnection;
import com.fujitsu.shun.ShunPreparedKey;
import com.fujitsu.shun.common.ShunException;
/*** Update data using direct access key ***/
public class DirectAccessSample2 {
public static void main(String[] args) {
ShunConnection con = null;
ShunPreparedKey keystmt = null;
try
{
//Direct access key name
String sCoursKeyName = "key";
//Direct access key
String key = "0001";
//Update data
String updateCourseData =
"<course>"
+ " <id>0001</id>"
+ " <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>";
// Create 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");
//Create ShunPreparedKey object for updating data
keystmt = con.prepareUpdateKey(sCoursKeyName);
//Add the data to be updated
keystmt.add(key, updateCourseData);
//Update course information
keystmt.updateByKey();
System.out.println("Update complete");
keystmt.close();
con.close();
}
// Processing to perform if an error occurs while the application is running
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_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();
}
// Recovery procedure in the event of an error
finally {
try {
if(keystmt != null) keystmt.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_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_CONNECTION_TERMINATED:
System.out
.println("Error level :SHUN_ERROR_CONNECTION_TERMINATED");
break;
}
System.out.println("Error message:" + ex.getMessage());
ex.printStackTrace();
}
}
}
} |