JS精确计算 |
|
|
/**
* 乘法(精确)
* @param arg1
* @param arg2
*/
function accMul(arg1, arg2){
if(arg1==null || arg1=="" || arg1==undefined || arg2==null || arg2=="" || arg2==undefined){
return;
}
var m=0,s1=arg1.toString(),s2=arg2.toString();
var result, len=0;
try{
m+=s1.split(".")[1].length
}catch(e){}
try{
m+=s2.split(".")[1].length
}catch(e){}
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
}
/**
* 除法(精确)
* @param arg1
* @param arg2
* @returns {number}
*/
function accDiv(arg1,arg2){
var t1=0,t2=0,r1,r2;
try{
t1=arg1.toString().split(".")[1].length
}catch(e){}
try{
t2=arg2.toString().split(".")[1].length
}catch(e){}
with(Math){
r1=Number(arg1.toString().replace(".",""));
r2=Number(arg2.toString().replace(".",""));
return (r1/r2)*pow(10,t2-t1);
}
}
/**
* 加法(精确)
* @param arg1
* @param arg2
* @returns {number}
*/
function accAdd(arg1,arg2){
var r1,r2,m;
try{
r1=arg1.toString().split(".")[1].length
}catch(e){r1=0}
try{
r2=arg2.toString().split(".")[1].length
}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2));
return (arg1*m+arg2*m)/m
}
/**
* 精度控制为7位以内
* @param arg
*/
function prec(arg){
if(arg==null || arg=="" || arg==undefined){
return;
}
var a = arg.toString();
try{
if(a.split(".")[1].length > 7){
arg = arg.toFixed(7);
}
}catch(e){}
return arg;
}
|
JS数组去重 |
|
|
/**
* 数组去重
* @returns {*[]}
*/
Array.prototype.unique = function()
{
var n = {},r=[]; //n为hash表,r为临时数组
for(var i = 0; i < this.length; i++) //遍历当前数组
{
if (!n[this[i].gdsId]) //如果hash表中没有当前项
{
n[this[i].gdsId] = true; //存入hash表
r.push(this[i].gdsId); //把当前数组的当前项push到临时数组里面
}
}
return r;
}
|
mysql分区表执行计划 |
|
|
explain partitions select * from mf_production_line_his
WHERE SECTION BETWEEN DATE('2014-01-23') AND DATE ('2015-12-01')
AND CREATED_TIME BETWEEN DATE('2014-01-23') AND DATE ('2015-12-01')
;
|
db2中关于递归(with)的使用 |
|
|
在with在oracle中称为家族树!pub中oracle模块对此类的使用已经很成熟!
因为有人问及,这边简单的再探讨一下
-------------------
1.创建测试表
create table zxt_test
( id varchar(10),
ivalue varchar(20),
iname varchar(20)
)
not logged initially;
commit;
-----------
2.插入测试语句
insert into zxt_test values('1','aa','x'),('2','bb','x'),('3','bb','x'),('1','bb','y'),('2','bb','y'),('3','bb','y');
commit;
---------------
3.查看数据
select * from zxt_test;
1 'aa' 'x'
2 'bb' 'x'
3 'bb' 'x'
1 'bb' 'y'
2 'bb' 'y'
3 'bb' 'y'
----------------
4.with 的写法!
with
s as (
select row_number()over(partition by iname order by id) id1,
row_number()over(partition by iname order by id) id2,
ivalue,iname from zxt_test
)
,
t(iname,id1,id2,ivalue) as
(
select iname,id1,id2,cast(ivalue as varchar(100)) from s where id1 =1 and id2=1
union all
select t.iname,t.id1+1,t.id2,cast(s.ivalue||','||t.ivalue as varchar(100))
from s, t
where s.id2=t.id1+1 and t.iname = s.iname
)
select iname,ivalue from t where t.id1= (select max(id1) from s where s.iname = t.iname);
5.结果如下:
'x' 'bb,bb,aa'
'y' 'bb,bb,bb'
---------
|
递归 |
|
|
with rpl (parentid,rowid)
as (
select root.PARENT_MODULE_ID, root.row_id
from ad_program root
union all
select child.PARENT_MODULE_ID, child.row_id
from rpl parent, ad_program child
where parent.rowid = child.PARENT_MODULE_ID
) select rowid, parentid from rpl;
|
取得一个字符串占有的字节数 |
|
|
/**
* 取得一个字符串占有的字节数 其中汉字占据的字节数在系统变量Global.js中定义
* @param {String} inValue 要计算长度的字符串
* @return 字符串的长度
*/
bitLength=function(inValue){
inValue=inValue.toString();
if(inValue==null || inValue == "") return 0;
var len = 0;
for(var i=0; i < inValue.length; i++){
if(inValue.substring(i,i+1).charCodeAt(0) < 128){
len++;
continue;
}
len+=unieap.Global.bitsOfOneChinese;
}
return len;
}
|
数字格式转换函数 |
|
|
/**
* 数字格式转换函数
*
* @param {Number} inValue 输入值
* @param {String} dataPattern 格式化格式,形如#,###.00、###.00‰等
*/
numberFormat = function(inValue,dataPattern){
if (inValue == ""||!dataPattern) {
return inValue;
}
if (/\,/g.test(inValue)) {
inValue = inValue.replace(/\,/g, '');
}
if (/\-/g.test(inValue)) {
var tempvalue = inValue;
tempvalue = tempvalue.replace(/\-/g, '');
if (inValue.indexOf('-') == 0) {
inValue = "-" + tempvalue;
}
else {
inValue = tempvalue;
}
}
var str = inValue.toString();
var tempPattern = dataPattern;
if (tempPattern == null || tempPattern == "") {
return inValue;
}
if (str.indexOf("-") == 0)
str = str.substr(1);
if (/\,/g.test(str)) {
str = str.replace(/\,/g, "");
}else if(/-/g.test(str)){
str=str.replace(/-/g,"");
}
if (str.indexOf('%') != -1) {//如果数中包含百分号
str = str.substr(0, str.length - 1);
//str=(parseFloat(str)/100).toString();
if (tempPattern.indexOf('%') == tempPattern.length - 1) {
tempPattern = tempPattern.substr(0, tempPattern.length - 1);
}
}else if (str.indexOf('\u2030') != -1) {//如果数中包含千分号
str = str.substr(0, str.length - 1);
if (tempPattern.indexOf('\u2030') != -1) {
tempPattern = tempPattern.substr(0, tempPattern.length - 1);
}
}else {
if (tempPattern.indexOf('%') == tempPattern.length - 1) {
// str = (parseFloat(str) * 100).toString();
tempPattern = tempPattern.substr(0, tempPattern.length - 1);
}else if (tempPattern.indexOf('\u2030') != -1) {
//console.log("");
//str = (parseFloat(str) * 1000).toString();
tempPattern = tempPattern.substr(0, tempPattern.length - 1);
//console.log("tempPattern"+tempPattern);
}
}
var number = str;
var strInt;
var strFloat;
var formatInt;
var formatFloat;
if (/\./g.test(tempPattern)) {//判断格式化串中是否包含小数点
formatInt = tempPattern.split('.')[0];
formatFloat = tempPattern.split('.')[1];
}
else {
formatInt = tempPattern;
formatFloat = null;
}
if (/\./g.test(str)) {//如果要求格式化的数字串是否包含小数部分
if (formatFloat != null) {//如果要求格式化浮点数
var tempFloat = Math.round(parseFloat('0.' + str.split('.')[1]) * Math.pow(10, formatFloat.length)) / Math.pow(10, formatFloat.length);
//strInt = (Math.floor(number) + Math.floor(tempFloat)).toString();
strInt = number.toString().split('.')[0];
strFloat = /\./g.test(tempFloat.toString()) ? tempFloat.toString().split('.')[1] : '0';
}
else {//不要求格式化浮点数
//strInt = Math.round(number).toString();
strInt = number.toString().split('.')[0];
strFloat = '0';
}
}
else {
strInt = str;
strFloat = '0';
}
if (formatInt != null) {
var outputInt = '';
//找出匹配中零的个数,零用于占位
var zero = formatInt.match(/0*$/)[0].length;
var comma = null;
if (/,/g.test(formatInt)) {
comma = formatInt.match(/,[^,]*/)[0].length - 1;
// console.log(comma);
}
else
if (/-/g.test(formatInt)) {
comma = formatInt.match(/-[^-]*/)[0].length - 1;
}
var newReg = new RegExp('(\\d{' + comma + '})', 'g');
if (strInt.length < zero) {
outputInt = new Array(zero + 1).join('0') + strInt;
outputInt = outputInt.substr(outputInt.length - zero, zero)
}
else {
outputInt = strInt;
}
//console.log("format"+strInt);
if (/,/g.test(formatInt)) {
var outputInt = outputInt.substr(0, outputInt.length % comma) + outputInt.substring(outputInt.length % comma).replace(newReg, (comma != null ? ',' : '') + '$1')
outputInt = outputInt.replace(/^,/, '');
}
else
if (/-/g.test(formatInt)) {
var outputInt = outputInt.substr(0, outputInt.length % comma) + outputInt.substring(outputInt.length % comma).replace(newReg, (comma != null ? '-' : '') + '$1')
outputInt = outputInt.replace(/^-/, '');
}
strInt = outputInt;
}
if (formatFloat != null) {
var outputFloat = '';
var zero = formatFloat.match(/^0*/)[0].length;
if (strFloat.length < zero) {//当输入的数据的位数小于格式化的位数时加上少的位数
outputFloat = strFloat + new Array(zero + 1).join('0');
var outputFloat1 = outputFloat.substring(0, zero);
var outputFloat2 = outputFloat.substring(zero, formatFloat.length);
outputFloat = outputFloat1 + outputFloat2.replace(/0*$/, '');
}
else {//否则截去多余的部分
outputFloat = strFloat.substring(0, formatFloat.length);
}
strFloat = outputFloat;
}
else {
if (tempPattern != '' || (tempPattern == '' && strFloat == '0')) {
strFloat = '';
}
}
var temp = strInt + (strFloat == '' ? '' : '.' + strFloat);
if (dataPattern.indexOf('%') != -1||inValue.toString().indexOf('%')!=-1) {
temp += "%";
}
else
if (dataPattern.indexOf('\u2030') != -1) {
//console.log(pattern);
temp += '‰';
//console.log(temp);
}
return inValue.toString().indexOf("-") == 0 ? ("-" + temp) : temp;
} ;
|
日期转换 |
|
|
/**
* 日期转换函数
*
* @param {String|Long} inValue 输入值
* @param {String|Null} datePattern日期格式,默认为"yyyy-MM-dd"
* @return {String} 转换后的日期格式
*/
dateFormat = function(inValue,datePattern){
var date,_t =inValue, retV = datePattern?datePattern:"yyyy-MM-dd";
if((_t=parseInt(inValue,10))+""==inValue){
date = new Date(_t);
}
else{
return inValue;
}
//parse month
if(retV.indexOf("MM")!=-1){
var m = date.getMonth()+1;
m = m<10?"0"+m:m;
retV = retV.replace(/MM/g,m);
}
retV = retV.toLowerCase();
//parse year
if(retV.indexOf("yyyy")!=-1){
retV = retV.replace(/yyyy/g,date.getFullYear());
}else if(retV.indexOf("yy")!=-1){
var year4=date.getFullYear();
var year2=year4.toString().substring(2);
retV = retV.replace(/yy/g,year2);
}
//parse day
if(retV.indexOf("dd")!=-1){
var d = date.getDate();
d = d<10?"0"+d:d;
retV = retV.replace(/dd/g,d);
}
//parse hours
if(retV.indexOf("hh")!=-1){
var h = date.getHours();
h = h<10?"0"+h:h;
retV = retV.replace(/hh/g,h);
}
//parse minute
if(retV.indexOf("mm")!=-1){
var mm = date.getMinutes();
mm = mm<10?"0"+mm:mm;
retV = retV.replace(/mm/g,mm);
}
//parse second
if(retV.indexOf("ss")!=-1){
var s = date.getSeconds();
s = s<10?"0"+s:s;
retV = retV.replace(/ss/g,s);
}
//week
if(retV.indexOf("w")!=-1){
retV = retV.replace(/w/g,"0");
}
//if(retV.indexOf("p")!=-1){
//retV = retV.replace(/p/g,"%P");
//}
return retV;
}
|
java自带native2ascii |
|
|
native2ascii -encoding utf-8 2.properties 3.properties
native2ascii -reverse -encoding utf-8 2.properties 3.properties
|
类型在JVM中的定义 |
|
|
int I
void V
boolean Z
char C
byte B
short S
float F
long L
double D
array [.... 比如[Ljava/lang/String; 表示String数组
其他对象 L....; 比如Ljava/lang/Object; 表示java.lang.Object
方法(参数列表...)返回值类型 比如(Ljava/lang/String;)V 表示该方法有一个类型为String的参数,返回值为void类型
构造函数 (参数列表...)V 比如()V 表示默认构造参数
|
持久层实现 |
|
|
1.混杂模式
混杂模式是持久化功能的原始实现模式.即在业务中混杂JDBC访问代码,从而提供所需的持久化功能.
这种模式的优点在于开发的迅速便捷.对于原型系统或者小型系统显得别具意义.但我们也应该看到,基于这种模式开发的系统,其维护性和扩展性较差,对象属性,数据库结构的变动都将直接导致业务逻辑代码的修改.
实际上,在这种模式中,我们并不能清晰的分辨出所谓"持久层"的逻辑层次.
2.基于Data Class的持久层实现模式
在这种模式中,数据类(DATA CLASS)作为业务类与持久层沟通的桥梁,起着承上启下的作用.
DOMAIN CLASS作为对现实世界的抽象,起着信息携带者的作用.而DATA ACCESSOR CLASS则通过JDBC代码将DOMAIN CLASS与数据库表相关联(.........未完整.)
3.基于现有持久层框架的实现模式
实际上,这种模式只是第二种模式的延伸.DATA CLASSES所包含的DATA ACCESSOR CLASS和DOMAIN CLASS数量并没有减少.只是,我们把其中最为烦琐的工作---基于JDBC的OR映射工作,交由第三方组件完成.
|
生产者消费者 |
|
|
import java.util.concurrent.*;
class Item
{
private String name = "";
public Item(String name)
{
this.name = name;
}
public String toString()
{
return this.name;
}
}
class Consumer implements Runnable
{
private Factory factory;
public Consumer(Factory factory)
{
this.factory = factory;
}
public void run() {
try
{
while(!Thread.interrupted())
{
synchronized(this)
{
if(factory.item == null)
{
wait();
}
}
synchronized(factory.pro)
{ System.out.println("Consume Item..."+ factory.item.toString());
factory.item = null;
factory.pro.notifyAll();
}
}
}
catch(InterruptedException e)
{
System.out.println("Consumer interrupted");
}
}
}
class Producer implements Runnable
{
private Factory factory;
private int count = 0;
public Producer(Factory factory)
{
this.factory = factory;
}
public void run() {
try
{
while(!Thread.interrupted())
{
synchronized(this)
{
if (factory.item != null)
{
wait();
}
}
if(++count == 10) {
System.out.println("Out of Item, closed");
factory.exec.shutdownNow();
System.out.println("shutdown");
}
System.out.println("Produce Item..");
synchronized(factory.con) {
factory.item = new Item("Item" + count);
factory.con.notifyAll();
}
TimeUnit.MILLISECONDS.sleep(100);
}
}
catch(InterruptedException e)
{
System.out.println("Producer interrupted");
}
}
}
public class Factory
{ protected Consumer con = new Consumer(this);
protected Producer pro = new Producer(this);
protected Item item;
ExecutorService exec = Executors.newCachedThreadPool();
public Factory()
{ exec.execute(pro);
exec.execute(con);
}
public static void main(String args[])
{
new Factory();
}
}
|