package net;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Locale;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
/**
* @author 张宏亮
* @Date 2006-3-1
* version 1.0
* explain 通用方法的封装类
* Email it5719@163.com
*/
public class ActionUtil {
/**
* 截取中文字符串的方法
* @param str 要截取的字符串,bytes截取之后每个串的字节数
* @return 包含截串的结果的ArrayList
* @throws Exception 截取汉字异常
*/
public static ArrayList splitString(String str,int bytes) throws Exception{
ArrayList array = new ArrayList();
try {
String splitStr = str;
int splitByte = bytes;
int loopCount = 0;
loopCount = (splitStr.length()%splitByte==0)?(splitStr.length()/splitByte):(splitStr.length()/splitByte+1);
//System.out.println("字符串的长度是"+splitStr.length()+",要截取成"+loopCount+"段,因为每段需要截取"+splitByte+"字节!");
for(int i=1;i<=loopCount;i++) {
if(i==loopCount)
array.add(splitStr.substring((i-1)*splitByte,splitStr.length()));
else
array.add(splitStr.substring((i-1)*splitByte,(i*splitByte)));
}
Iterator it = array.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
} catch (RuntimeException e) {
e.printStackTrace();
throw new Exception("截取汉字发生异常");
}
return array;
}
/**
* 测试池连接的方法
* @param name 连接池的名字在TOMCAT中配置,sql 要发送的SQL语句
* @return void
* @throws Exception 数据库连接异常,NamingException JNDI初始化异常
*/
public static void poolConnection(String name,String sql) {
Connection con = null;
Statement stmt = null;
try {
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(name);
con = ds.getConnection();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.print(rs.getString(1));
System.out.print(rs.getString(2));
System.out.print(rs.getString(3));
}
} catch (NamingException e) {
System.out.println("JNDI初始化异常");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("数据库连接异常");
e.printStackTrace();
}
finally {
try {
stmt.close();
con.close();
} catch (SQLException e) {
System.out.println("关闭连接池异常");
e.printStackTrace();
}
}
}
/**
* 输入身份证号码返回年龄
* @param id 身份证号码
* @return 年龄
* @throws Exception 身份证号码不正确(长度不够)
*/
public static int getAge(String id) throws Exception {
int age = -1;
int length = id.length();
String birthday = "";
if (length == 15) {
birthday = id.substring(6, 8);
birthday = "19" + birthday;
} else if (length == 18)
birthday = id.substring(6, 10);
else
throw new Exception("错误的身份证号");
int currentYear = Calendar.getInstance().get(1);
age = currentYear - (new Integer(birthday)).intValue();
System.out.println("您的身份证号码是"+id+",您的年龄是"+age);
return age;
}
/**
* 获得当前日期字符串
* @return 日期字符串
*/
public static String getCurrentDate() {
Calendar cal = Calendar.getInstance();
String currentDate = null;
java.util.Date d = cal.getTime();
String currentYear = (new Integer(cal.get(1))).toString();
String currentMonth = null;
String currentDay = null;
if (cal.get(2) < 9)
currentMonth = "0" + (new Integer(cal.get(2) + 1)).toString();
else
currentMonth = (new Integer(cal.get(2) + 1)).toString();
if (cal.get(5) < 10)
currentDay = "0" + (new Integer(cal.get(5))).toString();
else
currentDay = (new Integer(cal.get(5))).toString();
currentDate = currentYear + currentMonth + currentDay;
System.out.println(currentDate);
return currentDate;
}
public static void main(String[] args) {
try {
getCurrentDate();
getAge("220103198601211017");
//因为池连方法需要数据源,所以在着就不测试了
splitString("《2005年国民经济和社会发展统计公报》",3);
} catch (Exception e) {
e.printStackTrace();
System.out.println("测试发生异常");
}
}
}