1 在平台中使用KSQL——推荐
在X5的Java开发中,可以使用KSQL进行数据操作。KSQL与SQL在语法上相近,区别在于KSQL操作的是“概念”,SQL操作的是“数据库表”。KSQL使用平台事务,因此不需要关心事务。
在Java中,使用KSQL进行查询数据操作,使用select方法,例如:
import Java.util.Iterator;
import com.justep.system.data.KSQL;
import com.justep.system.data.Row;
public class DynamicFun {
static public String getCustomerMaxCode(){
try {
//定义查询ksql,在KSQL中必须使用别名,概念标识和关系标识是大小写敏感的
String kSql="select max(X5_Customer.fCustomerCode) as fCustomerCode from X5_Customer X5_Customer";
//执行select取得数据。Table是返回的数据集
com.justep.system.data.Table table = KSQL.select(kSql, null, "/x5demo/baseDemo/data", null);
Iterator<Row> rows = table.iterator();
Row row;
row = rows.next();
int result = Integer.parseInt(row.getString("fCustomerCode")) + 1;
return "" + result;
} catch (Exception e) {
throw new RuntimeException (e.getMessage());
}
}
}
在Java中,使用KSQL进行更新数据操作,使用executeUpdate方法,例如:
public static void costRequestProcessAfterFinish() {
try {
String key = com.justep.system.process.ProcessUtils.getProcessData1();
// 定义更新ksql,由于概念没有主键关系,所以使用概念别名作为概念的主键
String kSql = "update X5_CostRequest a set a.fState='已审批' where a='" + key + "'";
KSQL.executeUpdate(kSql,null,"/x5demo/bizDemo/data",null);
}
catch (Exception e) {
throw new RuntimeException (e.getMessage());
}
}
2 在平台中使用SQL
使用SQL方式访问数据表时,和标准Java的方法相同。关于事务,即可以使用平台的自动事务,也可以自己控制事务。
(1)使用平台事务
使用com.justep.model.ModelUtils.getConnectionInTransaction(“data模块路径”)方法,获得指定数据模块的数据库连接。这个连接已经在平台的事务中,回滚和提交平台自动处理。连接不必手动关闭,平台会关闭。
System.out.println("##平台负责事务");
java.sql.Connection conn = null;
java.sql.PreparedStatement pstmt = null;
try {
// 取得数据库连接
conn = com.justep.model.ModelUtils.getConnectionInTransaction("/x5demo/bDemo/data");
// 更新一个字段
pstmt = conn.prepareStatement("update DE_JKSQD set fJE = fJE+1 where fID = ?");
pstmt.setString(1, com.justep.system.process.ProcessUtils.getProcessData1());
pstmt.execute();
// 更新另一个字段
pstmt = conn.prepareStatement("update DE_JKSQD set fBZ = '审批中' where fID = ?");
pstmt.setString(1, com.justep.system.process.ProcessUtils.getProcessData1());
pstmt.execute();
} catch (NamingException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
//手工回滚事务或者抛出异常都可以
//ContextHelper.getTransaction().rollback();
throw new RuntimeException(e);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
System.out.println("##close.SQLException");
}
}
(2)自己控制事务
使用com.justep.system.data.Transaction() 方法,获得一个新的事务,使用getConnection(“data模块路径”)方法,获得指定数据模块的数据库连接。这个事务不在平台的事务中,因此回滚和提交都需要处理。这个连接需要手动关闭。
开始事务——begin()
提交事务——commit()
回滚事务——rollback()
System.out.println("##自己控制事务");
java.sql.Connection conn = null;
java.sql.PreparedStatement pstmt = null;
com.justep.system.data.Transaction tx = null;
try {
// 取得数据库连接
tx = new com.justep.system.data.Transaction();
conn = tx.getConnection(DataModel);
tx.begin();
// 更新一个字段
pstmt = conn.prepareStatement("update DE_JKSQD set fJE = fJE+1 where fID = ?");
pstmt.setString(1, com.justep.system.process.ProcessUtils.getProcessData1());
pstmt.execute();
// 更新另一个字段
pstmt = conn.prepareStatement("update DE_JKSQD set fBZ = '审批中' where fID = ?");
pstmt.setString(1, com.justep.system.process.ProcessUtils.getProcessData1());
pstmt.execute();
tx.commit();
} catch (NamingException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
tx.rollback();
throw new RuntimeException(e);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
System.out.println("##close.SQLException");
}
}
本文由WeX5君整理,WeX5一款开源免费的html5开发工具,H5 App开发就用WeX5!
阅读其他app 开发相关文章:http://doc.wex5.com/?p=3443
评一波