少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

#

//这里面主要是由于银联的手机支付用到了一个付款的转换方式问题,当调用支付宝付款的时候,比如一分钱格式就是我们普通用的格式:0.01,而银联确要把格式转化为000000000001(银联要求是12位)这种格式的,最后一位是分,然后向前依次是角,元,十元,百元,相信不用我说,你已经懂了吧。所以这里就需要一个转换,为了应对像1000,1000.00,1000.0这种传过来的格式的数据,特做一下转换,然后传给银联进行支付。


package com.abin.lee.collection.inner;
public class ConsumeFinance {
public static enum Currency{
CHINARMB("chinarmb"),DOLLAR("dollar"),HK("hk");
private Currency(String textVal){
this.textVal=textVal;
}
private String textVal;
public String toString(){
return textVal;
}
}
public static String ConsumeProcess(String money,Currency currency){
System.out.println("currentcy="+currency);
String flag=null;
if(currency.equals(Currency.CHINARMB)){
ConsumeFinance.ChinaProcess chinaProcess=new ConsumeFinance.ChinaProcess();
flag=chinaProcess.chinaChange(money, currency);
}
return flag;
public static String ConsumeProcessReturn(String money,Currency currency){
System.out.println("currentcy="+currency);
String flag=null;
if(currency.equals(Currency.CHINARMB)){
ConsumeFinance.ChinaReturn chinaReturn=new ConsumeFinance.ChinaReturn();
flag=chinaReturn.chinaReChange(money, currency);
}
return flag;
public static class ChinaProcess{
private static String status=null;
public static String chinaChange(String money,Currency currency){
int point=money.lastIndexOf(".");
if(point==-1){
StringBuffer stb=new StringBuffer();
int len=money.length();
int lon=10-len;
for(int i=0;i<lon;i++){
stb.append("0");
}
stb.append(money);
stb.append("00");
status=stb.toString().intern();
}
if(point>0){
int len=money.indexOf(".");
int chn=money.length()-len-1;
switch(chn){
//先判断money格式为100.0这种格式的,小数点后面有一位小数点的情况
case  1:
StringBuffer stb=new StringBuffer(money);
stb.deleteCharAt(len);
int lend=stb.length();
StringBuffer sb=new StringBuffer();
for(int i=0;i<12-lend;i++){
sb.append("0");
}
sb.append(stb.toString());
status=sb.toString().intern();
//先判断money格式为100.01这种格式的,小数点后面有两位小数点的情况
case 2:
money=money.replace(".", "");
int lende=money.length();
String connect="";
for(int i=0;i<12-lende;i++){
connect+="0";
}
status=connect+money;
}
}
return status;
}
}
//把000000001001转化为10.01这种格式
public static class ChinaReturn{
private static String status=null;
public static String chinaReChange(String money,Currency currency){
int change=money.length();
int num=0;
for(int i=0;i<change;i++){
int value=Integer.valueOf(String.valueOf(money.charAt(i)));
if(value>0){
num=i;
break;
}
}
money=money.substring(num);
if(num==10){
status="0."+money;
}
if(num==11){
status="0.0"+money;
}
if(num<10){
System.out.println(money.length());
StringBuffer stb=new StringBuffer(money);
stb.insert(money.length()-2, ".");
status=stb.toString().intern();
}
return status;
}
}
public static void main(String[] args) {
Currency currency=Currency.CHINARMB;
String money="000000001001";
String result=ConsumeProcessReturn(money, currency);
// String result=ConsumeProcess(money, currency);
System.out.println("result="+result);
}
}
posted @ 2012-09-02 00:03 abin 阅读(1861) | 评论 (0)编辑 收藏

//ThreadLocal第一种用法:

//建表:
-- Create table
create table USERBEAN
(
  ID       NVARCHAR2(40) not null,
  USERNAME NVARCHAR2(40),
  PASSWORD NVARCHAR2(40)
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints 
alter table USERBEAN
  add constraint SSSID primary key (ID)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );





package com.abin.lee.collection.threadlocal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MyThreadLocal {
//使用ThreadLocal保存Connection变量
public static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
private static final String URL = "jdbc:oracle:thin:@localhost:1521:XE";
private static final String USERNAME = "abing";
private static final String PWD = "abing";
public static Connection getConnection() {
Connection connection = null;
String url = "";
//如果connThreadLocal没有本线程对应的Connection创建一个新的Connection,并将其保存到线程本地变量中。
if (threadLocal.get() == null) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
connection = DriverManager.getConnection(URL, USERNAME, PWD);
threadLocal.set(connection);
} catch (Exception e) {
e.printStackTrace();
}
}else {
return threadLocal.get();//直接返回线程本地变量
}
return connection;
}
public static void remove() {
threadLocal.remove();
}
}




package com.abin.lee.collection.threadlocal;
import java.io.Serializable;
public class UserBean implements Serializable{
private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}





//测试代码
package com.abin.lee.collection.threadlocal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class OracleOperate {
public static List<UserBean> findAll(){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select * from userbean order by id desc";
List<UserBean> list=new ArrayList<UserBean>();
UserBean user=null;
try {
conn=MyThreadLocal.getConnection();
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while (rs.next()&&rs!=null) {
user=new UserBean();
user.setId(rs.getString("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
List<UserBean> list=findAll();
for(UserBean lst:list){
System.out.println("id="+lst.getId());
System.out.println("username="+lst.getUsername());
System.out.println("id="+lst.getPassword());
}
}
}










//ThreadLocal第二种用法: 
package com.abin.lee.collection.threadlocal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MyThreadLocal {
private static final String URL = "jdbc:oracle:thin:@localhost:1521:XE";
private static final String USERNAME = "abing";
private static final String PWD = "abing";
//使用ThreadLocal保存Connection变量
public static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>(){
protected Connection initialValue() {
Connection connection = null;
//如果connThreadLocal没有本线程对应的Connection创建一个新的Connection,并将其保存到线程本地变量中。
if (connection == null) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
connection = DriverManager.getConnection(URL, USERNAME, PWD);
} catch (Exception e) {
e.printStackTrace();
}
}
return connection;
}
};
public static Connection getConnection() {
return threadLocal.get();
}
public static void remove() {
threadLocal.remove();
}
}




package com.abin.lee.collection.threadlocal;
import java.io.Serializable;
public class UserBean implements Serializable{
private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}






package com.abin.lee.collection.threadlocal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class OracleOperate {
public static List<UserBean> findAll(){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select * from userbean order by id desc";
List<UserBean> list=new ArrayList<UserBean>();
UserBean user=null;
try {
conn=MyThreadLocal.getConnection();
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while (rs.next()&&rs!=null) {
user=new UserBean();
user.setId(rs.getString("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
List<UserBean> list=findAll();
for(UserBean lst:list){
System.out.println("id="+lst.getId());
System.out.println("username="+lst.getUsername());
System.out.println("id="+lst.getPassword());
}
}
}
posted @ 2012-09-01 17:03 abin 阅读(447) | 评论 (0)编辑 收藏

package com.abin.lee.collection.volatiler;
public class MyThread implements Runnable{
private volatile boolean flag=false;
public void run() {
while(!flag){
try {
System.out.println("before");
Thread.sleep(3000);
System.out.println("after");
} catch (Exception e) {
e.printStackTrace();
}
flag=true;
}
}
public void setDone(boolean flag){
this.flag=flag;
}
}






测试代码:
package com.abin.lee.collection.volatiler;
public class MyVolatileOne {
public static void main(String[] args) {
MyThread myThread=new MyThread();
Thread thread=new Thread(myThread);
thread.start();
}
}
posted @ 2012-09-01 15:56 abin 阅读(293) | 评论 (0)编辑 收藏

package com.abin.lee.collection.singleton;
/**
 * 双重检查加锁 单例模式
 * @author abin
 * 这个单例模式写法可以大大减少getInstance的时间消耗!
 */
public class SingletonOne {
  /** 
     * volatile关键词确保:当singleton变量被初始化成SingletonOne实例时,多个线程正确地处理singleton变量 
     */ 
 private volatile static SingletonOne singleton=null;
 public SingletonOne(){}
 public static SingletonOne getInstance(){
  if(null==singleton){//检查实例,如果不存在就进入同步区块
   synchronized (SingletonOne.class) {//注意,只有第一次才彻底执行这里的代码
    if(null!=singleton){
     singleton=new SingletonOne();
    }
   }
  }
  return singleton;
 }
}





package com.abin.lee.collection.singleton;

public class SingletonTwo {
 /**
  * volatile关键词确保:当uniqueInstance变量被初始化成Singleton实例时,
  * 多个线程正确地处理uniqueInstance变量
  */
 private volatile static SingletonTwo singleton = new SingletonTwo();

 private SingletonTwo() {
 }

 public static SingletonTwo getInstance() {
  return singleton;
 }
}

posted @ 2012-09-01 00:27 abin 阅读(652) | 评论 (0)编辑 收藏

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
    function checkAll() {
        var m = document.getElementsByName("all")[0];
        var s = document.getElementsByName("check");

        if (m.checked) {

            for ( var i = 0; i < s.length; i++) {
                s[i].checked = true;
            }
        } else {
            for ( var i = 0; i < s.length; i++) {
                s[i].checked = false;
            }

        }
    }

    function checkEvery() {
        var m = document.getElementsByName("all")[0];
        var s = document.getElementsByName("check");

        for ( var i = 0; i < s.length; i++) {
            if (s[i].checked&&m.checked) {
                m.checked = true;
            } else {
                m.checked = false;
            }
        }

    }

    function turn() {
        with (document) {
            var m = getElementById("change");
            var n = getElementById("table");
            if (m.value == "收缩") {
                n.style.display = "none";
                m.value = "展开";
            } else {
                n.style.display = "block";
                m.value = "收缩";
            }
        }
    }
</script>
</head>
<body>
    <table border="1" align="center" width="60%" id="table1">
        <tr>
            <td><input type="checkbox" name="all" onclick="checkAll();">全选</td>
            <td><input type="button" value="收缩" id="change"
                onclick="turn();"></td>
        </tr>
    </table>
    <%
        int number = Integer.parseInt(request.getParameter("number"));
    %>

    <table border="1" align="center" width="60%" id="table">
        <%
            for (int i = 0; i < number; i++) {
        %>
        <tr>
            <td><input type="checkbox" name="check" onclick="checkEvery()"></td>
            <td><%=i%></td>
        </tr>
        <%
            }
        %>
    </table>
</body>
</html>
posted @ 2012-08-30 19:50 abin 阅读(2015) | 评论 (2)编辑 收藏

<!-- 获取复选框的值-->
<script language=javascript>
function c()
{
   var c=document.getElementsByName("aaa");//getElement s ByName忘掉了一直没注意郁闷死
   for( var i=0;i<c.length;i++)
   {
    if(c[i].checked)
    {
     alert(c[i].value);
    }
   }
}
function d()
{
   var check=document.getElementsByName("aaa");
   for(i=0;i<check.length;i++)
   {
    //三元运算符,不知道为什么我用非(~)运算符,只能全选,只有选这个用了
    check[i].checked=check[i].checked?false:true;
   }
}
function e()
{
   var check=document.getElementsByName("aaa");
   for(i=0;i<check.length;i++)
   {
    check[i].checked=true;
   }
  
}
</script>

<input type="checkbox" name="aaa" value="1" />1
<input type="checkbox" name="aaa" value="2" />2
<input type="checkbox" name="aaa" value="3" />3
<input type="checkbox" name="aaa" value="4" />4
<input type="checkbox" name="aaa" value="5" />5
<input type="checkbox" name="aaa" value="6" />6
<input type="button" value="选中查看" onclick="c()"/>
<input type="button" value="反选" onclick="d()" />
<input type="button" value="全选" onclick="e()" />
<br />


如果需要将复选框选中的值操作,可在循环里面加字符串拼接,每个用逗号隔开,在js里页面跳转,凭借的字符串如果是数组,作为url传递的参数,接手后不是数组,是字符串形式的,只要用函数分割就ok了。

posted @ 2012-08-30 17:45 abin 阅读(485) | 评论 (0)编辑 收藏

package com.abin.lee.dom4j;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class CreateDom4j {
 public static String createXml(){
  Document document=DocumentHelper.createDocument();
  Element school=document.addElement("school");
  Element teacher=school.addElement("teacher");
  Element student=teacher.addElement("student");
  student.addText("abin");
  Element boy=teacher.addElement("boy");
  boy.addText("han");
  Element girl=teacher.addElement("girl");
  girl.addText("xia");
  
  return document.asXML();
 }
 
 public static Map<String, String> parseXml(String xml){
  Map<String, String> map=new HashMap<String, String>();
  Document doc=null;
  try {
   doc=DocumentHelper.parseText(xml);
   Element school=doc.getRootElement();
   System.out.println("school Name="+school.getName());
   System.out.println("school Value="+school.getText());
   map.put(school.getName(), school.getText());
   
   Element teacher=school.element("teacher");
   map.put(teacher.getName(), teacher.getText());
   
   Element student=teacher.element("student");
   map.put(student.getName(), student.getText());
   
   Element boy=teacher.element("boy");
   map.put(boy.getName(), boy.getText());
   
   Element girl=teacher.element("girl");
   map.put(girl.getName(), girl.getText());
  } catch (Exception e) {
   e.printStackTrace();
  }
  return map;
 }
 
 public static Map<String, String> parseString(String xml){
  Map<String, String> map=new HashMap<String, String>();
  Document doc=null;
  try {
   doc=DocumentHelper.parseText(xml);
   Element school=doc.getRootElement();
   map.put(school.getName(), school.getText());
   
   Element teacher=school.element("teacher");
   map.put(teacher.getName(), teacher.getText());
   
   for(Iterator it=teacher.elementIterator();it.hasNext();){
    Element element=(Element)it.next();
    map.put(element.getName(), element.getText());
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return map;
 }
 public static void main(String[] args) {
  String result=createXml();
  System.out.println("result="+result);
//  Map<String, String> map=parseXml(result);
//  for(Iterator it=map.entrySet().iterator();it.hasNext();){
//   Map.Entry entry=(Map.Entry)it.next();
//   System.out.println("entry name="+entry.getKey());
//   System.out.println("entry value="+entry.getValue());
//  }
  
  Map<String, String> map=parseString(result);
  for(Iterator it=map.entrySet().iterator();it.hasNext();){
   Map.Entry entry=(Map.Entry)it.next();
   System.out.println("entry name="+entry.getKey());
   System.out.println("entry value="+entry.getValue());
  }
  
  
 }

}


http://hi.baidu.com/stevenjohn/item/aababd47077c0ce5dd0f6c78

posted @ 2012-08-29 22:45 abin 阅读(683) | 评论 (0)编辑 收藏

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>javascript实现表格动态增加删除</title>

<script>
 function addline() {
  var table1=document.createElement("table");
  var row=table1.insertRow();
  var td1=row.insertCell();
  td1.innerHTML=document.getElementById("name1").value;
  var td2=row.insertCell();
  td2.innerHTML=document.getElementById("age1").value;
  var td3=row.insertCell();
  td3.innerHTML='<input type="button" value="删除" onclick="deleteRow(this.parentElement)" />';
  
  document.getElementById("div1").appendChild(table1); 
  table1.setAttribute('border','1');
 }
 function deleteRow(obj) {
  obj.parentElement.removeChild(obj);
 }
</script>
<body>
 <div id="div1" style="">
 
 </div>
 <input type="text" name="name" id="name1" />
 <input type="text" name="age" id="age1" />
 <input type="button" value="PRESS" onclick="return addline();" />
</body>
</html>

posted @ 2012-08-28 23:55 abin 阅读(392) | 评论 (0)编辑 收藏

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>javascript实现表格动态增加</title>

<script>
 function addline() {
  var table1=document.getElementById("tb1");
  var row1=document.createElement("tr");
  var td1=document.createElement("td");
  var td2=document.createElement("td");
  var td3=document.createElement("td");
  var id1=document.getElementById("id1").value;
  var name1=document.getElementById("name1").value;
  var age1=document.getElementById("age1").value;
  var txt1=document.createTextNode(id1);
  var txt2=document.createTextNode(name1);
  var txt3=document.createTextNode(age1);
  td1.appendChild(txt1);
  td2.appendChild(txt2);
  td3.appendChild(txt3);
  row1.appendChild(td1);
  row1.appendChild(td2);
  row1.appendChild(td3);
  table1.appendChild(row1);
 }
 
</script>
<body id="bd">
 <table id="tb1" border="1">
  <tr>
   <td>ID</td>
   <td>NAME</td>
   <td>AGE</td>
  </tr>
 </table>
 <input type="text" id="id1" />
 <input type="text" id="name1" />
 <input type="text" id="age1" />
 <br/>
 <input type="button" value="PRESS" onclick="return addline();"/>
</body>
</html>

posted @ 2012-08-28 23:24 abin 阅读(393) | 评论 (0)编辑 收藏

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>javascript实现表格动态增加删除</title>

<script>
 function addline() {
  var tb = document.getElementById("mytable");
  var row = tb.insertRow(tb.rows.length);
  var colname = row.insertCell(row.cells.length);
  var colage = row.insertCell(row.cells.length);
  var colbutton = row.insertCell(row.cells.length);

  colname.innerHTML = document.getElementById("name1").value;
  colage.innerHTML = document.getElementById("age1").value;
  colbutton.innerHTML = '<input type="button" value="删除" onclick="deleteRow('
    + (tb.rows.length - 1) + ')" />'
 }
 function deleteRow(index) {
  var tb = document.getElementById("mytable");
  tb.deleteRow(index);
 }
</script>
<body>
 <table id="mytable" border="1">
  <tr>
   <td>NAME</td>
   <td>AGE</td>
   <td>OPERATE</td>
  </tr>
 </table>
 <input type="text" name="name" id="name1" />
 <input type="text" name="age" id="age1" />
 <input type="button" value="PRESS" onclick="return addline();" />
</body>
</html>

posted @ 2012-08-28 23:21 abin 阅读(818) | 评论 (0)编辑 收藏

仅列出标题
共50页: First 上一页 30 31 32 33 34 35 36 37 38 下一页 Last