📄 updatepricedao.java
字号:
package com.hr.persistence.Idao;
import java.math.BigDecimal;
import java.text.*;
import java.util.*;
import com.hr.bean.*;
import com.hr.persistence.conn.PersistenceConst;
import com.letpower.framework.persistence.access.operator.QueryOperator;
import com.letpower.framework.persistence.access.operator.RsDataSet;
import com.letpower.framework.persistence.access.operator.SqlParameter;
import com.letpower.framework.persistence.access.operator.SqlType;
import com.letpower.framework.persistence.access.operator.UpdateOperator;
/**************************************************************************
*类 名: QUT_PDTPRICE_SIMM
*功 能: 更新数据库,在产品价格公告板模板表基础上(QUT_PDTPRICE)中添加两个字段,DIR,Count:DIR(价格上/下情况),Count(计算上/下多少次)并且这两个字段都是进行随机取值,
* 实现K图的真实性;
*编 程 人: gongpei
*编程日期: 2009-04-27
*修 改 人:
*修改日期:
*修改记录:
**************************************************************************/
/**
* 实现功能: 1.对表产品价格公告版表(QUT_PDTPRICE_SIMM)进行添加数据;
* 2.当程序开始运行的时候查出表中离当前时间最近的那条数据,以他的买入价
* ,卖出价和中间价作为新程序运行的初始价格,这样可以保证K图的连续性和真实性,同时得到这条信息中的DIR方向
* ,以及上次程序运行时共进行了多少次,通过select查询出这几个变量;
* 3.在获取上述的几个变量时,通过获取一系列的随机数,对产品价格公告版表进行无限循环插入数据,直到程序停止运行;
*/
public class UpdatePriceDAO {
public String sql = null;
public Market market = null; // 市场价格源信息对象
public UpdatePriceDAO(Market m) {
this.market = m;
}
// 当价格不变化时,只更新数据库价格浮动标志和更新时间。
public boolean updateUdfg() {
boolean flag = false; //更新数据库成功与否标志
UpdateOperator update = new UpdateOperator();
update.setDataSourceName(PersistenceConst.SYSDATASOURCE_DEFAULT);
sql = "update " + market.getTable()
+ " set MDTM=?,UDFG=0";
update.setSql(sql);
/**
* SimpleDateFormat是创建时间格式的类,把系统当前时间以"yyyy-MM-dd HH:mm:ss"格式输出的,
* SimpleDateFormat类中的format方法是将给定的 Date 格式化为日期/时间字符串,
* Calendar类中的getIntance方法是获取当前系统时间以毫秒为单位;
*/
String date = new SimpleDateFormat("yyyyMMdd HH:mm:ss")
.format(Calendar.getInstance().getTime());
update.addParameter(new SqlParameter(SqlType.CHAR, date));
update.access();
if (update.getEffectCounts() > 0) {
System.out.println("更新价格浮动标志和更新时间成功!");
flag = true;
}
return flag;
}
// 查询并更新数据库价格信息
public List<Price> selectPrice() {
List<Price> priceList = new ArrayList<Price>();
sql = "select * from " + market.getTable();
QueryOperator query = new QueryOperator();
query.setDataSourceName(PersistenceConst.SYSDATASOURCE_DEFAULT);
query.setSql(sql);
query.access();
RsDataSet rs = new RsDataSet(query.getSqlResultSet());
if (rs.rowCount > 0) {
for (int i = 0; i < rs.rowCount; i++) {
Price price = new Price();
price.setSequ(rs.getFieldValueAsString("SEQU").toString()); // 价格流水号
price.setExnm(rs.getFieldValueAsString("EXNM").toString()); // 币别对名称
BigDecimal neby = rs.getFieldValueAsBigDecimal("NEBY"); // 买入价格
BigDecimal nesl = rs.getFieldValueAsBigDecimal("NESL"); // 卖出价格
// System.out.println(neby + "变化前价格 " + nesl);
/**
* 读取随机数组进行增加;每随机增加数值;
*/
int j[] = { 1, 2, 3 };
Random rd = new Random();
BigDecimal f = new BigDecimal(String.valueOf(j[rd.nextInt(3)] * 0.0001));
NumberFormat format = new DecimalFormat("#0.00000");// 对数值进行初始化;
/**
* 在dir数组中随机元素,作为价格的上升/下降的方向;
*/
String dir[] = { "1", "2" };
Random ran = new Random();
String udfg = dir[ran.nextInt(2)];
// 2价格上升,1价格下降
if (udfg.equals("2")) {
neby = neby.add(f);
nesl = nesl.add(f);
} else {
neby = neby.subtract(f);
nesl = nesl.subtract(f);
}
neby = new BigDecimal(format.format(neby.doubleValue()));
nesl = new BigDecimal(format.format(nesl.doubleValue()));
// System.out.print(neby + "变化后价格 " + nesl);
BigDecimal nemd = (neby.add(nesl)).divide(new BigDecimal(2), 5,
BigDecimal.ROUND_HALF_UP); // 中间价是(买入价+卖出价价)/2;
// System.out.println(" 中间价nemd= " + nemd);
price.setNeby(neby); // 买入价格
price.setNesl(nesl); // 卖出价格
price.setNemd(nemd); // 中间价格
price.setUDFG(udfg); // 浮动标志
priceList.add(price);
rs.next();
}
rs.clearAll();
}
query.closeResource();
query = null;
rs = null;
return priceList;
}
// 更新数据库价格信息
public boolean updatePrice(List<Price> list) {
boolean flag = false; //更新数据库成功与否标志
for (int i = 0; i < list.size(); i++) {
Price price = (Price) list.get(i);
UpdateOperator update = new UpdateOperator();
update.setDataSourceName(PersistenceConst.SYSDATASOURCE_DEFAULT);
sql = "update " + market.getTable()
+ " set MDTM=?,NEBY=?,NESL=?,NEMD=?,UDFG=?,COUNT=COUNT+1 where SEQU=?";
update.setSql(sql);
/**
* SimpleDateFormat是创建时间格式的类,把系统当前时间以"yyyy-MM-dd HH:mm:ss"格式输出的,
* SimpleDateFormat类中的format方法是将给定的 Date 格式化为日期/时间字符串,
* Calendar类中的getIntance方法是获取当前系统时间以毫秒为单位;
*/
String date = new SimpleDateFormat("yyyyMMdd HH:mm:ss")
.format(Calendar.getInstance().getTime());
update.addParameter(new SqlParameter(SqlType.CHAR, date));
update.addParameter(new SqlParameter(SqlType.DOUBLE, price.getNeby())); //买入价
update.addParameter(new SqlParameter(SqlType.DOUBLE, price.getNesl())); //卖出价
update.addParameter(new SqlParameter(SqlType.DOUBLE, price.getNemd())); //中间价
update.addParameter(new SqlParameter(SqlType.CHAR, price.getUDFG())); //价格浮动标志
update.addParameter(new SqlParameter(SqlType.CHAR, price.getSequ())); //价格流水号
update.access();
if (update.getEffectCounts() > 0) {
System.out.println("第"+i+"条记录更新成功!");
flag = true;
}
update = null;
}
// System.out.println("更新数据库价格信息成功与否标志flag====== "+flag);
return flag;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -